POJ 3468 A Simple Problem with Integers
来源:互联网 发布:nginx web服务器配置 编辑:程序博客网 时间:2024/06/10 23:59
区间修改,区间求和。用线段树维护区间和,区间修改打lazy标记。
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 111111;using namespace std;long long lazy[maxn<<2],sum[maxn<<2];void pushup(int rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void pushdown(int rt,int m){ if(lazy[rt]) { lazy[rt<<1] += lazy[rt]; lazy[rt<<1|1] += lazy[rt]; sum[rt<<1] += (m-(m>>1))*lazy[rt]; sum[rt<<1|1] += (m>>1)*lazy[rt]; lazy[rt] = 0; }}void build(int l ,int r,int rt){ lazy[rt] = 0; if(l == r) { scanf("%I64d",&sum[rt]); return; } int m = (r+l)>>1; build(lson); build(rson); pushup(rt);}void update(int L,int R,int c,int l ,int r ,int rt){ if(L <= l && r <= R) { lazy[rt] += c; sum[rt] += (long long)c*(r-l+1); return ; } pushdown(rt,r-l+1); int m = (r+l)>>1; if(L <= m) update(L,R,c,lson); if(R > m) update(L,R,c,rson); pushup(rt);}long long query(int L,int R,int l ,int r ,int rt){ if(L <= l && r <= R) { return sum[rt]; } pushdown(rt,r-l+1); long long res = 0; 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; scanf("%d%d",&n,&q); build(1,n,1); while(q--) { char op[2]; int a,b,c; scanf("%s",op); if(op[0] == 'Q') { scanf("%d%d",&a,&b); printf("%I64d\n",query(a,b,1,n,1)); } else if(op[0] == 'C') { scanf("%d%d%d",&a,&b,&c); update(a,b,c,1,n,1); } } return 0;}
0 0
- 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
- poj 3468 A Simple Problem with Integers
- POJ 3468 A Simple Problem with Integers
- poj 3468 A Simple Problem with Integers
- Linux IPC实践(11) --System V信号量(1)
- HDU 1698 Just a Hook
- 动态规划 求部分和
- 使MSHFlexGrid支持鼠标滚动
- 设计模式介绍:命令模式(command)
- POJ 3468 A Simple Problem with Integers
- Search Insert Position - Leetcode
- 设计模式介绍:模板方法(Template Method)模式
- ios 不使用控件设计app
- super关键字的使用
- 是时候开始学习 Ionic了
- 大家好。新来的。
- HDU 1342 Lotto 【DFS】
- Linux IPC实践(12) --System V信号量(2)