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>
#define MAXN 1000007
using namespace std;
int n,m;
int st[MAXN],pos[MAXN];
pair<int,int> tree[4*MAXN];
int lazy[MAXN];
void psh(int v){
tree[2*v].first+=lazy[v];
lazy[2*v]+=lazy[v];
tree[2*v+1].first+=lazy[v];
lazy[2*v+1]+=lazy[v];
lazy[v]=0;
}
pair<int,int> combine(pair<int,int> fr,pair<int,int> sc){
if(fr.first<sc.first)return fr;
if(fr.first>sc.first)return sc;
return {fr.first,fr.second+sc.second};
}
void update(int v,int l,int r,int ll,int rr,int val){
if(ll>rr)return;
if(l==ll and r==rr){
tree[v].first+=val;
lazy[v]+=val;
}else{
psh(v);
int tt=(l+r)/2;
update(2*v,l,tt,ll,min(tt,rr),val);
update(2*v+1,tt+1,r,max(tt+1,ll),rr,val);
tree[v]=combine(tree[2*v],tree[2*v+1]);
}
}
void give_initial_chart(int H, int W,vector<int> R,vector<int> C){
n=H; m=W;
for(int i=1;i<=m;i++){
st[C[i-1]+1]=i;
pos[i-1]=C[i-1]+1;
}
st[0]=st[m+1]=m+1;
for(int i=1;i<=4*m;i++){
tree[i].second=1;
}
for(int i=0;i<=m;i++){
update(1,1,m,min(st[i],st[i+1]),max(st[i],st[i+1])-1,1);
}
}
int swap_seats(int a, int b){
update(1,1,m,min(st[pos[a]-1],st[pos[a]]),max(st[pos[a]-1],st[pos[a]])-1,-1);
update(1,1,m,min(st[pos[a]],st[pos[a]+1]),max(st[pos[a]],st[pos[a]+1])-1,-1);
update(1,1,m,min(st[pos[b]-1],st[pos[b]]),max(st[pos[b]-1],st[pos[b]])-1,-1);
update(1,1,m,min(st[pos[b]],st[pos[b]+1]),max(st[pos[b]],st[pos[b]+1])-1,-1);
swap(st[pos[a]],st[pos[b]]); swap(pos[a],pos[b]);
update(1,1,m,min(st[pos[a]-1],st[pos[a]]),max(st[pos[a]-1],st[pos[a]])-1,1);
update(1,1,m,min(st[pos[a]],st[pos[a]+1]),max(st[pos[a]],st[pos[a]+1])-1,1);
update(1,1,m,min(st[pos[b]-1],st[pos[b]]),max(st[pos[b]-1],st[pos[b]])-1,1);
update(1,1,m,min(st[pos[b]],st[pos[b]+1]),max(st[pos[b]],st[pos[b]+1])-1,1);
return tree[1].second;
}
/*
int main(){
give_initial_chart(1,6,{0,0,0,0,0,0},{0,1,2,3,4,5});
cout<<swap_seats(0,2)<<"\n";
cout<<swap_seats(0,5)<<"\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... |