- #include <bits/stdc++.h>
- #define C 100003
- using namespace std;
- struct data{
- long long sum, prop;
- }Tree[C*3];
- void Update(int B, int E, int Node, int x, int y, int v){
- if(B >= x && E <= y){
- Tree[Node].sum += (long long)((E-B+1)*v);
- Tree[Node].prop += (long long)v;
- return;
- }
- if(B > E || B > y || E < x)return;
- int Mid = (B+E) >> 1, Left = Node << 1, Right = Left+1;
- Update(B, Mid, Left, x, y, v);
- Update(Mid+1, E, Right, x, y, v);
- Tree[Node].sum = Tree[Left].sum + Tree[Right].sum + (long long)(E-B+1)*Tree[Node].prop;
- }
- long long Query(int B, int E, int Node, int x, int y, long long Carry = 0LL){
- if(B >= x && E <= y)return Tree[Node].sum + (long long)(E-B+1)*Carry;
- if(B > E || B > y || E < x)return 0LL;
- int Mid = (B+E)>>1, Left = Node<<1, Right = Left+1;
- long long p = Query(B, Mid, Left, x, y, Carry+Tree[Node].prop);
- long long q = Query(Mid+1, E, Right, x, y, Carry+Tree[Node].prop);
- return p+q;
- }
- int main(){
- int T, N, Q, F, X, Y, V, CS = 0;
- scanf("%d", &T);
- while(T--){
- scanf("%d %d", &N, &Q);
- memset(Tree, 0, sizeof(Tree));
- printf("Case %d:\n", ++CS);
- for(int i = 1; i <= Q; i++){
- scanf("%d", &F);
- if(F == 0){
- scanf("%d %d %d", &X, &Y, &V);
- Update(0, N-1, 1, X, Y, V);
- }
- else{
- scanf("%d %d", &X, &Y);
- printf("%lld\n", Query(0, N-1, 1, X, Y));
- }
- }
- }
- return 0;
- }
Saturday, June 6, 2015
LightOJ - 1164 - Horrible Queries
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment