POJ3468 A Simple Problem with Integers
来源:互联网 发布:软件许可权合同 编辑:程序博客网 时间:2024/05/17 23:38
PS: 线段树区间操作,省赛热身。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100100;typedef long long LL;int N, Q;LL sumv[maxn*3], addv[maxn*3];int ql, qr;LL _sum;//Accepted4836K1750MSC++1949B2014-05-04 16:23:50void maintain(int o, int L, int R) { int lc = o*2, rc = o*2+1; if(L < R) { sumv[o] = sumv[lc] + sumv[rc]; } sumv[o] += (R-L+1)*addv[o]; //WA. if(L==R) { addv[o] = 0; }}void update(int o, int L, int R, int v) { if(ql<=L && qr >= R) { addv[o] += v; } else { int M = L + (R-L)/2; if(ql <= M) update(o*2, L, M, v); if(qr > M) update(o*2+1, M+1, R, v); } maintain(o, L, R);}void query(int o, int L, int R, LL add) { if(ql<=L && qr >= R) { _sum += sumv[o] + add*(R-L+1); } else { int M = L + (R-L)/2; if(ql <= M) query(o*2, L, M, add+addv[o]); if(qr > M) query(o*2+1, M+1, R, add+addv[o]); }}void init() { memset(sumv, 0, sizeof(sumv)); memset(addv, 0, sizeof(addv));}int main(){ // freopen("in", "r", stdin); int tmp, from, to; char str[5]; while(scanf("%d%d", &N, &Q)!=EOF) { init(); for(int i = 1; i <= N; i++) { scanf("%d", &tmp); ql = qr = i; update(1, 1, N, tmp); } getchar(); for(int i = 1; i <= Q; i++) { scanf("%s", str); int v = 0; if(str[0]=='Q') { scanf("%d%d", &from, &to); ql = from, qr = to; _sum = 0, v = 0; query(1, 1, N, v); printf("%lld\n", _sum); } else if(str[0]=='C') { int add; scanf("%d%d%d", &from, &to, &add); ql = from, qr = to; // WA. update(1, 1, N, add); } } } return 0;}
0 0
- poj3468 A Simple Problem with Integers
- POJ3468:A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468-A Simple Problem with Integers
- POJ3468--A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- [POJ3468]A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- POJ3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- poj3468 A Simple Problem with Integers
- c/c++/mfc/Linux c 关于文件的操作
- java.util.ConcurrentModificationException
- Mplayer安装教程(fedora9环境下)
- JavaScript 闭包 for循环
- 前端开发日记一:网页定位两三事
- POJ3468 A Simple Problem with Integers
- C++11使用lambda操作std::map
- 怎么找回清空的回收站删除文件呢
- sql语句 in和exists的区别
- ubuntu 13.04 搭建spcaview
- 开始写博客
- 实现session监听器
- 提取轮廓函数 cvFindContours ---OpenCV
- Sqlserver 导出insert插入语句