1+ #include < bits/stdc++.h> // https://leetcode.cn/problems/range-module
2+ using namespace std ;
3+ using ll = long long ;
4+ class RangeModule
5+ {
6+ constexpr static ll inf = 1e9 ;
7+ constexpr static ll maxn = 60 * 1e4 ; // log(inf)*q
8+ struct node
9+ {
10+ ll ls, rs, sum, laz;
11+ } t[maxn] = {};
12+ int cnt = 1 ;
13+ #define cll const ll &
14+ #define mkcf ll cf = (lf + rf) >> 1
15+ void pushdown (int r, cll lf, cll rf)
16+ {
17+ if (!t[r].ls )
18+ t[r].ls = ++cnt;
19+ if (!t[r].rs )
20+ t[r].rs = ++cnt;
21+ if (!t[r].laz )
22+ return ;
23+ if (t[r].laz == 1 )
24+ {
25+ mkcf;
26+ t[t[r].ls ].sum = (cf - lf + 1 ); // len-len/2
27+ t[t[r].rs ].sum = rf - cf; // len/2
28+ }
29+ else
30+ {
31+ t[t[r].ls ].sum = t[t[r].rs ].sum = 0 ;
32+ }
33+ t[t[r].ls ].laz = t[t[r].rs ].laz = t[r].laz ;
34+ t[r].laz = 0 ;
35+ }
36+ void pushup (int r)
37+ {
38+ t[r].sum = t[t[r].ls ].sum + t[t[r].rs ].sum ;
39+ }
40+ void update (int r, ll lf, ll rf, cll lc, cll rc, cll v)
41+ {
42+ if (lc <= lf && rf <= rc)
43+ {
44+ t[r].sum += (v == 1 ) * (rf - lf + 1 );
45+ t[r].laz = v;
46+ return ;
47+ }
48+ pushdown (r, lf, rf);
49+ mkcf;
50+ if (lc <= cf)
51+ update (t[r].ls , lf, cf, lc, rc, v);
52+ if (rc >= cf + 1 )
53+ update (t[r].rs , cf + 1 , rf, lc, rc, v);
54+ pushup (r);
55+ }
56+ ll query (int r, ll lf, ll rf, cll lc, cll rc)
57+ {
58+ if (lc <= lf && rf <= rc)
59+ return t[r].sum ;
60+ pushdown (r, lf, rf);
61+ ll res = 0 ;
62+ mkcf;
63+ if (lc <= cf)
64+ res += query (t[r].ls , lf, cf, lc, rc);
65+ if (rc >= cf + 1 )
66+ res += query (t[r].rs , cf + 1 , rf, lc, rc);
67+ return res;
68+ }
69+
70+ public:
71+ RangeModule () {}
72+
73+ void addRange (int left, int right)
74+ {
75+ update (1 , 1 , inf, left, right - 1 , 1 );
76+ }
77+
78+ bool queryRange (int left, int right)
79+ {
80+ return query (1 , 1 , inf, left, right - 1 ) == right - left;
81+ }
82+
83+ void removeRange (int left, int right)
84+ {
85+ update (1 , 1 , inf, left, right - 1 , -1 );
86+ }
87+ };
88+
89+ signed main ()
90+ {
91+ ios::sync_with_stdio (false ), cin.tie (0 );
92+
93+ return 0 ;
94+ }
95+
96+ /* *
97+ * Your RangeModule object will be instantiated and called as such:
98+ * RangeModule* obj = new RangeModule();
99+ * obj->addRange(left,right);
100+ * bool param_2 = obj->queryRange(left,right);
101+ * obj->removeRange(left,right);
102+ */
0 commit comments