线段树(单点更新) HDOJ 4288 Coder

 

题目传送门

 1 #include <cstdio>  2 #include <cstring>  3 #define lson l, m, rt << 1  4 #define rson m + 1, r, rt << 1 | 1  5  6 const int MAX_N = 500000 + 10;  7 int sum[MAX_N << 2];  8  9 void pushup(int rt) 10 { 11 if (rt >> 1 % 5 == 3) 12 sum[rt] += sum[rt >> 1]; 13 if (rt >>1 | 1 % 5 == 3) 14 sum[rt] += sum[rt >> 1 | 1]; 15 } 16 17 void build(int l, int r, int rt) 18 { 19 sum[rt] = r - l + 1; 20 if (l == r) 21  { 22 return ; 23  } 24 int m = (l + r) >> 1; 25  build (lson); 26  build (rson); 27  pushup (rt); 28 } 29 30 void update(int p, int l, int r, int rt) 31 { 32 if (l == r) 33  { 34 sum[rt] = p; 35 return ; 36  } 37 int m = (l + r) >> 1; 38 if (p <= m) update (p, lson); 39 else update (p, rson); 40 41  pushup (rt); 42 } 43 44 45 int main(void) //HDOJ 4288 Coder 46 { 47 //freopen ("inG.txt", "r", stdin); 48 int n; 49 char op[10]; 50 int x; 51 52 while (~scanf ("%d", &n)) 53  { 54 build (1, n, 1); 55 while (n--) 56  { 57 scanf ("%s", &op); 58 if (strcmp (op, "add") == 0) 59  { 60 scanf ("%d", &x); 61 update (x, 1, n, 1); 62  } 63 if (strcmp (op, "del") == 0) 64  { 65 scanf ("%d", &x); 66 update (-x, 1, n, 1); 67  } 68 if (strcmp (op, "sum") == 0) 69  { 70 printf ("%d\n", sum[1]); 71  } 72  } 73  } 74 75 return 0; 76 }

 

你可能感兴趣的:(code)