Description
Input
Output
Sample Input
Sample Output
解题思路:
线段树的模板题,必须熟练掌握build,update,query的使用,这题是动态查和,其实BIT也能过。顺手写个喽。
速度确实快了一些,这也就验证了树状数组能干的,线段树一定可以,只不过树状数组的编码量和速度都要比线段树快很多喽。
树状数组就是一路更新上去,一路求和下去,,,2333,怎么理解看你们了。
线段树代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<string> 4 5 using namespace std; 6 7 # define MAX 100005 8 # define lid (id<<1) 9 # define rid (id<<1|1) 10 11 12 int a[MAX]; 13 struct Segtree 14 { 15 int l,r; 16 int sum; 17 }tree[MAX*4]; 18 19 void push_up( int id ) 20 { 21 tree[id].sum = tree[lid].sum+tree[rid].sum; 22 } 23 24 25 void build ( int id,int l,int r ) 26 { 27 tree[id].l = l; tree[id].r = r; 28 if ( l==r ) 29 { 30 tree[id].sum = a[l]; 31 return; 32 } 33 int mid = (tree[id].l+tree[id].r)>>1; 34 build(lid,l,mid); 35 build(rid,mid+1,r); 36 push_up(id); 37 } 38 39 40 void update( int id, int x,int val ) 41 { 42 if ( tree[id].l == tree[id].r ) 43 { 44 tree[id].sum += val; 45 return; 46 } 47 int mid = ( tree[id].l+tree[id].r )>>1; 48 if ( x <= mid ) 49 update(lid,x,val); 50 else 51 update(rid,x,val); 52 push_up(id); 53 } 54 55 56 int query ( int id ,int l,int r ) 57 { 58 if ( tree[id].l==l&&tree[id].r==r ) 59 return tree[id].sum; 60 int mid = ( tree[id].l+tree[id].r )>>1; 61 if ( r <= mid ) 62 return query(lid,l,r); 63 if ( l > mid ) 64 return query(rid,l,r); 65 return query(lid,l,mid)+query(rid,mid+1,r); 66 67 } 68 69 int main(void) 70 { 71 int icase = 1; 72 int t;scanf("%d",&t); 73 while ( t-- ) 74 { 75 int n;scanf("%d",&n); 76 for ( int i = 1;i <= n;i++ ) 77 scanf("%d",&a[i]); 78 build(1,1,n); 79 string str; 80 printf("Case %d:\n",icase++); 81 while ( cin>>str ) 82 { 83 if ( str[0]=='Q' ) 84 { 85 int t1,t2;scanf("%d%d",&t1,&t2); 86 int ans = query(1,t1,t2); 87 printf("%d\n",ans); 88 } 89 else if ( str[0]=='A' ) 90 { 91 int t1,t2;scanf("%d%d",&t1,&t2); 92 update(1,t1,t2); 93 } 94 else if ( str[0]=='S' ) 95 { 96 int t1,t2;scanf("%d%d",&t1,&t2); 97 t2 = t2*(-1); 98 update(1,t1,t2); 99 } 100 else 101 break; 102 } 103 } 104 105 106 return 0; 107 }
树状数组代码:
1 # include<cstdio> 2 # include<iostream> 3 # include<cstring> 4 # include<string> 5 6 using namespace std; 7 8 # define MAX 1000004 9 10 int n; 11 int a[MAX]; 12 int tree[MAX]; 13 14 void update ( int x,int val ) 15 { 16 while ( x <= n ) 17 { 18 tree[x]+=val; 19 x+=x&(-x); 20 } 21 } 22 23 int query ( int x ) 24 { 25 int sum = 0; 26 while ( x ) 27 { 28 sum+=tree[x]; 29 x-=x&(-x); 30 } 31 return sum; 32 } 33 34 35 int main(void) 36 { 37 int icase = 1; 38 int t;scanf("%d",&t); 39 while ( t-- ) 40 { 41 scanf("%d",&n); 42 for ( int i = 1;i <= n;i++ ) 43 scanf("%d",&a[i]); 44 for ( int i = 1;i <= n;i++ ) 45 update(i,a[i]); 46 47 printf("Case %d:\n",icase++); 48 string str; 49 while ( cin>>str ) 50 { 51 if ( str[0]=='Q' ) 52 { 53 int t1,t2;scanf("%d%d",&t1,&t2); 54 int ans1 = query(t1-1), ans2 = query(t2); 55 printf("%d\n",ans2-ans1); 56 } 57 else if ( str[0]=='A' ) 58 { 59 int t1,t2;scanf("%d%d",&t1,&t2); 60 update(t1,t2); 61 } 62 else if ( str[0]=='S' ) 63 { 64 int t1,t2;scanf("%d%d",&t1,&t2); 65 t2 = t2*(-1); 66 update(t1,t2); 67 } 68 else 69 break; 70 } 71 memset(tree,0,sizeof(tree)); 72 memset(a,0,sizeof(a)); 73 74 } 75 76 77 78 return 0; 79 }