1
+ /* *
2
+ * @file 7502.cpp
3
+ * @author Macesuted ([email protected] )
4
+ * @date 2024-08-05
5
+ *
6
+ * @copyright Copyright (c) 2024
7
+ *
8
+ */
9
+
10
+ #include < bits/stdc++.h>
11
+ using namespace std ;
12
+
13
+ #define maxn 500005
14
+
15
+ int64_t TIME;
16
+
17
+ struct Func {
18
+ mutable int64_t k, b;
19
+
20
+ Func (void ) : k(0 ), b(0 ) {}
21
+ Func (int64_t _k, int64_t _b) : k(_k), b(_b) {}
22
+
23
+ int64_t getVal (void ) const { return k * TIME + b; }
24
+
25
+ bool operator <(const Func& o) const { return getVal () < o.getVal () || (getVal () == o.getVal () && k < o.k ); }
26
+ Func operator +(const Func& o) const { return Func (k + o.k , b + o.b ); }
27
+ Func operator -(const Func& o) const { return Func (k - o.k , b - o.b ); }
28
+ Func& operator +=(const Func& o) { return *this = *this + o; }
29
+ Func& operator -=(const Func& o) { return *this = *this - o; }
30
+ };
31
+
32
+ int64_t t[maxn], l[maxn], r[maxn];
33
+
34
+ multiset<Func> S;
35
+ set<pair<int64_t , Func>> opTim;
36
+ Func ans;
37
+
38
+ int64_t cross (Func a, Func b) { return (b.b - a.b + 1 ) / 2 ; }
39
+
40
+ void insertL (set<Func>::iterator p) {
41
+ if (p == S.begin ()) return ;
42
+ auto q = p;
43
+ q--;
44
+ opTim.emplace (cross (*q, *p), *q);
45
+ return ;
46
+ }
47
+ void eraseL (set<Func>::iterator p) {
48
+ if (p == S.begin ()) return ;
49
+ auto q = p;
50
+ q--;
51
+ opTim.erase ({cross (*q, *p), *q});
52
+ return ;
53
+ }
54
+ void insertR (set<Func>::iterator p) {
55
+ auto q = p;
56
+ q++;
57
+ if (q == S.end ()) return ;
58
+ opTim.emplace (cross (*p, *q), *p);
59
+ return ;
60
+ }
61
+ void eraseR (set<Func>::iterator p) {
62
+ auto q = p;
63
+ q++;
64
+ if (q == S.end ()) return ;
65
+ opTim.erase ({cross (*p, *q), *p});
66
+ return ;
67
+ }
68
+
69
+ void print (void ) {
70
+ cerr << " PRINT " << TIME << endl;
71
+ for (auto [k, b] : S) cerr << ' (' << k << ' ,' << b << ' ,' << k * TIME + b << ' )' ;
72
+ cerr << endl;
73
+ for (auto [tim, Func] : opTim) cerr << " (" << tim << ' ' << Func.k << ' ' << Func.b << ' )' ;
74
+ cerr << endl;
75
+ cerr << ans.k << ' ' << ans.b << ' ' << ans.getVal () + S.size () / 2 << endl;
76
+ return ;
77
+ }
78
+
79
+ void insert (int l, int r) {
80
+ auto pl = S.lower_bound ({-1 , l + TIME});
81
+ if (pl == S.end ()) return ;
82
+ if (pl->getVal () > r) {
83
+ if (pl->k == -1 ) return ;
84
+ auto it = S.emplace (+1 , l - 1 - TIME);
85
+ it = S.emplace (-1 , r + 1 + TIME);
86
+ insertL (it);
87
+ ans += Func{+1 , l - 1 - TIME};
88
+ ans -= Func{-1 , r + 1 + TIME};
89
+ return ;
90
+ }
91
+ auto pr = --S.lower_bound ({-1 , r + 1 + TIME});
92
+ if (pl->k == +1 ) eraseR (pl), ans -= *pl, pl->b = l - 1 - TIME, ans += *pl, insertR (pl), pl++;
93
+ if (pr->k == -1 )
94
+ eraseL (pr), ans += *pr, pr->b = r + 1 + TIME, ans -= *pr, insertL (pr);
95
+ else
96
+ pr++;
97
+ for (auto i = pl; i != pr;) {
98
+ eraseL (i);
99
+ ans += *i, i = S.erase (i);
100
+ eraseR (i);
101
+ ans -= *i, i = S.erase (i);
102
+ if (i != S.end ()) insertL (i);
103
+ }
104
+ return ;
105
+ }
106
+
107
+ void update (set<pair<int64_t , Func>>::iterator it) {
108
+ auto p = S.find (it->second );
109
+ ans -= *p, p = S.erase (p);
110
+ ans += *p, p = S.erase (p);
111
+ opTim.erase (it);
112
+ return ;
113
+ }
114
+
115
+ void solve (void ) {
116
+ int n, q;
117
+ cin >> n >> q;
118
+ for (int i = 1 ; i <= n; i++) cin >> t[i] >> l[i] >> r[i];
119
+ TIME = 0 , S.clear (), S.insert ({-1 , 0 }), S.insert ({+1 , 0 }), opTim.clear (), opTim.emplace (t[n + 1 ] = INT64_MAX, Func{0 , 0 });
120
+ ans = Func (2 , 0 );
121
+ for (int i = 1 , j = 1 ; i <= q; i++) {
122
+ int64_t qt;
123
+ cin >> qt;
124
+ while (min (t[j], opTim.begin ()->first ) <= qt) {
125
+ if (j <= n && t[j] < opTim.begin ()->first )
126
+ TIME = t[j], insert (l[j], r[j]), j++;
127
+ else
128
+ TIME = opTim.begin ()->first - 1 , update (opTim.begin ());
129
+ }
130
+ TIME = qt;
131
+ cout << ans.getVal () + S.size () / 2 << ' ' ;
132
+ }
133
+ cout << endl;
134
+ return ;
135
+ }
136
+
137
+ int main () {
138
+ ios::sync_with_stdio (false ), cin.tie (nullptr ), cout.tie (nullptr );
139
+
140
+ int _;
141
+ cin >> _;
142
+ while (_--) solve ();
143
+
144
+ return 0 ;
145
+ }
0 commit comments