LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)
来源:互联网 发布:js indexof 返回值 编辑:程序博客网 时间:2024/05/20 18:44
题目链接: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. 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
代码如下:
#include <cstdio>#include <algorithm>using namespace std;#define lson l , mid , rt << 1#define rson mid + 1 , r , rt << 1 | 1#define LL __int64const LL maxn = 111111;LL add[maxn<<2];LL sum[maxn<<2];//求和void PushUp(LL rt){ sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void PushDown(LL rt,LL len){ if (add[rt]) { add[rt<<1] += add[rt]; add[rt<<1|1] += add[rt]; sum[rt<<1] += add[rt] * (len - (len >> 1)); sum[rt<<1|1] += add[rt] * (len >> 1);//更新右儿子的和 add[rt] = 0; }}void build(LL l,LL r,LL rt){ add[rt] = 0;//初始化为所有结点未被标记 if (l == r) { //scanf("%lld",&sum[rt]); sum[rt] = 0; return ; } LL mid = (l + r) >> 1; build(lson); build(rson); PushUp(rt);}void update(LL L,LL R,LL c,LL l,LL r,LL rt){ if (L <= l && r <= R) { add[rt] += c; sum[rt] += (LL)c * (r - l + 1);//更新代表某个区间的节点和,该节点不一定是叶子节点 return ; } PushDown(rt , r - l + 1); LL mid = (l + r) >> 1; if (L <= mid) update(L , R , c , lson);//更新左儿子 if (mid < R) update(L , R , c , rson);//更新右儿子 PushUp(rt);}LL query(LL L,LL R,LL l,LL r,LL rt){ if (L <= l && r <= R) { return sum[rt]; } PushDown(rt , r - l + 1); LL mid = (l + r) >> 1; LL ret = 0; if (L <= mid) ret += query(L , R , lson); if (mid < R) ret += query(L , R , rson); return ret;}int main(){ LL N , Q; LL t; LL cas = 0; scanf("%lld",&t); while(t--) { scanf("%lld%lld",&N,&Q);//N为节点数 build(1 , N , 1); //建树 printf("Case %d:\n",++cas); while (Q--)//Q为询问次数 { //char op[2]; LL op; LL a , b , c; scanf("%lld",&op); if(op == 1) { scanf("%lld%lld",&a,&b); printf("%lld\n",query(a+1 , b+1 , 1 , N , 1)); } else if(op == 0) { scanf("%lld%lld%lld",&a,&b,&c);//c为区间a到b增加的值 update(a+1 , b+1 , c , 1 , N , 1); } } } return 0;}/*210 50 0 9 101 0 60 3 7 20 4 5 11 5 520 30 10 12 11 11 121 19 19*/
- LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)
- LightOJ 1164 Horrible Queries 线段树区间更新求和
- lightOj 1164 Horrible Queries (线段树)
- lightoj 1164 - Horrible Queries 线段树入门
- poj 3468 线段树(成段增减 区间求和)
- poj3468 A Simple Problem with Integers(线段树模板 功能:区间增减,区间求和)
- 【线段树】-POJ-3468-区间增减&区间求和
- POJ 3468 线段树 区间增减 区间求和
- LightOJ - 1164 Horrible Queries 线段树+懒惰标记
- hdu 4027Can you answer these queries?(线段树功能:平方减少,区间求和)
- 线段树模板-单点增减 区间求和模板
- HDU 1166 敌兵布阵 (线段树 单点增减, 区间求和)
- poj 3468 线段树成段增减+区间求和
- HDU-4027-Can you answer these queries? (线段树,区间开平方,区间求和)
- poj 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
- HDU 1166 敌兵布阵 (线段树 单点增减 区间求和 模板)
- 【POJ】A Simple Problem with Integers(线段树区间修增减求和)
- HDU 1166 敌兵布阵 【线段树(单点增减 区间求和)】
- 5-1简述智能卡的发展,以及射频电子标签在其中所处的位置
- OS用户数据安全:Keychain、Touch ID以及1Password
- SQL中ROUND函数
- hdu 1176 免费馅饼 二维dp
- eclipse 出现Failed to load the JNI shared library jvm.dll 错误的解决办法
- LightOJ 1164 - Horrible Queries(线段树啊 功能:区间增减和区间求和)
- 指针知识(一) 直接引用、指针定义、初始化、运算符
- atomic与nonatomic
- 32位CPU所含有的寄存器
- php笔记
- Spring MVC和Struts2的比较(转)
- 第4周项目4-指向学生类的指针
- MySQL设置密码
- Django之模板继承