이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 (qry.size() * ce.size() * 4 >= BS * BS || i == q) {
for (int j = 1; j <= m; ++j)
if (la[j] > lt)
ce.emplace_back(j,la[j],i,d[j]);
sort(begin(ce),end(ce),[](const tuple<int,int,int,int>& x,const tuple<int,int,int,int>& y){return get<3>(x)>get<3>(y);});
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;
if (ew < w)
break;
int u = find(e[ei].F), v = find(e[ei].S);
if (tl <= t && t <= tr && u != v) {
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... |