MCPcopy Create free account
hub / github.com/ShahjalalShohag/code-library / interval_set

Class interval_set

Data Structures/Interval Set.cpp:7–46  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5//for Q assign operation it takes Qlogn time in total
6template<class T>
7struct interval_set {
8 map<pair<int, int>, T> value;//{r,l}=val
9
10 void init(int n) {
11 value[ {n, 1}] = (T)0; //initial value
12 }
13 //assign a[i]=val for l<=i<=r
14 //returns affected ranges before performing this assign operation
15 vector<pair<pair<int, int>, T> > assign(int l, int r, T val) {
16 auto bg = value.lower_bound({l, 0})->first;
17 if(bg.second != l) {
18 T val = value[bg];
19 value.erase(bg);
20 value[ {l - 1, bg.second}] = val;
21 value[ {bg.first, l}] = val;
22 }
23
24 auto en = value.lower_bound({r, 0})->first;
25 if(en.first != r) {
26 T val = value[en];
27 value.erase(en);
28 value[ {en.first, r + 1}] = val;
29 value[ {r, en.second}] = val;
30 }
31
32 vector<pair<pair<int, int>, T> > ret;
33 auto itt = value.lower_bound({l, 0});
34 while(true) {
35 if(itt == value.end() || itt->first.first > r) break;
36 ret.push_back({{itt->first.second, itt->first.first}, itt->second});
37 ++itt;
38 }
39
40 for(auto it : ret)
41 value.erase({it.first.second, it.first.first});
42
43 value[ {r, l}] = val;
44 return ret;
45 }
46};
47interval_set<int>se;
48//assign a value in range in each query
49//in the end print the sum of the array elements

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected