POJ 3468 A Simple Problem with Integers
2014-09-26 20:20
169 查看
区间更新区间求和。
题目:http://poj.org/problem?id=3468
题目:http://poj.org/problem?id=3468
#include <stdio.h> #include <iostream> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; typedef long long ll; const int maxn = 100005; ll tree[maxn<<2]; ll flag[maxn<<2]; void PushUp(int rt) { tree[rt] = tree[rt<<1] + tree[rt<<1|1]; } void PushDown(int rt,int m){ if(flag[rt]){ flag[rt<<1] += flag[rt]; flag[rt<<1|1] += flag[rt]; tree[rt<<1] += (m - (m>>1))*flag[rt]; tree[rt<<1|1] += (m>>1)*flag[rt]; flag[rt] = 0; } } void build(int l,int r,int rt){ if(l == r){ scanf("%lld",tree+rt); return; } int m =(l+r)>>1; build(lson); build(rson); PushUp(rt); } void update(int L,int R,int p,int l,int r,int rt){ if(L <= l && r <= R){ flag[rt] += p; tree[rt] += (ll)p*(r-l+1); return; } PushDown(rt, r-l+1); int m = (l+r)>>1; if(L <= m) update(L, R, p, lson); if(R > m) update(L, R, p, rson); PushUp(rt); } ll query(int L,int R,int l,int r,int rt){ if(L <= l && r <= R) return tree[rt]; ll res = 0; PushDown(rt, r-l+1); int m = (l+r)>>1; if(L <= m) res += query(L, R, lson); if(R > m) res += query(L, R, rson); return res; } int main(){ int n,q; while(~scanf("%d%d",&n,&q)){ build(1, n, 1); while(q--){ char ch[2]; int x,y,z; scanf("%s",ch); if(ch[0] == 'Q'){ scanf("%d%d",&x,&y); printf("%lld\n",query(x, y, 1, n, 1)); } else{ scanf("%d%d%d",&x,&y,&z); update(x, y, z, 1,n,1); } // for(int i = 1;i < n*4;i++) // printf("%lld ",tree[i]); // printf("\n"); } } return 0; }
相关文章推荐
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- POJ 2155 Matrix 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers(线段树区间更新+求和)
- POJ - 3468 - A Simple Problem with Integers - 线段树Lazy大法
- [POJ 3468] A Simple Problem with Integers Splay树
- 【POJ 3468】A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers(区间更新)
- 线段树_poj_3468_A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers 插线问线
- 线段树 POJ 3468 A Simple Problem with Integers 线段树 成段更新入门
- POJ 3468: A Simple Problem with Integers
- POJ - 3468 A Simple Problem with Integers(线段树成段更新,查询区间和)
- poj 3468 A Simple Problem with Integers(线段树,区间更新)
- POJ 3468 A Simple Problem with Integers (线段树 区间增减 区间求和)
- poj 3468 A Simple Problem with Integers(线段树——区间更新)
- POJ 3468 A Simple Problem with Integers 【树状数组】
- poj-3468 A Simple Problem with Integers-线段树-整段区间增加求和
- POJ 3468 A Simple Problem with Integers