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 "seats.h"
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,int(v.size())) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first
const int N=1e6+99,dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int n,m,t,lz[N<<2];
vector<int> a[N];
pair<int,int> seg[N<<2];
vector<int> A,B;
pair<int,int> operator + (pair<int,int> A,pair<int,int> B){
if(A.F==B.F) A.S+=B.S;
if(A.F<=B.F) return A;
return B;
}
void upd(int id){
seg[id]=seg[id<<1]+seg[id<<1|1];
}
void shift(int id){
lz[id<<1]+=lz[id];
lz[id<<1|1]+=lz[id];
seg[id<<1].F+=lz[id];
seg[id<<1|1].F+=lz[id];
lz[id]=0;
}
void debug(int id=1,int l=0,int r=t){
if(l+1==r){
cout<<l<<" -> "<<seg[id].F<<" "<<seg[id].S<<endl;
return ;
}
int mid=(l+r)>>1;
shift(id);
debug(id<<1,l,mid);
debug(id<<1|1,mid,r);
upd(id);
}
void build(int id=1,int l=0,int r=t){
if(l+1==r){
seg[id]=mp(-(r)+(l==0),1);
return ;
}
int mid=(l+r)>>1;
build(id<<1,l,mid);
build(id<<1|1,mid,r);
upd(id);
}
void add(int L,int R,int val,int id=1,int l=0,int r=t){
if(r<=L || R<=l) return ;
if(L<=l && r<=R){
lz[id]+=val;
seg[id].F+=val;
return ;
}
int mid=(l+r)>>1;
shift(id);
add(L,R,val,id<<1,l,mid);
add(L,R,val,id<<1|1,mid,r);
upd(id);
}
void Add(int l,int r,int val){
if(l==r) return ;
if(r<l) swap(l,r);
add(l,r,val);
}
void do_it(int x,int y,int p){
vector<pair<int,int>> vec;
f(i,0,4){
int u=x+dx[i],v=y+dy[i];
vec.pb({a[u][v],i});
}
sort(all(vec));
int l=max(a[x][y],vec[1].F),r=vec[2].F;
if(l<r && (vec[0].S^2)==vec[1].S){
Add(l,r,p);
}
}
void updcell(int x,int y,int val){
//cout<<x<<" "<<y<<endl;
f(i,0,4){
int u=x+dx[i],v=y+dy[i];
Add(a[x][y],a[u][v],-1);
}
a[x][y]=val;
f(i,0,4){
int u=x+dx[i],v=y+dy[i];
Add(a[x][y],a[u][v],+1);
}
}
void give_initial_chart(int _n, int _m,vector<int> _A,vector<int> _B) {
n=_n,m=_m;
A=_A,B=_B; t=n*m;
f(i,0,t) A[i]++,B[i]++;
vector<int> P(m+2,t);
f(i,0,n+2) a[i]=P;
f(i,0,t) a[A[i]][B[i]]=i;
build();
//debug();
f(x,1,n+1){
f(y,1,m+1){
f(i,0,4){
int u=x+dx[i],v=y+dy[i];
//cout<<a[x][y]<<" -> "<<a[u][v]<<endl;
if(a[x][y]<a[u][v]){
Add(a[x][y],a[u][v],1);
}
}
}
}
f(x,1,n+1){
f(y,1,m+1){
do_it(x,y,-1);
}
}
//debug();
//erorp(seg[1]);
cout<<"ans -> "<<seg[1].S*(seg[1].F==4)<<endl;
}
int swap_seats(int u, int v) {
//eror(A[u]);
//eror(A[v]);
set<pair<int,int>> s;
s.insert({A[u],B[u]});
s.insert({A[v],B[v]});
f(i,0,4){
int x=A[u]+dx[i],y=B[u]+dy[i];
if(1<=x && x<=n && 1<=y && y<=m) s.insert({x,y});
}
f(i,0,4){
int x=A[v]+dx[i],y=B[v]+dy[i];
if(1<=x && x<=n && 1<=y && y<=m) s.insert({x,y});
}
for(auto [x,y] : s){
do_it(x,y,+1);
}
updcell(A[u],B[u],v);
updcell(A[v],B[v],u);
swap(A[u],A[v]);
swap(B[u],B[v]);
for(auto [x,y] : s){
do_it(x,y,-1);
}
//
build();
//debug();
f(x,1,n+1){
f(y,1,m+1){
f(i,0,4){
int u=x+dx[i],v=y+dy[i];
//cout<<a[x][y]<<" -> "<<a[u][v]<<endl;
if(a[x][y]<a[u][v]){
Add(a[x][y],a[u][v],1);
}
}
}
}
f(x,1,n+1){
f(y,1,m+1){
do_it(x,y,-1);
}
}
//
return seg[1].S*(seg[1].F==4);
}
/*
1 3 3
0 0
0 1
0 2
0 2
1 2
0 1
1 3 1
0 2
0 1
0 0
0 0
1 4 0
0 3
0 2
0 1
0 0
1 4 4
0 0
0 1
0 2
0 3
0 1
0 3
1 3
1 2
*/
Compilation message (stderr)
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:151:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
151 | for(auto [x,y] : s){
| ^
seats.cpp:158:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
158 | for(auto [x,y] : s){
| ^
# | 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... |