hud 1166
来源:互联网 发布:淘宝助理复制店铺 编辑:程序博客网 时间:2024/05/21 06:38
线段树做法
#include <iostream>
#include <stdio.h>#include <string.h>using namespace std;const int N=50005;int ans;struct node { int l,r,sum; int mid() { return (l+r)>>1 ; }}tree[N*4];void build(int l,int r,int rt){ tree[rt].l=l; tree[rt].r=r; if(l==r) { scanf("%d",&tree[rt].sum) ; return ; } int m=tree[rt].mid(); build(l,m,rt<<1 ); build(m+1,r,rt<<1|1); tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;}void query(int l,int r,int rt,int L,int R){ if(L<=l&&R>=r) { ans+=tree[rt].sum; return ; } int m=tree[rt].mid(); if(R<=m) query(l,m,rt<<1,L,R); else if(L>m) query(m+1,r,rt<<1|1,L,R); else { query(l,m,rt<<1,L,R); query(m+1,r,rt<<1|1,L,R); }} void updata(int l,int r,int rt,int pos,int add){ if(l==r) { tree[rt].sum+=add; return ; } int m=tree[rt].mid(); if(pos<=m) updata(l,m,rt<<1,pos,add); else updata(m+1,r,rt<<1|1,pos,add); tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;} int main(){ int t,n; int a,b; char str[10]; scanf("%d",&t); for(int i=1;i<=t;i++) { printf("Case %d:\n",i); scanf("%d",&n); build(1,n,1); while( scanf("%s",&str)) { if(str[0]=='E') break; scanf("%d%d",&a,&b); if(str[0]=='Q') { ans=0; query(1,n,1,a,b); printf("%d\n",ans); } else if (str[0]=='A') updata(1,n,1,a,b); else updata(1,n,1,a,-b); } } return 0;}
ZKW线段树
#include <iostream>#include <stdio.h>#include <memory.h>using namespace std;const int N=50005;int T[4*N];int M;int PushUp(int n){ T[n]=T[n<<1]+T[n<<1|1];}void build(int n){ for(M=1;M<=n+1;M<<=1); for(int i=M+1;i<=M+n;i++) scanf("%d",&T[i]); for(int i=M-1;i>0;i--) PushUp(i);}void add(int n,int v){ for(T[n+=M]+=v,n>>=1;n;n>>=1) PushUp(n);}int Query(int s,int t){ int ans=0; for(s=s+M-1,t=t+M+1;s^t^1;s>>=1,t>>=1) { if(~s&1) ans+=T[s^1]; if(t&1) ans+=T[t^1]; } return ans;}int main(){ char str[10]; int n,t,a,b,k=1; scanf("%d",&t); for(int i=1;i<=t;i++) { printf("Case %d:\n",i); memset(T,0,sizeof(T)); scanf("%d",&n); build(n); while(1) { scanf("%s",str); if(str[0]=='E') break; scanf("%d%d",&a,&b); if(str[0]=='A') add(a,b); else if(str[0]=='S') add(a,-b); else printf("%d\n",Query(a,b)); } }}
0 0
- hud 1166
- hud 1166 敌兵布阵
- hud 1166 敌兵布阵
- HUD
- HUD 1166 敌兵布阵 (线段树, 单点修改)
- HUD 1000
- HUD 1033
- HUD 4038
- HUD P1561
- HUD 4473
- HUD 4502
- Hud 4608
- hud 2073
- hud 1518
- HUD-1142
- Unity HUD
- UDK HUD
- hud 2795
- 蓝桥杯 2014本科C++ B组 地宫取宝 DFS+记忆化搜索
- 1009. Product of Polynomials (25)
- pat1050 String Subtraction
- C语言 数组排序 打乱 查找
- 移动平台WEB前端开发技巧汇总
- hud 1166
- 1011. World Cup Betting (20)
- Cocos2d-x 3.0final 终结者系列教程20 《2048》手游开发揭秘
- Swift-高级运算符
- 辛星浅析Redis中的主从复制
- Activiti 5.17 以文件形式上传的restful resource
- pat1054 The Dominant Color
- Cocos2d-x 3.0final 终结者系列教程16-《微信飞机大战》实现
- [leedcode oj 125]Valid Palindrome