「BZOJ3212」Pku3468 A Simple Problem with Integers
Description
You have N integers, A1, A2, … , 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 A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4
Sample Output
4
55
9
15
55
9
15
HINT
The sums may exceed the range of 32-bit integers.
题解
蛋碎一地。。。
原来最下面写的是可能超过32位。。。
原来以为是不会超过
话说bzoj为什么会有这种水题。。
维护线段树,区间加,区间求和。。很好玩么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
#include<iostream> #include<cstdio> #define N 100001 #define ll long long using namespace std; int n,m; struct data{int l,r;ll s,add;}t[N<<2]; void pushup(int k) {t[k].s=t[k<<1].s+t[k<<1|1].s;} void pushdown(int k) { if(t[k].l==t[k].r)return; if(t[k].add) { t[k<<1].add+=t[k].add; t[k<<1|1].add+=t[k].add; t[k<<1].s+=t[k].add*(t[k<<1].r-t[k<<1].l+1); t[k<<1|1].s+=t[k].add*(t[k<<1|1].r-t[k<<1|1].l+1); t[k].add=0; } } void build(int k,int l,int r) { t[k].l=l;t[k].r=r; if(l==r){scanf("%lld",&t[k].s);return;} int mid=(l+r)>>1; build(k<<1,l,mid); build(k<<1|1,mid+1,r); pushup(k); } void add(int k,int x,int y,int v) { pushdown(k); int l=t[k].l,r=t[k].r,sum=r-l+1; if(l==x&&r==y){t[k].add+=v;t[k].s+=(ll)sum*v;return;} int mid=(l+r)>>1; if(mid>=y)add(k<<1,x,y,v); else if(mid<x)add(k<<1|1,x,y,v); else {add(k<<1,x,mid,v);add(k<<1|1,mid+1,y,v);} pushup(k); } ll que(int k,int x,int y) { pushdown(k); int l=t[k].l,r=t[k].r; if(l==x&&r==y)return t[k].s; int mid=(l+r)>>1; if(mid>=y)return que(k<<1,x,y); else if(mid<x)return que(k<<1|1,x,y); else {return que(k<<1,x,mid)+que(k<<1|1,mid+1,y);} } int main() { scanf("%d%d",&n,&m); build(1,1,n); for(int i=1;i<=m;i++) { int x,y,z; char ch[10];scanf("%s",ch); if(ch[0]=='Q') { scanf("%d%d",&x,&y); printf("%lld\n",que(1,x,y)); } else { scanf("%d%d%d",&x,&y,&z); add(1,x,y,z); } } return 0; } |
这种傻逼题不是直接暴力就可以过了么
可以么2333
暴力BIT啊