1164 - Horrible Queries

   PDF (English) Statistics Forum
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 }
时间: 2024-10-29 19:09:57

1164 - Horrible Queries的相关文章

LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1164 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.

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

HDU4027 Can you answer these queries 线段树区间求和+剪枝

给了你n,然后n个数字在一个数组中,接下来m个询问,每个询问三个数字 t,x,y,若t==0,那么修改区间[x,y]的每一个值,变为原来每个位置上的数 开根号取整,若t==1,那么对区间[x,y]求和 由于n,m,很大,所以树状数组铁定超时,若直接用线段树来做区间修改,那么也是超时,这类题目没别的方法了,静心剪枝,发现题目给的数据范围为2^63,有没有发现,2^63开根号 绝对不需要开10次,就能到1,到1以后就不需要再开了,意思就是若有某个区间[x,y]每一个点的值都为1时,这一段区间事实上是

[CodeChef - GERALD07 ] Chef and Graph Queries

Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected graph G. This graph consists of N vertices and M edges. Each vertex of the graph has an unique index from 1 to N, also each edge of the graph has an uniq

Bitwise And Queries

Bitwise And Queries Time limit: 1500 msMemory limit: 128 MB You are given QQ queries of the form a\ b\ xa b x. Count the number of values yy such that a \leq y \leq ba≤y≤b and x\ \& \ y = xx & y=x, where we denote by \&& the bitwise and op

JavaScript根据CSS的Media Queries来判断浏览设备的方法

CSS 部分 首先随便新建一个用来做判断的类,然后通过 Media Queries 来对这个类的 z-index 属性赋予不同的值.这个类仅作为 JavaScript 读取使用,所以需要将其移出屏幕窗口,让浏览者不可见以免引起意外情况. 作为演示,下面代码设置了四种设备状态:桌面普通版.小屏幕桌面版.平板电脑版和手机版. /* default state */ .state-indicator { position: absolute; top: -999em; left: -999em; z-

lightoj Again Array Queries

1100 - Again Array Queries   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB Given an array with n integers, and you are given two indices i and j (i ≠ j) in the array. You have to find two integers in the range whose diffe

响应式web设计之CSS3 Media Queries

开始研究响应式web设计,CSS3 Media Queries是入门. Media Queries,其作用就是允许添加表达式用以确定媒体的环境情况,以此来应用不同的样式表.换句话说,其允许我们在不改变内容的情况下,改变页面的布局以精确适应不同的设备. 那么,Media Queries是如何工作的? 两种方式,一种是直接在link中判断设备的尺寸,然后引用不同的css文件: <link rel="stylesheet" type="text/css" href=

Medial Queries的另一用法:实现IE hack的方法

所谓Medial Queries就是媒体查询. 随着Responsive设计的流行,Medial Queries可算是越来越让人观注了.他可以让Web前端工程实现不同设备下的样式选择,让站点在不同的设备中实现不同的效果. 众所周知,有些时候为了实现IE下的某些效果与现代浏览器一致,我们不得不使用一些hack手段来实现目的.比如说使用“\0”,“\”和“\9”来仅让IE某些版本识别,而对于现代浏览器来说,他会直接无视这些代码.今天我想为大家介绍的是使用@media实现IE hack的方法. 仅IE