POJ-3468-A Simple Problem with Integers
来源:互联网 发布:高清屏幕软件 编辑:程序博客网 时间:2024/03/29 20:53
线段树区间更新,区间求和
#include <iostream> #include <cstdio> #define maxn 100005#define INF 1e9using namespace std;typedef long long ll;ll sum[maxn<<2], add[maxn<<2];void Build(int n, int l, int r){ add[n] = 0; if(l == r){ scanf("%lld ", sum+n); return ; } int mid = (l + r) >> 1; Build(n<<1, l, mid); Build(n<<1|1, mid+1, r); sum[n] = sum[n<<1] + sum[n<<1|1];} void Pushdown(int n, int m){ if(add[n]){ sum[n<<1] += (m - (m >> 1)) * add[n]; sum[n<<1|1] += (m >> 1) * add[n]; add[n<<1] += add[n]; add[n<<1|1] += add[n]; add[n] = 0; }}void Update(int n, int L, int R, int l, int r, int d){ if(l == L && R == r){ add[n] += d; sum[n] += (r - l + 1) * d; return ; } Pushdown(n, R - L + 1); int mid = (L + R) >> 1; if(r <= mid) Update(n<<1, L, mid, l, r, d); else if(l > mid) Update(n<<1|1, mid+1, R, l, r, d); else{ Update(n<<1, L, mid, l, mid, d); Update(n<<1|1, mid+1, R, mid+1, r, d); } sum[n] = sum[n<<1] + sum[n<<1|1];}void Query(int n, int L, int R, int l, int r, ll &s){ if(L == l && R == r){ s += sum[n]; return ; } Pushdown(n, R - L + 1); int mid = (L + R) >> 1; if(r <= mid) Query(n<<1, L, mid, l, r, s); else if(l > mid) Query(n<<1|1, mid+1, R, l, r, s); else{ Query(n<<1, L, mid, l, mid, s); Query(n<<1|1, mid+1, R, mid+1, r, s); }}int main(){// freopen("in.txt", "r", stdin); int n, q; while(scanf("%d%d ", &n, &q) == 2){ char ch; int a, b, c; Build(1, 1, n); for(int i = 0; i < q; i++){ scanf("%c", &ch); if(ch == 'Q'){ ll s = 0; scanf("%d%d ", &a, &b); Query(1, 1, n, a, b, s); printf("%lld\n", s); } else{ scanf("%d%d%d ", &a, &b, &c); Update(1, 1, n, a, b, c); } } } 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
- centos7 安装 nginx
- 【饥荒mod制作吧第四篇】mod制作教程系列
- 工作笔记(16-04-28)
- 大数据管理:数据集成的技术、方法与最佳实践 读书笔记三
- hdu1300 Pearls 【dp】
- POJ-3468-A Simple Problem with Integers
- Allegro 中Assembly_Top 、Silkscreen_Top、Autosilk_Top 的区别
- INSTALL_FAILED_SHARED_USER_INCOMPATIBLE的问题解决
- Python web —— webbrowser + feedparser 网络爬虫刷博器
- 使用ThinkPHP框架快速开发网站(多图)
- Android手机开启开发者模式
- 使用springMVC实现文件上传和下载之文件下载
- Carthage使用记录
- mysql中IN,OR,BETWEEN性能比较