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
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int n,h,w;
vector<int>r,c;
struct segtreeMin{
int n;
vector<int>d,u;
void init(vector<int>v){
n=v.size();
u=v;
d.resize(4*n);
build(1,0,n-1);
}
void build(int node,int l,int r){
if(l==r){
d[node]=u[l];
return;
}
int mid=(l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
d[node]=min(d[node*2],d[node*2+1]);
}
int query(int node,int l,int r,int L,int R){
if(L>R||l>R||L>r) return n;
if(L<=l&&r<=R){
return d[node];
}
int mid=(l+r)/2;
return min(query(node*2,l,mid,L,R),query(node*2+1,mid+1,r,L,R));
}
void update(int node,int l,int r,int pos,int val){
if(pos<l||r<pos) return;
if(l==r){
d[node]=val;
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,pos,val);
update(node*2+1,mid+1,r,pos,val);
d[node]=min(d[node*2],d[node*2+1]);
}
};
struct segtreeMax{
int n;
vector<int>d,u;
void init(vector<int>v){
n=v.size();
u=v;
d.resize(4*n);
build(1,0,n-1);
}
void build(int node,int l,int r){
if(l==r){
d[node]=u[l];
return;
}
int mid=(l+r)/2;
build(node*2,l,mid);
build(node*2+1,mid+1,r);
d[node]=max(d[node*2],d[node*2+1]);
}
int query(int node,int l,int r,int L,int R){
if(L>R||l>R||L>r) return 0;
if(L<=l&&r<=R){
return d[node];
}
int mid=(l+r)/2;
return max(query(node*2,l,mid,L,R),query(node*2+1,mid+1,r,L,R));
}
void update(int node,int l,int r,int pos,int val){
if(pos<l||r<pos) return;
if(l==r){
d[node]=val;
return;
}
int mid=(l+r)/2;
update(node*2,l,mid,pos,val);
update(node*2+1,mid+1,r,pos,val);
d[node]=max(d[node*2],d[node*2+1]);
}
};
segtreeMin sgr1,sgc1;
segtreeMax sgr2,sgc2;
void give_initial_chart(int H,int W,vector<int>R,vector<int>C){//len HW
h=H,w=W,r=R,c=C,n=H*W;
sgr1.init(r);
sgr2.init(r);
sgc1.init(c);
sgc2.init(c);
}
int swap_seats(int a,int b){
swap(r[a],r[b]);
swap(c[a],c[b]);
sgr1.update(1,0,n-1,a,r[a]);
sgr2.update(1,0,n-1,a,r[a]);
sgc1.update(1,0,n-1,a,c[a]);
sgc2.update(1,0,n-1,a,c[a]);
sgr1.update(1,0,n-1,b,r[b]);
sgr2.update(1,0,n-1,b,r[b]);
sgc1.update(1,0,n-1,b,c[b]);
sgc2.update(1,0,n-1,b,c[b]);
int ans=1;
int r1=r[0],r2=r[0],c1=c[0],c2=c[0];
for(int i=1;i<n;){
r1=min(r1,r[i]);
r2=max(r2,r[i]);
c1=min(c1,c[i]);
c2=max(c2,c[i]);
int g=(r2-r1+1)*(c2-c1+1);
if(g==i+1){
ans++;
i++;
}
else{
r1=sgr1.query(1,0,n-1,0,g-1);
r2=sgr2.query(1,0,n-1,0,g-1);
c1=sgc1.query(1,0,n-1,0,g-1);
c2=sgc2.query(1,0,n-1,0,g-1);
i=g-1;
continue;
}
}
return ans;
}
# | 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... |