HDU 1166
来源:互联网 发布:微信小店数据推送 编辑:程序博客网 时间:2024/05/16 09:41
http://acm.hdu.edu.cn/showproblem.php?pid=1166
简单的线段树单点更新,查询区间和
/*#include <stdio.h>#include <algorithm>#include <string.h>#include <iostream>#define maxs 1010101#define MME(i,j) memset(i,j,sizeof(i))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define scan(a) scanf("%d",&a)using namespace std;int sum[maxs];void pushplus(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt){ if(l==r) { scan(sum[rt]); return; } int m=(l+r)>>1; build(lson); build(rson); pushplus(rt);}void updata(int q,int add,int l,int r,int rt){ if(l==r) { sum[rt]+=add; return; } int m=(l+r)>>1; if(q<=m) updata(q,add,lson); else updata(q,add,rson); pushplus(rt);}int query(int L,int R,int l,int r,int rt){ if(L<=l and r<=R) { return sum[rt]; } int m=(l+r)>>1,ret=0; if(L<=m) ret+=query(L,R,lson); if(R>m) ret+=query(L,R,rson); return ret;}char s[200];int main(){ int t; scanf("%d",&t); while(t--) { static int times=1; printf("Case %d:\n",times++); int n; scanf("%d",&n); build(1,n,1); int a,b; while(scanf("%s",s) and strcmp(s,"End")!=0) { if(strcmp(s,"Query")==0) { scanf("%d %d",&a,&b); printf("%d\n",query(a,b,1,n,1)); } else if(strcmp(s,"Add")==0) { scanf("%d %d",&a,&b); updata(a,b,1,n,1); } else if(strcmp(s,"Sub")==0) { scanf("%d %d",&a,&b); updata(a,-b,1,n,1); } } } return 0;}*/#include <stdio.h>#include <algorithm>#include <string.h>#include <iostream>#define maxs 1010101#define MME(i,j) memset(i,j,sizeof(i))#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define scan(a) scanf("%d",&a)using namespace std;int sum[maxs];void push_up(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int l,int r,int rt){ if(l == r) { scanf("%d",&sum[rt]); return; } int m = (l+r)>>1; build(lson); build(rson); push_up(rt);}int query(int al,int ar,int l,int r,int rt){ if(al<=l and ar>=r) { return sum[rt]; } int ret=0,m=(l+r)>>1; if(al<=m) ret+=query(al,ar,lson); if(ar>m) ret+=query(al,ar,rson); return ret;}void Update(int p, int data,int l,int r,int rt){ if(l==r) // 单点更新时l==r { sum[rt]+=data; return; } int m=(l+r)>>1; if(p<=m) Update(p,data,lson); if(p>m) Update(p,data,rson); push_up(rt);}char s[200];int main(){ int t; scanf("%d",&t); while(t--) { static int times=1; printf("Case %d:\n",times++); int n; scanf("%d",&n); build(1,n,1); int a,b; while(scanf("%s",s) and strcmp(s,"End")!=0) { if(strcmp(s,"Query")==0) { scanf("%d %d",&a,&b); printf("%d\n",query(a,b,1,n,1)); } else if(strcmp(s,"Add")==0) { scanf("%d %d",&a,&b); Update(a,b,1,n,1); } else if(strcmp(s,"Sub")==0) { scanf("%d %d",&a,&b); Update(a,-b,1,n,1); } } } return 0;}
0 0
- hdu 1166
- hdu 1166
- hdu 1166
- hdu 1166
- HDU 1166
- HDU 1166
- hdu 1166 ~
- hdu-1166
- hdu 1166
- hdu 1166
- HDU 1166
- hdu 1166
- hdu 1166
- HDU 1166
- HDU 1166
- hdu 1166
- hdu 1166
- hdu 1166
- Mac下的多线程
- 2017婚假国家新规定
- 婚姻是一场精神上的门当户对
- laravel5.1 源码阅读
- 蓝桥杯-第七届省赛javaA组- 分小组
- HDU 1166
- ContentProvider内容提供者的使用
- contentprovider与contentresolver
- LVS配置命令理解2
- h5做app和原生app有什么区别?
- Java并发编程系列(三)----Lock
- LeetCode 387. First Unique Character in a String
- PAT甲级.1025. PAT Ranking (25)
- 计算机网络知识总结