BZOJ 3212 Pku3468 A Simple Problem with Integers
来源:互联网 发布:cocos2d x游戏源码 编辑:程序博客网 时间:2024/05/16 15:17
题目大意:你拍一,我拍一,大家一起刷水题。
CODE:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAX 100010#define LEFT (pos << 1)#define RIGHT (pos << 1|1)#define CNT (r - l + 1)using namespace std;struct SegTree{long long sum,c;}tree[MAX << 2];int cnt,asks;int src[MAX];char c[10];void BuildTree(int l,int r,int pos){if(l > r)return ;if(l == r) {tree[pos].sum = src[l];return ;}int mid = (l + r) >> 1;BuildTree(l,mid,LEFT);BuildTree(mid + 1,r,RIGHT);tree[pos].sum = tree[LEFT].sum + tree[RIGHT].sum;}inline void PushDown(int pos,int cnt){if(tree[pos].c) {tree[LEFT].c += tree[pos].c;tree[RIGHT].c += tree[pos].c;tree[LEFT].sum += tree[pos].c * (cnt - (cnt >> 1));tree[RIGHT].sum += tree[pos].c * (cnt >> 1);tree[pos].c = 0;}}void Modify(int l,int r,int x,int y,int pos,long long c){if(l == x && y == r) {tree[pos].sum += CNT * c;tree[pos].c += c;return ;}PushDown(pos,CNT);int mid = (l + r) >> 1;if(y <= mid)Modify(l,mid,x,y,LEFT,c);else if(x > mid)Modify(mid + 1,r,x,y,RIGHT,c);else {Modify(l,mid,x,mid,LEFT,c);Modify(mid + 1,r,mid + 1,y,RIGHT,c);}tree[pos].sum = tree[LEFT].sum + tree[RIGHT].sum;}long long Ask(int l,int r,int x,int y,int pos){if(l == x && y == r)return tree[pos].sum;PushDown(pos,CNT);int mid = (l + r) >> 1;if(y <= mid)return Ask(l,mid,x,y,LEFT);if(x > mid)return Ask(mid + 1,r,x,y,RIGHT);long long left = Ask(l,mid,x,mid,LEFT);long long right = Ask(mid + 1,r,mid + 1,y,RIGHT);return left + right;}int main(){cin >> cnt >> asks;for(int i = 1; i <= cnt; ++i)scanf("%d",&src[i]);BuildTree(1,cnt,1);for(int x,y,i = 1; i <= asks; ++i) {scanf("%s",c);if(c[0] == 'Q') {scanf("%d%d",&x,&y);printf("%lld\n",Ask(1,cnt,x,y,1));}else {long long z;scanf("%d%d%lld",&x,&y,&z);Modify(1,cnt,x,y,1,z);}}return 0;}
0 0
- BZOJ 3212 Pku3468 A Simple Problem with Integers
- bzoj 3212: Pku3468 A Simple Problem with Integers 线段树
- BZOJ 3212 Pku3468 A Simple Problem with Integers 线段树
- bzoj 3212: Pku3468 A Simple Problem with Integers
- 3212: Pku3468 A Simple Problem with Integers
- BZOJ P3212 Pku3468 A Simple Problem with Integers
- pku3468: A Simple Problem with Integers
- [BZOJ3212]Pku3468 A Simple Problem with Integers
- [BZOJ3212] Pku3468 A Simple Problem with Integers
- bzoj3212 Pku3468 A Simple Problem with Integers
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
- bzoj 3212: Pku3468 A Simple Problem with Integers (线段树)
- PKU3468(A Simple Problem with Integers)线段树的成段更新
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- A Simple Problem with Integers
- C#总结
- php-memcached内存缓存的使用
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128
- 加速网页的方式
- [Perforce]password (P4PASSWD) invalid or unset. 的错误解决
- BZOJ 3212 Pku3468 A Simple Problem with Integers
- 微信连WiFi已OUT?
- ios数据持久化之数据库(SQLite)
- Cocos2d-x --Action (动作)
- C#绘图
- 在Android中使用WindowManager实现悬浮窗口
- 1-6q环视之采集,模拟摄像头,模拟电视
- ORACLE中的锁(oracle lock mode)
- 安装zookeeper集群