# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
318754 | BeanZ | Bridges (APIO19_bridges) | C++14 | 2618 ms | 131676 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define endl '\n'
const int N = 1e5 + 5;
vector<pair<ll, ll>> rb;
vector<ll> join[N];
ll u[N], v[N], d[N], t[N], a[N], b[N], ch[N], p[N], ans[N];
void rollback(ll u){
while (rb.size() > u){
p[rb.back().first] = rb.back().second;
rb.pop_back();
}
}
ll find_set(ll u){
if (p[u] < 0) return u;
return find_set(p[u]);
}
void dsu(ll u, ll v){
u = find_set(u);
v = find_set(v);
if (u == v) return;
if (p[u] > p[v]) swap(u, v);
rb.push_back({u, p[u]});
rb.push_back({v, p[v]});
p[u] += p[v];
p[v] = u;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("A.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll n, m;
cin >> n >> m;
for (int i = 1; i <= m; i++){
cin >> u[i] >> v[i] >> d[i];
}
ll q;
cin >> q;
for (int i = 1; i <= q; i++){
cin >> t[i] >> a[i] >> b[i];
}
const int base = 1000;
for (int l = 1; l <= q; l += base){
for (int i = 1; i <= n; i++) p[i] = -1;
ll r = min(l + base - 1, q);
for (int i = 1; i <= m; i++) ch[i] = 0;
rb.clear();
vector<ll> memupd, memres, un;
for (int i = l; i <= r; i++){
if (t[i] == 1) memupd.push_back(a[i]);
}
for (int i = l; i <= r; i++){
if (t[i] == 1){
d[a[i]] = b[i];
ch[a[i]] = 1;
} else {
for (auto j : memupd){
if (d[j] >= b[i]){
join[i].push_back(j);
}
}
memres.push_back(i);
}
}
for (int i = 1; i <= m; i++){
if (ch[i] == 0) un.push_back(i);
}
sort(un.begin(), un.end(), [&](ll g, ll h){return d[g] > d[h];});
sort(memres.begin(), memres.end(), [&](ll g, ll h){return b[g] > b[h];});
ll now = 0;
//for (auto j : memres) cout << j << " ";
//cout << endl;
for (auto j : memres){
while (now < un.size() && d[un[now]] >= b[j]){
dsu(u[un[now]], v[un[now]]);
//cout << u[un[now]] << " " << v[un[now]] << " " << now << " " << a[j] << " " << j << endl;
now++;
}
ll tmp = rb.size();
for (auto o : join[j]){
dsu(u[o], v[o]);
}
//cout << b[j] << endl;
//cout << j << endl;
ans[j] = -p[find_set(a[j])];
//cout << j << " " << ans[j] << endl;
rollback(tmp);
}
}
for (int i = 1; i <= q; i++){
if (t[i] == 2) cout << ans[i] << endl;
}
}
/*
3 4
1 2 5
2 3 2
3 1 4
2 3 8
1
2 1 5
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |