poj 3468 线段树 成段更新
来源:互联网 发布:奥瑞那消防主机编程 编辑:程序博客网 时间:2024/04/30 12:57
题意:
给出n个数,m中操作!!区域求和已经区域更新!
解法:
线段树!!
#include<iostream>#include<cstdio>#include<algorithm>using namespace std;const int maxn=100010;#define ll long longll sum[maxn<<2],col[maxn<<2],s[maxn];#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,rvoid pushUP(int rt){ sum[rt]=sum[rt<<1]+sum[rt<<1|1];}void build(int rt,int l,int r){ col[rt]=0; if(l==r) { sum[rt]=s[l]; return; } int mid=(l+r)>>1; build(lson); build(rson); pushUP(rt);}void pushDOWN(int rt,int m){ if(col[rt]) { col[rt<<1]+=col[rt]; col[rt<<1|1]+=col[rt]; sum[rt<<1]+=col[rt]*(m-(m>>1)); sum[rt<<1|1]+=col[rt]*(m>>1); col[rt]=0; // pushUP(rt); }}void updata(int rt,int l,int r,int L,int R,int c){ if(L<=l&&r<=R) { col[rt]+=c; sum[rt]+=(r-l+1)*(ll)c; return; } pushDOWN(rt,r-l+1);//更新到整个区域里 int mid=(l+r)>>1; if(mid>=L) updata(lson,L,R,c); if(mid<R) updata(rson,L,R,c); pushUP(rt);}ll query(int rt,int l,int r,int L,int R){ if(L<=l&&r<=R) { return sum[rt]; } pushDOWN(rt,r-l+1);//一开始没有在这里pushDown 导致WA一次 //更新到整个区间上
int mid=(l+r)>>1; __int64 res=0; if(mid>=L) res+=query(lson,L,R); if(mid<R) res+=query(rson,L,R); return res;}int main(){ int i,j,a,b,c; int n,m; char op[2]; scanf("%d%d",&n,&m); for(i=1; i<=n; i++)scanf("%lld",&s[i]);//在这里没有定义成 ll 形式的WA了一次 build(1,1,n); while(m--) { scanf("%s",op);if (op[0] == 'Q') {scanf("%d%d",&a,&b);printf("%lld\n",query(1,1,n,a,b));} else {scanf("%d%d%d",&a,&b,&c);updata(1,1,n,a,b,c);} } return 0;}
- poj 3468 线段树 成段更新
- poj 3468 线段树--成段更新
- poj 3468线段树 成段更新
- POJ 3468 【线段树区间更新-成段更新】
- poj 2528 线段树--成段更新
- 线段树 POJ 3468 A Simple Problem with Integers 线段树 成段更新入门
- poj 3468 线段树 区间内的线段和(成段更新)
- POJ 3667 Hotel 线段树 区间合并(成段更新)
- POJ 2352 Stars 线段树 单点更新 成段求和
- POJ 3225 Help with Intervals 线段树 成段更新
- POJ 2777 Count Color 线段树 成段更新
- POJ 3667(线段树,区间合并,成段更新)
- POJ 2528 Mayor posters 线段树 成段更新
- POJ 3225 Help with Intervals 线段树 成段更新
- POJ 1436 Horizontally Visible Segments 线段树 成段更新
- poj 2155 (二维线段树的成段更新)
- poj 2991 线段树 成段更新 + 向量旋转
- 线段树 成段更新
- 英语的重要性
- zoj1292--------Integer Inquiry 大数
- 杂记3
- 一个男人关心的东西决定了他的层次
- 香港金融中心谁与争锋
- poj 3468 线段树 成段更新
- AlarmManager 定时
- 佛感动了
- C++虚继承
- 手机图标收集总结
- 明天的事
- 用event 10013验证实例恢复的终点?
- JavaScript与.NET应用程序交互_实验1
- Firebird的基本数据类型