hdu 1166 (트 리 배열 입문)

1605 단어 cqueryinclude
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 10
using namespace std;
const int MAX = 50010;
int a[MAX], c[MAX];
int n;
int lowbit(int x){
   return x&(-x);
}

int sum(int len){
   int temp = len;
   int result = 0;
   while(temp>0){
      result = result + c[temp];
      temp = temp - lowbit(temp);
   }
   return result;
}

void get_result(int from, int to){
   int temp1 = sum(from - 1);
   int temp2 = sum(to);
   int t = temp2 - temp1;
   printf("%d
", t); } void update(int x, int value){ while(x <= n){ c[x] = c[x] + value; x = x + lowbit(x); } } int init(int n) { for (int i = 0; i <= n; i++) { a[i] = 0; c[i] = 0; } return 0; } int main() { int T, loc, temp; char tem[M]; scanf("%d", &T); for(int k = 1; k <= T; k++) { printf("Case %d:
", k); scanf("%d", &n); init(n); for(int j = 1; j <= n; j++){ scanf("%d", &a[j]); update(j, a[j]); } while(scanf("%s", tem)!=EOF && tem[0] != 'E'){ scanf("%d%d", &loc, &temp); switch(tem[0]){ case 'Q': get_result(loc, temp); break; case 'A': update(loc, temp); break; case 'S': update(loc, temp*(-1)); break; } } } return 0; } /******************** 10 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End ******************/

좋은 웹페이지 즐겨찾기