hdu1166 敌兵布阵 线段树
TLE求错误啊啊啊啊啊啊啊
#include <iostream> #include <cstdio> #include <fstream> #include <cmath> #include <cstring> using namespace std; #define MAXN 50010 int ai[MAXN]; int N; int T; struct node { int left; int right; int sum; }Tree[MAXN<<2]; void PushUp(int root) { Tree[root].sum = Tree[root<<1].sum + Tree[(root<<1)+1].sum; } void Build(int start,int end,int root) { Tree[root].left = start; Tree[root].right = end; int mid = (start + end)>>1; if(start == end) { Tree[root].sum = ai[start]; return; } Tree[root].left = start; Tree[root].right = end; Build(start,mid,root*2); Build(mid+1,end,root*2+1); PushUp(root); //Tree[root].sum = Tree[root<<1].sum + Tree[(root<<1)+1].sum; } int Query(int start,int end,int root) { if(start > Tree[root].right || end < Tree[root].left) return 0; if(start<= Tree[root].left && Tree[root].right <= end) return Tree[root].sum; int mid = (start + end)>>1; int a = Query(start,end,root<<1); int b = Query(start,end,(root<<1)+1); return a+b; } void Update(int root,int pos,int value,int start,int end,int type) { if(start == end) { if(type == 1) Tree[root].sum += value; if(type == 2) Tree[root].sum -= value; return; } int mid = (start + end)>>1; if(pos <= mid) Update(root<<1,pos,value,start,mid,type); else Update((root<<1)+1,pos,value,mid+1,end,type); PushUp(root); } int main() { freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); scanf("%d",&T); int count = 0; while(T--) { memset(ai,0,sizeof(ai)); memset(Tree,0,sizeof(Tree)); count++; scanf("%d",&N); for(int i = 1; i <= N; i++) { scanf("%d",&ai[i]); } Build(1,N,1); cout<<"Case "<<count<<":"<<endl; char oper[11]; int i,j; while(scanf("%s",oper)) { if(strcmp(oper,"End")==0) break; cin>>i>>j; if(strcmp(oper,"Query")==0) cout<<Query(i,j,1)<<endl; if(strcmp(oper,"Add")==0) { ai[i] += j; Update(1,i,j,1,N,1); } if(strcmp(oper,"Sub")==0) { ai[i] -= j; Update(1,i,j,1,N,2); } } } return 0; }