This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 51212, M = 121212, Q = 121212;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define F first
#define S second
pii e[M];
int d[M], la[M], ans[Q], dsu[N], vis[N], qu[N], vct;
vector<int> g[N];
int find(int u) {return dsu[u] < 0 ? u : dsu[u] = find(dsu[u]);}
void meld(int u,int v)
{
u = find(u), v = find(v);
if (u == v)
return;
if (dsu[v] < dsu[u])
swap(u,v);
dsu[u] += dsu[v];
dsu[v] = u;
}
set<pii,greater<pii>> se;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,q;
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> e[i].F >> e[i].S >> d[i];
se.emplace(d[i],i);
}
const int BS = sqrt(n+m)+1;
vector<tuple<int,int,int>> qry; // w, vertex id, time
vector<tuple<int,int,int,int>> ce; // edge id, time l, time r, w
vector<int> cv;
cin >> q;
for (int i = 1, lt = 0; i <= q; ++i) {
int a,b,c;
cin >> a >> b >> c;
if (a == 1) {
ce.emplace_back(b,la[b],i-1,d[b]);
la[b] = i;
se.erase({d[b],b});
d[b] = c;
se.emplace(d[b],b);
} else {
qry.emplace_back(c,b,i);
}
if (i - lt == BS || i == q) {
for (int j = 1; j <= m; ++j)
if (la[j] > lt)
ce.emplace_back(j,la[j],i,d[j]);
auto it = begin(se);
sort(begin(qry),end(qry),greater<decltype(qry[0])>());
fill_n(dsu,n+1,-1);
for (const auto & tu : qry) {
int w,vi,t;
tie(w,vi,t) = tu;
for (;it != end(se) && it->F >= w; ++it) {
int ei = it->S;
if (la[ei] <= lt) {
meld(e[ei].F,e[ei].S);
}
}
cv.clear();
for (const auto & eu : ce) {
int ei,tl,tr,ew;
tie(ei,tl,tr,ew) = eu;
int u = find(e[ei].F), v = find(e[ei].S);
if (tl <= t && t <= tr && u != v && ew >= w) {
g[u].emplace_back(v);
g[v].emplace_back(u);
cv.emplace_back(u);
cv.emplace_back(v);
}
}
vi = find(vi);
int ta = dsu[vi], ql=0, qr=0;
qu[qr++] = (vi);
vis[vi] = ++vct;
while (qr - ql) {
int u = qu[ql++];
for (int v : g[u])
if (vis[v] != vct) {
vis[v] = vct;
qu[qr++] = v;
ta += dsu[v];
}
}
ans[t] = -ta;
for (int u : cv)
g[u].clear();
}
qry.clear();
ce.clear();
lt = i;
}
}
for (int i = 1; i <= q; ++i)
if (ans[i])
cout << ans[i] << '\n';
}
# | 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... |