This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#ifndef LOCAL
#include "collapse.h"
#endif // LOCAL
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
const int maxn = 1e5 + 5;
typedef pair<int,int> ii;
int n , m , q;
vector<int> qList[maxn];
int type[maxn] , from[maxn] , to[maxn];
int cliff_at[maxn] , date[maxn];
int in[maxn] , out[maxn];
int res[maxn];
struct Dsurollback{
pair<int*,int> data[(int)2e6];
int lab[maxn] , sz;
int FindLab(int u){
return lab[u] < 0 ? u : FindLab(lab[u]);
}
void Change(int & u , int v){
data[sz++] = mp(&u,u);
u = v;
}
void rollback(int sz1){
while(sz > sz1){
(*data[sz-1].first) = data[sz-1].second;
--sz;
}
cnt -= cnt1;
cnt1 = 0;
}
int cnt = 0;
int cnt1 = 0;
void Merge(int u , int v){
u = FindLab(u);v = FindLab(v);
if(u == v)return;
if(lab[u] > lab[v])swap(u,v);
Change(lab[u],lab[u]+lab[v]);
Change(lab[v],u);
cnt++;cnt1++;
}
int cur(){return cnt1 = 0 , sz;};
void reset(){
fill_n(lab,maxn,-1);cnt = cnt1 = 0;
sz = 0;
}
}Dsu;
void solve(){
const int MAGIC = 400;
for(int l = 0 ; l < m ; l += MAGIC){
vector<int> sp , norm,q;
int r = min(m,l+MAGIC) - 1;
for(int i = 0 ; i <= r ; ++i){
if(i >= l)for(auto & id : qList[i])q.pb(id);
if(out[i] < l || type[i] == 1)continue;
if(i < l && out[i] > r)norm.pb(i);
else sp.pb(i);
}
sort(norm.begin(),norm.end(),[&](const int a , const int b){return to[a] < to[b];});
sort(q.begin(),q.end(),[&](const int a , const int b){return cliff_at[a] < cliff_at[b];});
int qi = 0 , qe = 0;
Dsu.reset();
for(int i = 0 ; i < n ; ++i){
while(qe < norm.size() && to[norm[qe]] == i)Dsu.Merge(from[norm[qe]] , to[norm[qe]]),++qe;
int tmp = Dsu.cnt;
while(qi < q.size() && cliff_at[q[qi]] == i){
int id = q[qi];
int pre = Dsu.cur();
for(auto & c : sp){
if(to[c] <= i && c <= date[id] && date[id] < out[c])Dsu.Merge(from[c],to[c]);
}
res[id] -= Dsu.cnt;
Dsu.rollback(pre);
++qi;
}
}
// cerr << norm.size() << " " << Dsu.cnt << endl;
sort(norm.begin(),norm.end(),[&](const int a , const int b){return from[a] > from[b];});
reverse(q.begin(),q.end());
qi = 0 , qe = 0;
Dsu.reset();
for(int i = n - 1 ; i >= 0 ; --i){
while(qe < norm.size() && from[norm[qe]] == i){
Dsu.Merge(from[norm[qe]] , to[norm[qe]]),++qe;
}
while(qi < q.size() && cliff_at[q[qi]] == i - 1){
int id = q[qi];
int pre = Dsu.cur();
for(auto & c : sp){
if(from[c] >= i && c <= date[id] && date[id] < out[c]){
Dsu.Merge(from[c],to[c]);
}
}
res[id] -= Dsu.cnt;
Dsu.rollback(pre);
++qi;
}
}
}
}
vector<int> simulateCollapse(int N,vector<int> T,vector<int> X,vector<int> Y,vector<int> W,vector<int> P)
{
n = N;m = T.size();q = W.size();
for(int i = 0 ; i < m ; ++i){
type[i] = T[i];
from[i] = X[i];to[i] = Y[i];
if(from[i] > to[i])swap(from[i],to[i]);
}
for(int i = 0 ; i < q ; ++i){
cliff_at[i] = P[i]; date[i] = W[i];
qList[date[i]].pb(i);
}
map<ii,int> mmap;
for(int i = 0 ; i < m ; ++i){
if(type[i] == 0){
mmap[mp(from[i],to[i])] = i;
}else{
out[mmap[mp(from[i],to[i])]] = i;
}
}
for(int i = 0 ; i < m ; ++i)if(type[i] == 0 && out[i] == 0)out[i] = m;
solve();
vector<int> res;
for(int i = 0 ; i < q ; ++i)res.pb(::res[i] + n);
return res;
}
#ifdef LOCAL
int main(int argc, char *avrgv[]) {
int N, C, qList;
freopen("A.INP","r",stdin);
freopen("A.OUT","w",stdout);
scanf("%d%d%d", &N, &C, &qList);
std::vector<int> T(C), X(C), Y(C);
for(int i = 0; i < C; i++) {
scanf("%d%d%d", &T[i], &X[i], &Y[i]);
}
std::vector<int> W(qList), P(qList);
for(int i = 0; i < qList; i++) {
scanf("%d%d", &W[i], &P[i]);
}
auto res = simulateCollapse(N, T, X, Y, W, P);
for(auto i : res) {
printf("%d\n", i);
}
}
#endif
Compilation message (stderr)
collapse.cpp: In function 'void solve()':
collapse.cpp:70:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(qe < norm.size() && to[norm[qe]] == i)Dsu.Merge(from[norm[qe]] , to[norm[qe]]),++qe;
~~~^~~~~~~~~~~~~
collapse.cpp:72:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(qi < q.size() && cliff_at[q[qi]] == i){
~~~^~~~~~~~~~
collapse.cpp:71:17: warning: unused variable 'tmp' [-Wunused-variable]
int tmp = Dsu.cnt;
^~~
collapse.cpp:89:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(qe < norm.size() && from[norm[qe]] == i){
~~~^~~~~~~~~~~~~
collapse.cpp:92:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(qi < q.size() && cliff_at[q[qi]] == i - 1){
~~~^~~~~~~~~~
# | 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... |