forked from Ashishgup1/Competitive-Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntervals Handling
47 lines (42 loc) · 844 Bytes
/
Intervals Handling
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
map<int, int> active;
void init()
{
active[-1] = -1;
active[2e9] = 2e9;
active[1] = n;
}
void add(int L, int R) //Always remove [L, R] before adding
{
active[L]=R;
ans+=R-L+1;
}
void remove(int L, int R)
{
int removed=0;
auto it = active.lower_bound(L);
it--;
if(it->second>=L)
{
active[L] = it->second;
it->second = L-1;
}
it++;
while(it->first <= R)
{
if(it->second > R)
{
removed+=R + 1 - it->first;
active[R+1] = it->second;
}
else
removed+= it->second - it->first + 1;
auto it2=it;
it++;
active.erase(it2);
}
ans-=removed;
}
//Problem 1: https://codeforces.com/contest/915/problem/E
//Solution 1: https://codeforces.com/contest/915/submission/40028187
//Problem 2 (with sets): https://codeforces.com/contest/899/problem/E
//Solution 2: https://codeforces.com/contest/899/submission/45963607