이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 1e6+10;
int n,m,Q;
vector <pii> edge;
vector <pair<pii,int>> upd[maxn],q[maxn];
int ans[maxn];
vector <pii> change[maxn];
int f[maxn],sz[maxn];
vector <pair<int,pii>> hist;
int val[maxn];
int getf(int x) {
if (f[x]==x) return x;
else return getf(f[x]);
}
void merge(int u,int v,bool redo) {
int fu = getf(u),fv = getf(v);
if (fu==fv) return;
if (sz[fu]>sz[fv]) swap(fu,fv);
if (redo) hist.pb({fu,{fv,sz[fv]}});
f[fu] = fv;
sz[fv] += sz[fu];
}
bool cmp(int x,int y) {
return val[x]<val[y];
}
bool cmpp(pair<pii,int> x,pair<pii,int> y) {
return x.fi.se>y.fi.se;
}
void redoo() {
while (!hist.empty()) {
auto tmp = hist.back();
hist.pop_back();
f[tmp.fi]=tmp.fi;
sz[tmp.se.fi] = tmp.se.se;
}
}
void solve(int cur) {
rep(i,1,n+1) f[i] = i,sz[i] = 1;
for (auto i:upd[cur]) change[i.fi.fi].pb({i.se,i.fi.se});
vector <int> samedge,difedge;
rep(i,0,m) {
if (SZ(change[i])==0) samedge.pb(i);
else difedge.pb(i);
}
sort(ALL(samedge),cmp);
sort(ALL(q[cur]),cmpp);
for (auto i:q[cur]) {
while (!samedge.empty() and val[samedge.back()]>=i.fi.se) {
int idx = samedge.back();
samedge.pop_back();
merge(edge[idx].fi,edge[idx].se,false);
}
for (int j:difedge) {
int curval = val[j];
rep(k,0,SZ(change[j])) {
if (change[j][k].fi>i.se) break;
curval = change[j][k].se;
}
if (curval>=i.fi.se) merge(edge[j].fi,edge[j].se,true);
}
ans[i.se] = sz[getf(i.fi.fi)];
redoo();
}
for (int j:difedge) {
val[j] = change[j].back().se;
while (!change[j].empty()) change[j].pop_back();
}
}
int main() {
// freopen("input.txt","r",stdin);
memset(ans,-1,sizeof(ans));
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,m) {
int u,v,w;
cin>>u>>v>>w;
edge.pb({u,v});
val[i] = w;
}
cin>>Q;
int sz = (int)sqrt(m*log(n)/log(2));
if (sz==0) sz=1;
debug(sz);
rep(i,0,Q) {
int op,x,y;
cin>>op>>x>>y;
if (op==1) {
x--;
upd[i/sz].pb({{x,y},i});
}
else q[i/sz].pb({{x,y},i});
}
rep(i,0,(Q-1)/sz+1) solve(i);
rep(i,0,Q) {
if (ans[i]!=-1) cout<<ans[i]<<"\n";
}
return 0;
}
# | 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... |