博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ-3468 A Simple Problem with Integers (区间求和,成段加减)
阅读量:7238 次
发布时间:2019-06-29

本文共 2417 字,大约阅读时间需要 8 分钟。

 

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.

The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

 

 

区间求和,成段加减

#include
#include
#include
using namespace std;#define lson l, m, rt<<1#define rson m+1, r, rt<<1|1typedef long long LL;const int N = 100000 + 5;LL T[N<<2],add[N<<2];void PushUP(int rt){ T[rt] = T[rt<<1] + T[rt<<1|1];}void PushDown(int rt, int m){ if(add[rt]){ add[rt<<1] += add[rt]; add[rt<<1|1] += add[rt]; T[rt<<1] += add[rt]*(LL)(m - (m >> 1)); T[rt<<1|1] += add[rt]*(LL)(m >> 1); add[rt] = 0; }}void Build(int l, int r, int rt){ add[rt] = 0; if(l == r){ scanf("%lld",&T[rt]); return ; } int m = (l + r) >> 1; Build(lson); Build(rson); PushUP(rt);}void Updata(int L,int R, int c,int l, int r, int rt){ if(L <= l && r <= R){ add[rt] += c; T[rt] += (LL)c*(r - l + 1); return ; } PushDown(rt, r - l + 1); int m = (l + r) >> 1; if(L <= m) Updata(L, R, c, lson); if(R > m) Updata(L, R, c, rson); PushUP(rt);}LL Query(int L, int R, int l, int r, int rt){ if(L <= l && r <= R) return T[rt]; PushDown(rt, r - l + 1); int m = (l + r) >> 1; LL ret = 0; if(L <= m) ret += Query(L, R, lson); if(R > m) ret += Query(L, R, rson); return ret;}int main(){ int n,m; while(scanf("%d %d",&n,&m)==2){ Build(1, n, 1); char ch[5]; int a,b,c; while(m--){ scanf("%s %d %d",ch,&a,&b); if(ch[0] == 'Q')printf("%lld\n",Query(a, b, 1, n, 1)); else{ scanf("%d",&c); Updata(a, b, c, 1, n, 1); } } } return 0;}

 

 

 

 

 

转载于:https://www.cnblogs.com/Pretty9/p/7384046.html

你可能感兴趣的文章
telnet用法 测试端口号
查看>>
SQL SERVER 表分区
查看>>
找出单链表的中间位置指针
查看>>
[IOS]《A Swift Tour》翻译(一)
查看>>
[Asp.net]说说密码框和只读框
查看>>
content-type收集
查看>>
使用easyui实现列表的批量删除
查看>>
研发费用调整利润
查看>>
11g RAC R2 之Linux DNS 配置
查看>>
Windows 7系统安装MySQL5.5.21图解
查看>>
《5天学会卡西欧fx-5800p之实操视频教程(初级)》目录和我的工作室现场曝光...
查看>>
浏览器桌面通知(notifications)
查看>>
让Sqlite脱离VC++ Runtime独立运行
查看>>
android自动打包方法(ant+proguard+签名)
查看>>
EXP的flashback_scn和flashback_time
查看>>
开机就提示“请安装TCP/IP协议,error=10106”的解决的方法
查看>>
【分享】谁偷走了我的大学
查看>>
MyBatis的Dao层注入SqlSession
查看>>
移动端重构系列12——popup
查看>>
Linux重启命令与如何重启网络?
查看>>