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>
#include "seats.h"
using namespace std;
#define ll long long
#define pb push_back
#include <cstdio>
#include <cstdlib>
#include <vector>
const ll inf=1e9;
struct node{
ll val=0,freq=0;
node operator+(node n){
if(val==n.val) return {val,freq+n.freq};
if(val<n.val) return {val,freq};
return {n.val,n.freq};
}
};
struct SEG{
vector<node>seg;
vector<ll>lz;
void init(ll n){
seg.resize(4*n+5);
lz.resize(4*n+5,0);
}
void build(ll l, ll r, ll id){
seg[id].freq=r-l+1;
if(l==r)return;
ll mid=(l+r)>>1;
build(l,mid,id*2),build(mid+1,r,id*2+1);
}
void push(ll l, ll r, ll id){
seg[id].val+=lz[id];
if(l!=r){
for(int i=0;i<2;i++){
lz[id*2+i]+=lz[id];
}
}
lz[id]=0;
}
void upd(ll ul, ll ur, ll l, ll r, ll val, ll id){
push(l,r,id);
if(ul>ur)return;
if(l>ur||r<ul){
return;
}
if(l>=ul&&r<=ur){
lz[id]=val;
push(l,r,id);
return;
}
ll mid=(l+r)>>1;
upd(ul,ur,l,mid,val,id*2),upd(ul,ur,mid+1,r,val,id*2+1);
seg[id]=seg[id*2]+seg[id*2+1];
}
ll ans(){
if(seg[1].val==4)return seg[1].freq;
return 0;
}
void output(ll l, ll r, ll id){
cout<<l<<" "<<r<<" "<<id<<" "<<seg[id].val<<" "<<seg[id].freq<<'\n';
if(l==r){
return;
}
ll mid=(l+r)>>1;
output(l,mid,id*2),output(mid+1,r,id*2+1);
}
}st;
int h,w,n;
vector<int>r,c;
vector<vector<int> >grid;
vector<pair<int,int> >pos;
int get(int rr, int cc){
if(rr==-1||cc==-1||rr==h||cc==w)return n;
return grid[rr][cc];
}
void upd(vector<int>v, ll val){
st.upd(v[0]+1,v[1],1,n,val,1);
st.upd(v[2]+1,v[3],1,n,val*inf,1);
}
void change(ll i, ll j, ll val){
vector<int>v;
v.pb(get(i-1,j-1));
v.pb(get(i-1,j));
v.pb(get(i,j-1));
v.pb(get(i,j));
sort(v.begin(),v.end());
upd(v,val);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C){
h=H,w=W,r=R,c=C;
n=h*w;
grid.resize(h+5);
pos.resize(n+5);
st.init(n);
st.build(1,n,1);
for(int i=0;i<h+5;i++){
grid[i].resize(w+5);
}
for(int i=0;i<n;i++){
grid[r[i]][c[i]]=i;
pos[i]={r[i],c[i]};
}
for(int i=0;i<=h;i++){
for(int j=0;j<=w;j++){
change(i,j,1);
}
}
}
int swap_seats(int a, int b){
pair<int,int>one=pos[a],two=pos[b];
vector<pair<int,int> >ppp;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
ppp.pb({one.first+i,one.second+j});
ppp.pb({two.first+i,two.second+j});
}
}
sort(ppp.begin(),ppp.end());
ppp.erase(unique(ppp.begin(),ppp.end()),ppp.end());
for(int i=0;i<ppp.size();i++){
change(ppp[i].first,ppp[i].second,-1);
}
swap(grid[one.first][one.second],grid[two.first][two.second]);
r[a]=two.first,c[a]=two.second;
r[b]=one.first,c[b]=one.second;
swap(pos[a],pos[b]);
for(int i=0;i<ppp.size();i++){
change(ppp[i].first,ppp[i].second,1);
}
return st.ans();
}
Compilation message (stderr)
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:123:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int i=0;i<ppp.size();i++){
| ~^~~~~~~~~~~
seats.cpp:130:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
130 | for(int i=0;i<ppp.size();i++){
| ~^~~~~~~~~~~
# | 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... |