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 }
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 }

1164 - Horrible Queries的更多相关文章

  1. 题解【SP8002】HORRIBLE - Horrible Queries

    题面 题解 这是一道线段树的模板题. 题目需要我们维护一个支持区间修改.区间查询的一个数据结构,很容易想到线段树. 然后发现和洛谷上线段树的模板1是同一道题. 由于本题中每个数的初始值都为\(0\), ...

  2. GSS4 2713. Can you answer these queries IV 线段树

    GSS7 Can you answer these queries IV 题目:给出一个数列,原数列和值不超过1e18,有两种操作: 0 x y:修改区间[x,y]所有数开方后向下调整至最近的整数 1 ...

  3. Spring Data JPA教程, 第三部分: Custom Queries with Query Methods(翻译)

    在本人的Spring Data JPA教程的第二部分描述了如何用Spring Data JPA创建一个简单的CRUD应用,本博文将描述如何在Spring Data JPA中使用query方法创建自定义 ...

  4. 洛谷P3538 [POI2012]OKR-A Horrible Poem [字符串hash]

    题目传送门 A Horrible Poem 题目描述 Bytie boy has to learn a fragment of a certain poem by heart. The poem, f ...

  5. 实践 HTML5 的 CSS3 Media Queries

    先来介绍下 media,确切的说应该是 CSS media queries(CSS 媒体查询),媒体查询包含了一个媒体类型和至少一个使用如宽度.高度和颜色等媒体属性来限制样式表范围的表达式.CSS3 ...

  6. SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDatasource' 的访问

    delphi ado 跨数据库访问 语句如下 ' and db = '帐套1' 报错内容是:SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATE ...

  7. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  8. 使用CSS3 Media Queries实现网页自适应

    原文来源:http://webdesignerwall.com 翻译:http://xinyo.org 当今银屏分辨率从 320px (iPhone)到 2560px (大屏显示器)或者更大.人们也不 ...

  9. SQL Queries from Transactional Plugin Pipeline

    Sometimes the LINQ, Query Expressions or Fetch just doesn't give you the ability to quickly query yo ...

随机推荐

  1. 也谈string.Join和StringBuilder的性能比较

    前几天在园子里面看到一篇讲StringBuilder性能的文章.文章里面给出了一个测试用例,比较StringBuilder.AppendJoin和String.Join的性能.根据该测试结果,&quo ...

  2. 二进制免编译My SQL

    一 下载 MySQL 安装包教程 https://blog.csdn.net/zhan107876/article/details/100701135 ll -h mysql-5.6.47-linux ...

  3. SonarQube的部分规则探讨

    引言:为了更好的使项目代码规范化,减少Bug的出现,因此最近引入了SonarQube来帮助检测代码问题,这里就分享部分有趣的规则. 注:因为保密原则,文章贴出来的代码都是我按照格式仿写的,并非公司源码 ...

  4. 【模板】二分图最大权完美匹配(KM算法)/洛谷P6577

    题目链接 https://www.luogu.com.cn/problem/P6577 题目大意 给定一个二分图,其左右点的个数各为 \(n\),带权边数为 \(m\),保证存在完美匹配. 求一种完美 ...

  5. C#gridview尾部统计

    protected void gridSettlement_RowDataBound(object sender, GridViewRowEventArgs e) { if (dtSettlement ...

  6. C#数据库连接方式【简版】

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using ...

  7. accustom

    近/反义词: acclimatize, familiarize, habituate, inure, get used to, orient; alienate, estrange, wean New ...

  8. Flink基础

      一.抽象层次 Flink提供不同级别的抽象来开发流/批处理应用程序. 最低级抽象只提供有状态流.它 通过Process Function嵌入到DataStream API中.它允许用户自由处理来自 ...

  9. Spark基础:(二)Spark RDD编程

    1.RDD基础 Spark中的RDD就是一个不可变的分布式对象集合.每个RDD都被分为多个分区,这些分区运行在分区的不同节点上. 用户可以通过两种方式创建RDD: (1)读取外部数据集====> ...

  10. C++之无子数

    题目如下: 1 #include <iostream> 2 3 using namespace std; 4 5 6 bool isThisNumhaveChild(int num); 7 ...