【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵
2015-09-28 23:00
330 查看
hdu1166 敌兵布阵,单点修改,区间求和。
【ATTENTION】MAXN要开成节点数的4倍,开得不够会提示TLE。
【ATTENTION】MAXN要开成节点数的4倍,开得不够会提示TLE。
#include<iostream> #include<cstdio> #include<cstring> #define lson l,m,root<<1 #define rson m+1,r,root<<1|1 using namespace std; const int MAXN=50000*4+500; int n; int sum[MAXN]; void pushUP(int root) { sum[root]=sum[root<<1]+sum[root<<1|1]; } void build(int l,int r,int root) { if (l==r) { scanf("%d",&sum[root]); return; } int m=(l+r)>>1; build(lson); build(rson); pushUP(root); } void update(int p,int delta,int l,int r,int root) { if (l==r) { sum[root]+=delta; return; } int m=(l+r)>>1; if (p<=m) update(p,delta,lson); if (p>m) update(p,delta,rson); pushUP(root); } int query(int L,int R,int l,int r,int root) { int result=0; if (l>=L && r<=R) { return sum[root]; } int m=(l+r)>>1; if (L<=m) result+=query(L,R,lson); if (R>m) result+=query(L,R,rson); return result; } int main() { int t; scanf("%d",&t); for (int kase=0;kase<t;kase++) { cout<<"Case "<<kase+1<<":"<<endl; scanf("%d",&n); build(1,n,1); char s[6]; while (scanf("%s",s)) { if (s[0]=='E') break; int a,b; scanf("%d%d",&a,&b); if (s[0]=='A') update(a,b,1,n,1); if (s[0]=='S') update(a,-b,1,n,1); if (s[0]=='Q') cout<<query(a,b,1,n,1)<<endl; } } return 0; }
相关文章推荐
- Github README.md 添加图片
- Github README.md 添加图片
- Github README.md 添加图片
- Block传值和传值之间的注意事项
- java中wait和sleep的区别
- 【UIKit-110-5】#import <UIKit/UITableView.h>选中行
- Quartz2D之生成圆形头像、打水印、截图三种方法的封装
- node nodemailer
- 二叉树中和为某一值的路径(剑指offer+二叉树+递归)
- 【C语言】接收字符,大小写字符相互转换后输出,数字不输出
- SSH框架
- hdu 5489 Removed Interval 2015合肥网络赛 树状数组 dp 离散化/dp
- 创业的第四十八天
- Js 图片上传本地预览
- php乱码的解决方法
- 【转】MySQL my.cnf 参数 详解
- CentOS7下安装MySql5.6.26
- Fresco前传(1):一句话搞定图片显示(同时还可以满足各种小需求哦)
- 【第2节】Java数组(1)
- hdu5493Queue 树状数组+二分