首页 技术 正文
技术 2022年11月15日
0 收藏 443 点赞 2,877 浏览 2878 个字

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 MAHMUDSPECIAL 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 }
48 printf("Case %d:\n",s);
49 for(i=0; i<flag; i++)
50 printf("%lld\n",aa[i]);
51 }
52 }
53 void down(int k)
54 {
55 cnt[2*k+1]+=cnt[k];
56 cnt[2*k+2]+=cnt[k];
57 cnt[k]=0;
58 }
59 void up(int k,int v)
60 {
61 int cc=k;
62 tree[cc]+=cnt[k]*v;
63 if(cc==0)return ;
64 while(cc>=0)
65 {
66 cc=(cc-1)/2;
67 tree[cc]=tree[2*cc+1]+tree[2*cc+2];
68 if(cc==0)
69 return ;
70 }
71 }
72 LL ask(int l,int r,int k,int n,int m)
73 {
74 if(l>m||r<n)
75 {
76 if(cnt[k]>0)
77 {
78 up(k,m-n+1);
79 down(k);
80 }
81 return 0;
82 }
83 else if(l<=n&&r>=m)
84 {
85 if(cnt[k]>0)
86 {
87 up(k,m-n+1);
88 down(k);
89 return tree[k];
90 }
91 else return tree[k];
92 }
93 else
94 {
95 if(cnt[k]>0)
96 {
97
98 down(k);
99 }
100 LL nx=ask(l,r,2*k+1,n,(n+m)/2);
101 LL ny=ask(l,r,2*k+2,(n+m)/2+1,m);
102 return nx+ny;
103 }
104 }
105 void in(int l,int r,int k,int n,int m,int u)
106 {
107 if(l>m||r<n)
108 {
109 if(cnt[k]>0)
110 {
111 up(k,m-n+1);
112 down(k);
113 }
114 return ;
115 }
116 else if(l<=n&&r>=m)
117 {
118 cnt[k]+=u;
119 up(k,m-n+1);
120 down(k);
121 return ;
122 }
123 else
124 {
125 if(cnt[k]>0)
126 {
127
128 down(k);
129 }
130 in(l,r,2*k+1,n,(n+m)/2,u);
131 in(l,r,2*k+2,(n+m)/2+1,m,u);
132 }
133 }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,031
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,148
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,781
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,860