1. 程式人生 > >kuangbin帶我飛QAQ 線段樹

kuangbin帶我飛QAQ 線段樹

ase pen isa ostream namespace i++ ans fine IE

  1. HDU1166

  裸線段樹點修改

技術分享圖片
  1 #include <iostream>
  2 #include <string.h>
  3 #include <cstdio>
  4 #include <queue>
  5 #include <map>
  6 #include <vector>
  7 #include <string>
  8 #include <cstring>
  9 #include <algorithm>
 10 #include <math.h>
 11
12 #define SIGMA_SIZE 26 13 #pragma warning ( disable : 4996 ) 14 15 using namespace std; 16 typedef long long LL; 17 18 inline LL LMax(LL a,LL b) { return a>b?a:b; } 19 inline LL LMin(LL a,LL b) { return a>b?b:a; } 20 inline int Max(int a,int b) { return a>b?a:b; } 21 inline int
Min(int a,int b) { return a>b?b:a; } 22 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); } 23 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; } //a*b = gcd*lcm 24 const long long INF = 0x3f3f3f3f3f3f3f3f; 25 const int inf = 0x3f3f3f3f; 26 const int mod = 7; 27 const int maxk = 5e4+5
; 28 const int maxn = 5e4+5; 29 30 int N; 31 int num[maxn]; 32 int sum[maxn<<2]; 33 char str[10]; 34 35 void init() 36 { 37 memset( num, 0, sizeof(num) ); 38 memset( sum, 0, sizeof(sum) ); 39 } 40 41 void pushup( int rt ) 42 { sum[rt] = sum[rt<<1]+sum[rt<<1|1]; } 43 44 void build( int l, int r, int rt ) 45 { 46 if ( l == r ) 47 { sum[rt] = num[l]; return; } 48 int m = (l+r)>>1; 49 50 build( l, m, rt<<1 ); 51 build( m+1, r, rt<<1|1 ); 52 pushup(rt); 53 } 54 55 void update(int L, int C, int l, int r, int rt) 56 { 57 if( l == r ) 58 { sum[rt]+=C; return; } 59 60 int m=(l+r)>>1; 61 62 if(L <= m) 63 update( L, C, l, m, rt<<1 ); 64 else 65 update( L, C, m+1, r, rt<<1|1 ); 66 pushup(rt); 67 } 68 69 int query( int L, int R, int l, int r, int rt ) 70 { 71 if ( L <= l && R >= r ) 72 return sum[rt]; 73 74 int m = (l+r)>>1; 75 76 int Ans = 0; 77 if ( L <= m ) Ans += query( L, R, l, m, rt<<1 ); 78 if ( R > m ) Ans += query( L, R, m+1, r, rt<<1|1 ); 79 return Ans; 80 } 81 82 int main() 83 { 84 int T; cin >> T; 85 int cnt = 1; 86 while (T--) 87 { 88 init(); 89 scanf("%d", &N); 90 for ( int i = 1; i <= N; i++ ) 91 scanf( "%d", &num[i] ); 92 93 build( 1, N, 1 ); 94 printf( "Case %d:\n", cnt++ ); 95 96 int x, w; 97 while (1) 98 { 99 scanf( "%s", str ); 100 if ( str[0] == E ) 101 break; 102 103 scanf( "%d %d", &x, &w ); 104 if ( str[0] == A ) 105 update( x, w, 1, N, 1 ); 106 else if ( str[0] == S ) 107 update( x, -w, 1, N, 1 ); 108 else if ( str[0] == Q ) 109 printf( "%d\n", query(x,w,1,N,1) ); 110 } 111 } 112 return 0; 113 }
View Code

kuangbin帶我飛QAQ 線段樹