-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbs.cpp
More file actions
256 lines (192 loc) · 4.93 KB
/
pbs.cpp
File metadata and controls
256 lines (192 loc) · 4.93 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <bits/stdc++.h>
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
*this << "[";
for (auto it = d.b; it != d.e; ++it)
*this << ", " + 2 * (it == d.b) << *it;
ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define aff(v) for(auto e:v) cout<<e<<" ";cout<<endl;
#define mem1(a) memset(a,-1,sizeof(a))
#define mem0(a) memset(a,0,sizeof(a))
#define all(x) (x).begin(),(x).end()
#define yes(check) cout << (check ? "Yes" : "No") << endl
typedef long long ll;
typedef long double ld;
ll n,d;
const ll prime=1e9+7;
const ll prime2=998244353;
const ll prime3=7901;
const ll MOD = 998244353;
ll INF=2e18;
// problem: https://atcoder.jp/contests/agc002/tasks/agc002_d
// parallel binary search, you use this techniqe with queries (sometimes even without queries);
// especially when you know that for every query you can binary search on the ans :)
const int NAX=1e5+5;
vector<ll> vec(NAX);
class DSU {
private:
vector<int> p, sz;
// stores previous unites
vector<pair<int &, int>> history;
public:
DSU(int n) : p(n), sz(n, 1) { iota(p.begin(), p.end(), 0); }
int get(int x) { return x == p[x] ? x : get(p[x]); }
void unite(int a, int b) {
a = get(a);
b = get(b);
if (a == b) {
history.push_back({sz[a], sz[a]});
history.push_back({p[b], p[b]});
return;
}
if (sz[a] < sz[b]) { swap(a, b); }
// save this unite operation
history.push_back({sz[a], sz[a]});
history.push_back({p[b], p[b]});
p[b] = a;
sz[a] += sz[b];
}
int getsz(int x){
int a=get(x);
return sz[a];
}
int snapshot() { return history.size(); }
void rollback() {
ll until=max(0,(int)history.size()-2);
while (snapshot() > until) {
history.back().first = history.back().second;
history.pop_back();
}
}
};
int lll[NAX];
int rrr[NAX];
vector<pair<ll,ll>> edges;
DSU dsu(NAX);
int ans[NAX];
void upd(int x, int c) {
if (c == 1) {
dsu.unite(edges[x].first, edges[x].second);
} else {
dsu.rollback();
}
}
ll how[NAX];
void rec(int l, int r, vector<int> queries, int &it) {
if (queries.size() == 0) return;
int md = l + (r - l) / 2;
debug() << imie(l) imie(r) imie(queries) imie(md) imie(it);
vector<int> left_queries, right_queries;
while (it < md) {
debug() << imie("aHAA") imie(it);
upd(++it, 1);
}
while (it > md) {
upd(it--, -1);
}
for (auto q : queries) {
int a = dsu.get(lll[q]);
int b = dsu.get(rrr[q]);
ll szz=dsu.getsz(a);
debug() << imie(szz);
if(a!=b){
szz+=dsu.getsz(b);
}
debug() << imie(queries) imie(lll[q]) imie(rrr[q]) imie(q) imie(a) imie(b) imie(szz) imie(how[q]);
// debug() << imie(q) imie(a) imie(b) imie(lll[q]) imie(rrr[q]) imie(it) imie(l) imie(r);
if (szz>=how[q]) {
ans[q] = md;
left_queries.push_back(q);
} else {
right_queries.push_back(q);
}
}
queries.clear();
if (l == r) {
return;
}
rec(l, md, left_queries, it);
rec(md + 1, r, right_queries, it);
}
void solve(){
cin >>n;
vector<int> queries;
edges.clear();
// debug() << imie(stMin.RSQ(0,n-1));
ll m;
cin >>m;
int q;
for(int i=0;i<m;i++){
ll from,to;
cin >> from >>to;
from--;to--;
edges.push_back({from,to});
}
cin >>q;
for(int i=0;i<q;i++){
cin >>lll[i] >> rrr[i];
lll[i]--;
rrr[i]--;
cin >> how[i];
queries.push_back(i);
}
int it=-1;
rec(0,m-1,queries,it);
for(int i=0;i<q;i++){
cout << ans[i]+1<<' ';
}
cout <<'\n';
}
int main() {
fastInp;
// init();
//debug() << imie(s);
//freopen("grids.in","r",stdin);
//freopen("res.out","w",stdout);
// __gcd <long long> (x, y);
int tc=1;
//debug() << imie(sieve);
//cin >> tc;
debug() << imie(tc);
//cout << setprecision(10)<<fixed;
while(tc--){
//i++;
//cout <<"Test " << i << ":" << "\n";
solve();
}
return 0;
}
/*
Some insights:
.Binary search
.Graph representation
.Write brute force code
.Change your approach
*/