题解:P9358 [ICPC 2022 Xi'an R] Bridge
vp 时的做法,可能有点魔怔。以下认为题目描述了
考虑若所有修改在询问之前怎么做。对列
修改询问交错时考虑操作分块,对于一块,设其中的修改操作为
时间复杂度
#include<bits/stdc++.h>
// #define int long long
#define fi first
#define se second
#define pii std::pair<int,int>
#define vint std::vector<int>
#define vpair std::vector<pii>
#define all(x) (x).begin(),(x).end()
#define SZ(x) (x).size()
#define debug(...) fprintf(stderr,##__VA_ARGS__)
template<typename T>
void read(T &x){
x=0;
int f=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') f=-1;
c=getchar();
}
while(c>='0'&&c<='9') x=x*10+(int)(c-'0'),c=getchar();
x*=f;
}
std::stack<char>st;
template<typename T>
void print(T x){
if(x==0) putchar('0');
if(x<0) putchar('-'),x=-x;
while(st.size()) st.pop();
while(x) st.push((char)('0'+x%10)),x/=10;
while(st.size()) putchar(st.top()),st.pop();
}
template<typename T>
void printsp(T x){
print(x),putchar(' ');
}
template<typename T>
void println(T x){
print(x),putchar('\n');
}
template<typename T,typename I>
bool chkmin(T &a,I b){
if(a>b) return a=b,1;
return 0;
}
template<typename T,typename I>
bool chkmax(T &a,I b){
if(a<b) return a=b,1;
return 0;
}
template<typename T,typename I>
void addedge(std::vector<I>*vec,T u,T v){
vec[u].push_back(v);
}
template<typename T,typename I,typename K>
void addedge(std::vector<K>*vec,T u,T v,I w){
vec[u].push_back({v,w});
}
template<typename T,typename I>
void addd(std::vector<I>*vec,T u,T v){
addedge(vec,u,v),addedge(vec,v,u);
}
template<typename T,typename I,typename K>
void addd(std::vector<K>*vec,T u,T v,I w){
addedge(vec,u,v,w),addedge(vec,v,u,w);
}
bool Mbe;
const int inf=1e9,MOD1=998244353,MOD2=1e9+7;
const int maxn=2e5+10,B=320;
int n,m,q,a[maxn],b[maxn],op[maxn],bel[maxn],nw[maxn],ti[maxn][B],tim,go[maxn][B];
int blktim[B],ans[maxn];
vpair v,nv;
bool Men;
signed main(){
debug("%.6lfMB\n",(&Mbe-&Men)/1048576.0);
read(n),read(m),read(q);
for(int i=1;i<=q;i++){
read(op[i]),read(a[i]);
if(op[i]==1) read(b[i]);
}
a[0]=inf;
for(int l=1;l<=q;l++){
int r=std::min(q,l+B-1);
vpair vec;
vec.push_back({1,0});
for(int i=l;i<=r;i++)
if(op[i]==1) vec.push_back({b[i],i});
std::sort(all(vec));
int z=0,id=0;
for(int i=1;i<=m+1;i++){
if(z<vec.size()&&vec[z].fi<=i) id++;
while(z<vec.size()&&vec[z].fi<=i) z++;
bel[i]=id;
}
tim++;
z=v.size()-1;
for(int i=m+1;i>=1;i--){
if(bel[i]!=bel[i+1]) tim++,blktim[bel[i]]=tim;
while(z>=0&&v[z].fi==i){
int x=go[a[v[z].se]][bel[i]],y=go[a[v[z].se]+1][bel[i]];
if(ti[a[v[z].se]][bel[i]]!=tim) x=a[v[z].se];
if(ti[a[v[z].se]+1][bel[i]]!=tim) y=a[v[z].se]+1;
go[a[v[z].se]][bel[i]]=y,go[a[v[z].se]+1][bel[i]]=x;
ti[a[v[z].se]][bel[i]]=ti[a[v[z].se]+1][bel[i]]=tim;
z--;
}
}
for(int i=l;i<=r;i++){
if(op[i]==1) continue;
z=0;
int now=a[i];
for(int j=1;j<=id;j++){
int p=vec[z].fi;
while(z<vec.size()&&vec[z].fi==p){
if(vec[z].se>i){
z++;
continue;
}
if(now==a[vec[z].se]) now++;
else if(now==a[vec[z].se]+1) now--;
z++;
}
if(ti[now][j]==blktim[j]) now=go[now][j];
}
println(now);
}
vpair nv;
int z1=1,z2=0;
while(z1<vec.size()&&z2<v.size()){
if(vec[z1].fi<v[z2].fi) nv.push_back(vec[z1]),z1++;
else nv.push_back(v[z2]),z2++;
}
while(z1<vec.size()) nv.push_back(vec[z1]),z1++;
while(z2<v.size()) nv.push_back(v[z2]),z2++;
nv.swap(v);
l=r;
}
debug("%.6lfms\n",1e3*clock()/CLOCKS_PER_SEC);
}
/*
3 4 13
2 2
1 1 3
2 1
2 2
2 3
1 2 4
2 1
2 2
2 3
1 2 1
2 1
2 2
2 3
*/