forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB.cpp
More file actions
79 lines (69 loc) · 1.86 KB
/
B.cpp
File metadata and controls
79 lines (69 loc) · 1.86 KB
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
#include <bits/stdc++.h>
using namespace std;
#define DEFAULT 0
#define ACTIVE 1
#define INACTIVE 2
struct SegmentTree
{
int state;
long long low, high, mid, sum, minValue, maxValue;
SegmentTree *left, *right;
SegmentTree(long long low, long long high): low(low), high(high)
{
mid = (low + high) / 2;
state = DEFAULT;
sum = (low + high - 2) * (high - low + 1) / 2;
minValue = low - 1;
maxValue = high - 1;
left = NULL;
right = NULL;
}
void updateChildren()
{
left -> state = right -> state = ACTIVE;
left -> minValue = left -> maxValue = minValue;
left -> sum = (mid - low + 1) * minValue;
right -> minValue = right -> maxValue = minValue;
right -> sum = (high - mid) * minValue;
state = INACTIVE;
}
long long update(long long x, long long y, long long v)
{
if (minValue >= v) return 0;
long long res = 0;
if (low == x && high == y && maxValue <= v)
{
state = ACTIVE;
res = (high - low + 1) * v - sum;
minValue = maxValue = v;
sum = (high - low + 1) * v;
return res;
}
if (left == NULL)
left = new SegmentTree(low, mid);
if (right == NULL)
right = new SegmentTree(mid + 1, high);
if (state == ACTIVE)
updateChildren();
if (x <= mid)
res += left -> update(x, min(y, mid), v);
if (mid < y)
res += right -> update(max(x, mid + 1), y, v);
sum = left -> sum + right -> sum;
state = INACTIVE;
maxValue = max(left -> maxValue, right -> maxValue);
minValue = min(left -> minValue, right -> minValue);
return res;
}
};
int main()
{
int n, x, y;
SegmentTree *tree = new SegmentTree(0, 2000000000);
scanf("%d", &n);
while (n--)
{
scanf("%d%d", &x, &y);
printf("%I64d\n", tree -> update(x, x + y, x + y));
}
}