标签:研究 直线 超过 int scanf win lan set div
题目链接:https://vjudge.net/problem/HDU-1166
题目:
1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 EndSample Output
Case 1: 6 33 59
思路:线段树的单点修改
// // Created by HJYL on 2019/9/4. // #include <algorithm> #include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <set> #include<math.h> #include<map> using namespace std; const int maxn=1e6+7; int input[maxn]; struct Node{ int left; int right; int num; }tree[maxn*4]; void PushUp(int root) { tree[root].num=tree[root<<1].num+tree[root<<1|1].num; } void Build(int root,int l,int r) { tree[root].left=l; tree[root].right=r; if(l==r) { tree[root].num=input[l]; return; } int mid=(l+r)>>1; Build(root<<1,l,mid); Build(root<<1|1,mid+1,r); PushUp(root); } void change(int root,int dis,int k) { if(tree[root].left==tree[root].right) { tree[root].num+=k;return; } if(dis<=tree[root<<1].right) change(root<<1,dis,k); else change(root<<1|1,dis,k); PushUp(root); } int ans; void search(int root,int l,int r) { if(l<=tree[root].left&&tree[root].right<=r) { ans+=tree[root].num; return; } if(l<=tree[root<<1].right) search(root<<1,l,r); if(r>=tree[root<<1|1].left) search(root<<1|1,l,r); } int main() { int T; scanf("%d",&T); // cout<<"T="<<T<<endl; int kase=0; while(T--) { printf("Case %d:\n",++kase); int n; scanf("%d",&n); // cout<<"n="<<n<<endl; for(int i=1;i<=n;i++) { scanf("%d",&input[i]); //cout<<"input[i]="<<input[i]<<" "; } //cout<<"\n"; Build(1,1,n); char str[maxn]; while(~scanf("%s",str)) { //cout<<"s="<<str<<endl; if(strcmp(str,"End")==0) break; if(strcmp(str,"Query")==0) { int l,r; scanf("%d%d",&l,&r); //cout<<"l="<<l<<"r="<<r<<endl; ans=0; search(1,l,r); // cout<<"ans="; printf("%d\n",ans); } else if(strcmp(str,"Add")==0) { int a,b; scanf("%d%d",&a,&b); // cout<<"a="<<a<<"b="<<b<<endl; change(1,a,b); } else if(strcmp(str,"Sub")==0) { int p,q; scanf("%d%d",&p,&q); //cout<<"p="<<p<<"q="<<q<<endl; change(1,p,-q); } } } return 0; }
标签:研究 直线 超过 int scanf win lan set div
原文地址:https://www.cnblogs.com/Vampire6/p/11462471.html