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;
struct nd{
int mx,mn;
nd(){
mx=0;
mn=10000;
}
nd(int a,int b){
mx=a,mn=b;
}
};
nd merge(nd a,nd b){
return nd(max(a.mx,b.mx),min(a.mn,b.mn));
}
struct segtree{
vector<nd> node;
int n;
segtree(){
}
segtree(int n_,vector<int> a){
n=1;
while(n<n_) n*=2;
node.resize(2*n);
for(int i=0;i<2*n;i++) node[i]=nd();
for(int i=0;i<n_;i++) node[i+n]=nd(a[i],a[i]);
for(int i=n-1;i>=0;i--) node[i]=merge(node[2*i],node[2*i+1]);
}
void set(int x,int v){
x+=n;
node[x]=nd(v,v);
while(x>1){
x/=2;
node[x]=merge(node[2*x],node[2*x+1]);
}
}
nd prod(int l,int r,int a,int b,int k){
if(r<=a||b<=l) return nd();
if(l<=a&&b<=r) return node[k];
return merge(prod(l,r,a,(a+b)/2,2*k),prod(l,r,(a+b)/2,b,2*k+1));
}
nd prod(int l,int r){
return prod(l,r,0,n,1);
}
};
int r[1010101],c[1010101];
segtree rsg,csg;
int n,m;
int ans=0;
set<int> rs[1010],cs[1010];
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
n=H,m=W;
for(int i=0;i<n*m;i++){
r[i]=R[i];
c[i]=C[i];
rs[r[i]].insert(i);
cs[c[i]].insert(i);
}
segtree se(n*m,R),se3(n*m,C);
rsg=se,csg=se3;
}
int swap_seats(int a, int b) {
int ans=0;
rs[r[a]].erase(a);
cs[c[a]].erase(a);
rs[r[b]].erase(b);
cs[c[b]].erase(b);
swap(r[a],r[b]);
swap(c[a],c[b]);
rs[r[a]].insert(a);
rs[r[b]].insert(b);
cs[c[a]].insert(a);
cs[c[b]].insert(b);
rsg.set(a,r[a]);
rsg.set(b,r[b]);
csg.set(a,c[a]);
csg.set(b,c[b]);
set<int> idx;
vector<int> w;
for(int i=0;i<n;i++) w.push_back(*rs[i].begin());
for(int i=0;i<m;i++) w.push_back(*cs[i].begin());
w.push_back(n*m);
sort(w.begin(),w.end());
for(int i=0;i<w.size();i++){
if(i&&w[i]==w[i-1]) continue;
int t=w[i]-1;
if(t<0) continue;
nd p=rsg.prod(0,t+1),q=csg.prod(0,t+1);
if((p.mx-p.mn+1)*(q.mx-q.mn+1)==t+1) ans++;
}
return ans;
}
Compilation message (stderr)
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:86:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(int i=0;i<w.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... |