萌新刚学OI,救救孩子吧QAQ

P3374 【模板】树状数组 1

@[由比滨丶雪乃](/space/show?uid=35531) 树状数组多好呀,码量又小,又不容易打错。
by Wiene @ 2019-08-03 23:25:14


干嘛要打懒标记呢 ```cpp #include<iostream> #include<cstdio> using namespace std; struct Node{ int l,r,value; Node *left,*right; Node(int t,int p,int v,Node *a,Node *b){ l=t; r=p; value=v; left=a; right=b; } }*root; Node *build(int l,int r){ if(l==r)return new Node(l,r,0,0,0); int mid=(l+r)>>1; Node *left=build(l,mid),*right=build(mid+1,r); return new Node(l,r,0,left,right); } inline int sum(int l,int r,Node *cur){ if(l==cur->l&&r==cur->r)return cur->value; int mid=(cur->l+cur->r)>>1; if(l>mid)return sum(l,r,cur->right); if(r<=mid)return sum(l,r,cur->left); return sum(l,mid,cur->left)+sum(mid+1,r,cur->right); } inline void modify(int x,int v,Node *cur){ if(cur->l==cur->r){ cur->value+=v; return ; } int mid=(cur->l+cur->r)>>1; if(x>mid)modify(x,v,cur->right); if(x<=mid)modify(x,v,cur->left); cur->value=cur->left->value+cur->right->value; } inline int read(){ register int x=0,ch=getchar(),v=1; while(!isdigit(ch)){if(ch=='-')v=-1;ch=getchar();} while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();}; return v*x; } int n,k; int opt,l,r; int main(){ n=read(),k=read(); root=build(1,n); for(int i=1;i<=n;++i){ opt=read(); modify(i,opt,root); } while(k--){ opt=read(),l=read(),r=read(); if(opt==1){ modify(l,r,root); }else{ printf("%d\n",sum(l,r,root)); } } return 0; } ```
by Lates @ 2019-08-03 23:26:54


@[AC鸭](/space/show?uid=116689) +1
by Lates @ 2019-08-03 23:27:06


好吧(~~只是来试试打的线段树板子~~)qwq
by 由比滨丶雪乃 @ 2019-08-03 23:28:30


您longlong的常数可是十分优秀啊...几乎慢了一倍...建议改成int~~这样就能得到80分的好成绩~~
by Maxliu @ 2019-08-03 23:28:41


用树状数组A # 此贴终结
by 由比滨丶雪乃 @ 2019-08-03 23:28:51


@[由比滨丶雪乃](/space/show?uid=35531) 树状数组的常数比线段树要小得多QAQ
by Wiene @ 2019-08-03 23:31:36


@[Lates](/space/show?uid=119062) 卡常奆佬Orz
by Del_Your_Heart @ 2019-08-04 06:25:27


@[由比滨丶雪乃](/space/show?uid=35531) ~~分块啊,搞什么搞基数据结构~~
by Timing @ 2019-08-04 07:36:14


|