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 segtree{
vector<int> node;
int n;
segtree(){
}
segtree(int n_){
n=1;
while(n<n_) n*=2;
node.resize(2*n);
for(int i=0;i<2*n;i++) node[i]=10101010;
}
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]=10101010;
for(int i=0;i<n_;i++) node[i+n]=a[i];
for(int i=n-1;i>=0;i--) node[i]=min(node[2*i],node[2*i+1]);
}
void set(int x,int v){
x+=n;
node[x]=v;
while(x>1){
x/=2;
node[x]=min(node[2*x],node[2*x+1]);
}
}
int prod(int l,int r,int a,int b,int k){
if(r<=a||b<=l) return 10101010;
if(l<=a&&b<=r) return node[k];
return min(prod(l,r,a,(a+b)/2,2*k),prod(l,r,(a+b)/2,b,2*k+1));
}
int prod(int l,int r){
return prod(l,r,0,n,1);
}
};
//max
struct segtree2{
vector<int> node;
int n;
segtree2(){
}
segtree2(int n_){
n=1;
while(n<n_) n*=2;
node.resize(2*n);
for(int i=0;i<2*n;i++) node[i]=-10101010;
}
segtree2(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]=-10101010;
for(int i=0;i<n_;i++) node[i+n]=a[i];
for(int i=n-1;i>=0;i--) node[i]=max(node[2*i],node[2*i+1]);
}
void set(int x,int v){
x+=n;
node[x]=v;
while(x>1){
x/=2;
node[x]=max(node[2*x],node[2*x+1]);
}
}
int prod(int l,int r,int a,int b,int k){
if(r<=a||b<=l) return -10101010;
if(l<=a&&b<=r) return node[k];
return max(prod(l,r,a,(a+b)/2,2*k),prod(l,r,(a+b)/2,b,2*k+1));
}
int prod(int l,int r){
return prod(l,r,0,n,1);
}
};
int r[1010101],c[1010101];
segtree rsg,csg;
segtree2 rsg2,csg2;
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);
segtree2 se2(n*m,R),se4(n*m,C);
rsg=se,csg=se3,rsg2=se2,csg2=se4;
}
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]);
rsg2.set(a,r[a]);
rsg2.set(b,r[b]);
csg.set(a,c[a]);
csg.set(b,c[b]);
csg2.set(a,c[a]);
csg2.set(b,c[b]);
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;
//cout<<t<<" ";
if((rsg2.prod(0,t+1)-rsg.prod(0,t+1)+1)*(csg2.prod(0,t+1)-csg.prod(0,t+1)+1)==t+1) ans++;
}
//cout<<endl;
return ans;
}
Compilation message (stderr)
seats.cpp: In function 'int swap_seats(int, int)':
seats.cpp:122:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
122 | 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... |