码迷,mamicode.com
首页 > 其他好文 > 详细

1164 - Horrible Queries

时间:2016-05-16 01:58:01      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

1164 - Horrible Queries
Time Limit: 2 second(s) Memory Limit: 64 MB

World is getting more evil and it‘s getting tougher to get into the Evil League of Evil. Since the legendary Bad Horse has retired, now you have to correctly answer the evil questions of Dr. Horrible, who has a PhD in horribleness (but not in Computer Science). You are given an array of n elements, which are initially all 0. After that you will be given q commands. They are -

  1. 0 x y v - you have to add v to all numbers in the range of x to y (inclusive), where x and y are two indexes of the array.
  2. 1 x y - output a line containing a single integer which is the sum of all the array elements between x and y (inclusive).

The array is indexed from 0 to n - 1.

Input

Input starts with an integer T (≤ 5), denoting the number of test cases.

Each case contains two integers n (1 ≤ n ≤ 105) and q (1 ≤ q ≤ 50000). Each of the next q lines contains a task in one of the following form:

0 x y v (0 ≤ x ≤ y < n, 1 ≤ v ≤ 1000)

1 x y (0 ≤ x ≤ y < n)

Output

For each case, print the case number first. Then for each query ‘1 x y‘, print the sum of all the array elements between x and y.

Sample Input

Output for Sample Input

2

10 5

0 0 9 10

1 1 6

0 3 7 2

0 4 5 1

1 5 5

20 3

0 10 12 1

1 11 12

1 19 19

Case 1:

60

13

Case 2:

2

0

Note

Dataset is huge. Use faster i/o methods.


PROBLEM SETTER: IQRAM MAHMUD
SPECIAL THANKS: JANE ALAM JAN (DATASET, SOLUTION)
思路:线段树区间更新
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<queue>
  6 #include<stack>
  7 #include<map>
  8 #include<math.h>
  9 using namespace std;
 10 typedef long long LL;
 11 LL tree[6*100005];
 12 LL cnt[6*100005];
 13 LL aa[100005];
 14 void down(int k);
 15 void up(int k,int v);
 16 LL  ask(int l,int r,int k,int n,int m);
 17 void  in(int l,int r,int k,int n,int m,int u);
 18 int main(void)
 19 {
 20     int i,j,k;
 21     scanf("%d",&k);
 22     int s;
 23     int n,m;
 24     for(s=1; s<=k; s++)
 25     {
 26         scanf("%d %d",&n,&m);
 27         memset(tree,0,sizeof(tree));
 28         memset(cnt,0,sizeof(cnt));
 29         int flag=0;
 30         while(m--)
 31         {
 32             int x,y;
 33             int xx,yy,zz;
 34             scanf("%d",&x);
 35             if(x==0)
 36             {
 37                 scanf("%d %d %d",&xx,&yy,&zz);
 38                 in(xx,yy,0,0,n-1,zz);
 39             }
 40             else
 41             {
 42                 scanf("%d %d",&xx,&yy);
 43                 LL as=ask(xx,yy,0,0,n-1);
 44                 aa[flag++]=as;
 45             }
 46 
 47         } printf("Case %d:\n",s);
 48             for(i=0;i<flag;i++)
 49                 printf("%lld\n",aa[i]);
 50     }
 51 }
 52 void down(int k)
 53 {
 54     cnt[2*k+1]+=cnt[k];
 55     cnt[2*k+2]+=cnt[k];
 56     cnt[k]=0;
 57 }
 58 void up(int k,int v)
 59 {
 60     int cc=k;
 61     tree[cc]+=cnt[k]*v;
 62     if(cc==0)return ;
 63     while(cc>=0)
 64     {
 65         cc=(cc-1)/2;
 66         tree[cc]=tree[2*cc+1]+tree[2*cc+2];
 67         if(cc==0)
 68             return ;
 69     }
 70 }
 71 LL  ask(int l,int r,int k,int n,int m)
 72 {
 73     if(l>m||r<n)
 74     {  if(cnt[k]>0)
 75         {
 76              up(k,m-n+1);
 77             down(k);
 78         }
 79         return 0;
 80     }
 81     else if(l<=n&&r>=m)
 82     {
 83         if(cnt[k]>0)
 84         {
 85             up(k,m-n+1);
 86             down(k);
 87             return tree[k];
 88         }
 89         else return tree[k];
 90     }
 91     else
 92     {
 93         if(cnt[k]>0)
 94         {
 95             up(k,m-n+1);
 96             down(k);
 97         }
 98         LL nx=ask(l,r,2*k+1,n,(n+m)/2);
 99         LL ny=ask(l,r,2*k+2,(n+m)/2+1,m);
100         return nx+ny;
101     }
102 }
103 void  in(int l,int r,int k,int n,int m,int u)
104 {
105     if(l>m||r<n)
106     {   if(cnt[k]>0)
107         {
108              up(k,m-n+1);
109             down(k);
110         }
111         return ;
112     }
113     else if(l<=n&&r>=m)
114     {
115         cnt[k]+=u;
116          up(k,m-n+1);
117             down(k);
118         return ;
119     }
120     else
121     {
122         if(cnt[k]>0)
123         {
124             up(k,m-n+1);
125             down(k);
126         }
127         in(l,r,2*k+1,n,(n+m)/2,u);
128         in(l,r,2*k+2,(n+m)/2+1,m,u);
129     }
130 }

 

1164 - Horrible Queries

标签:

原文地址:http://www.cnblogs.com/zzuli2sjy/p/5496697.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!