-
Notifications
You must be signed in to change notification settings - Fork 0
/
1164 - Horrible Queries.cpp
executable file
·81 lines (75 loc) · 1.53 KB
/
1164 - Horrible Queries.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//1164 - Horrible Queries
//By MD.Imam Hossain Johny
//Dept. ICE, NSTU-11 batch
#include<bits/stdc++.h>
using namespace std;
#define SZ 100005
long long int arra[SZ];
struct info
{
long long int sum;
long long int prop;
}tree[SZ*3+1];
void update(int node, int b, int e, int i,int j,long long int value)
{
if(b>j || e<i)
return;
if(b>=i && e<=j)
{
tree[node].sum=tree[node].sum+(e-b+1)*value;
tree[node].prop=tree[node].prop+value;
return;
}
int mid=(b+e)/2;
int left=node<<1;
int right=(node<<1)+1;
update(left,b,mid,i,j,value);
update(right,mid+1,e,i,j,value);
tree[node].sum=tree[left].sum+tree[right].sum+(e-b+1)*tree[node].prop;
}
long long int query(int node,int b,int e, int i,int j,long long int carry=0)
{
if(b>j || e<i)
return 0;
if(b>=i && e<=j)
return tree[node].sum+(e-b+1)*carry;
int mid=(b+e)/2;
int left=node<<1;
int right=(node<<1)+1;
long long int p1=query(left,b,mid,i,j,carry+tree[node].prop);
long long int p2=query(right,mid+1,e,i,j,carry+tree[node].prop);
return p1+p2;
}
int main()
{
int x;
scanf("%d",&x);
for(int t=1;t<=x;t++)
{
memset(arra,0,sizeof arra);
memset(tree,0,sizeof tree);
int n=0;
int q=0;
printf("Case %d:\n",t);
scanf("%d%d",&n,&q);
for(int i=1;i<=q;i++)
{
int operation,x,y;
long long v;
long long int ans=0;
scanf("%d",&operation);
if(operation==0)
{
scanf("%d%d%lld",&x,&y,&v);
update(1,0,n-1,x,y,v);
}
else if(operation==1)
{
scanf("%d%d",&x,&y);
ans=query(1,0,n-1,x,y);
printf("%lld\n",ans);
}
}
}
return 0;
}