首页 > 代码库 > BZOJ 3212 Pku3468 A Simple Problem with Integers
BZOJ 3212 Pku3468 A Simple Problem with Integers
题目大意:你拍一,我拍一,大家一起刷水题。
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; }
BZOJ 3212 Pku3468 A Simple Problem with Integers
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。