-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1164 - Horrible Queries.cpp
83 lines (77 loc) · 1.72 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
81
82
83
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll arr[100005];
struct info
{
ll sum , prop;
}tree[100005 * 3];
void update (ll node , ll b , ll e , ll i , ll j , ll x)
{
if (i > e || j < b)
return;
if (b >= i && e <= j)
{
tree[node].sum += ((e - b + 1) * x);
tree[node].prop += x;
return;
}
ll l = node * 2;
ll r = (node * 2) + 1;
ll mid = (b + e) / 2;
update(l, b, mid, i, j, x);
update(r, mid + 1, e, i, j, x);
tree[node].sum = tree[l].sum + tree[r].sum + (e - b + 1) * tree[node].prop;
}
ll Q (ll node, ll b , ll e , ll i , ll j , ll carry = 0)
{
if(i > e or j < b)
{
return 0;
}
if(b >= i && e <= j)
{
return tree[node].sum + carry * ((e - b + 1));
}
ll mid , l , r;
l = node * 2;
r = node * 2+ 1;
mid = (b + e) / 2;
ll x = Q(l ,b , mid, i, j,carry+ tree[node].prop);
ll y = Q(r ,mid + 1 , e, i , j,carry+ tree[node].prop);
return x + y;
}
int main()
{
ll n , q;
int tes , o = 0;
scanf(" %d", &tes);
while(tes--)
{
o++;
memset(tree, 0 , sizeof tree);
scanf(" %lld %lld" ,&n , &q);
printf("Case %d:\n",o);
while(q--)
{
int ck;
ll i ,j , vl;
scanf(" %d", &ck);
if(ck == 0)
{
scanf("%lld %lld %lld", &i , &j , &vl);
i++;
j++;
update(1 , 1 , n, i, j, vl);
}
else if(ck == 1)
{
scanf("%lld %lld", &i , &j);
i++;
j++;
ll sum = Q(1 , 1 , n, i, j );
printf("%lld\n", sum);
}
}
}
}