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"
std::pair<int,int> DR[] = {{-1,0},{0,1},{1,0},{0,-1},{-1,1},{-1,-1},{1,1},{1,-1}};
# define ll long long
# define clock (clock() * 1000.0 / CLOCKS_PER_SEC)
# define rc(s) return cout << s,0
# define rcg(s) cout << s;exit(0)
# define _ ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
# define db(x) cerr << #x << " = " << x << '\n'
# define pb push_back
# define mp make_pair
# define all(s) s.begin(),s.end()
# define sz(x) (int)((x).size())
using namespace std;
int n,m,q,r[1 << 20],c[1 << 20],b[1 << 20],A,B,mnr[1 << 20],mnc[1 << 20],mxr[1 << 20],mxc[1 << 20];
int ans = 0;
set<int>posok;
int t[2][1 << 22][2];
set<int>s1[1 << 20],s2[1 << 20];
void upd1(int nod,int l,int r,int pos,int val,int op)
{
if(l == r)
{
t[0][nod][op] = val;
return ;
}
int mid = l + r >> 1;
if(pos <= mid) upd1(nod << 1,l,mid,pos,val,op);
else upd1(nod << 1 | 1,mid + 1,r,pos,val,op);
t[0][nod][op] = min(t[0][nod << 1][op],t[0][nod << 1 | 1][op]);
}
void upd2(int nod,int l,int r,int pos,int val,int op)
{
if(l == r)
{
t[1][nod][op] = val;
return ;
}
int mid = l + r >> 1;
if(pos <= mid) upd2(nod << 1,l,mid,pos,val,op);
else upd2(nod << 1 | 1,mid + 1,r,pos,val,op);
t[1][nod][op] = max(t[1][nod << 1][op],t[1][nod << 1 | 1][op]);
}
int qry1(int nod,int tl,int tr,int l,int r,int op)
{
if(l > r) return 1e9;
if(tl == l && tr == r) return t[0][nod][op];
int mid = tl + tr >> 1;
return min(qry1(nod << 1,tl,mid,l,min(mid,r),op),qry1(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r,op));
}
int qry2(int nod,int tl,int tr,int l,int r,int op)
{
if(l > r) return 0;
if(tl == l && tr == r) return t[1][nod][op];
int mid = tl + tr >> 1;
return max(qry2(nod << 1,tl,mid,l,min(mid,r),op),qry2(nod << 1 | 1,mid + 1,tr,max(mid + 1,l),r,op));
}
void add1(int idx, int val)
{
s1[val].insert(idx);
upd1(1,1,n * m,idx,val,0);
upd2(1,1,n * m,idx,val,0);
}
void add2(int idx,int val)
{
s2[val].insert(idx);
upd1(1,1,n * m,idx,val,1);
upd2(1,1,n * m,idx,val,1);
}
void rem1(int idx, int val)
{
s1[val].erase(idx);
upd1(1,1,n * m,idx,1e9,0);
upd2(1,1,n * m,idx,0,0);
}
void rem2(int idx, int val)
{
s2[val].erase(idx);
upd1(1,1,n * m,idx,1e9,1);
upd2(1,1,n * m,idx,0,1);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
n = H;
m = W;
posok.clear();
for(int i = 1;i <= 4 * n * m;i++) t[0][i][0] = t[0][i][1] = 1e9;
for(int i = 1;i <= 4 * n * m;i++) t[1][i][0] = t[1][i][1] = 0;
for(int i = 1;i <= n * m;i++)
{
s1[i].clear();
s2[i].clear();
}
for(int i = 1;i <= n * m;i++)
{
r[i] = R[i - 1] + 1;
c[i] = C[i - 1] + 1;
add1(i,r[i]);add2(i,c[i]);
}
int mn1 = 1e9,mn2 = 1e9,mx1 = 0,mx2 = 0;
for(int i = 1;i <= n * m;i++)
{
mn1 = min(mn1,r[i]);
mx1 = max(mx1,r[i]);
mn2 = min(mn2,c[i]);
mx2 = max(mx2,c[i]);
if((mn2 - mn1 + 1) * (mx2 - mx1 + 1) == i){
ans++;
posok.insert(i);
}
}
}
int swap_seats(int A, int B) {
int x = A,y = B;
x++;
y++;
if(x > y) swap(x,y);
rem1(x,r[x]);rem2(x,c[x]);
rem1(y,r[y]);rem2(y,c[y]);
swap(r[x],r[y]);
swap(c[x],c[y]);
add1(x,r[x]);add2(x,c[x]);
add1(y,r[y]);add2(y,c[y]);
// hz
vector<int>toerase;
for(auto it : posok)
{
if(it >= x && it <= y)
{
ans--;
toerase.pb(it);
}
}
for(auto it : toerase) posok.erase(posok.find(it));
// hz
set<int>vals;
vals.insert(x);
vals.insert(y);
for(int i = 1;i <= max(n,m);i++)
{
if(sz(s1[i]) && *(s1[i].begin()) >= x && *(s1[i].begin()) <= y) vals.insert(*(s1[i].begin()));
if(sz(s2[i]) && *(s2[i].begin()) >= x && *(s2[i].begin()) <= y) vals.insert(*(s2[i].begin()));
}
vector<int>vec;
for(auto it : vals) vec.pb(it);
vec.pb(y + 1);
int mn1 = 1e9,mn2 = 1e9,mx1 = 0,mx2 = 0;
mn1 = qry1(1,1,n * m,1,x,0);
mn2 = qry1(1,1,n * m,1,x,1);
mx1 = qry2(1,1,n * m,1,x,0);
mx2 = qry2(1,1,n * m,1,x,1);
for(int i = 0;i < sz(vec) - 1;i++)
{
mn1 = min(mn1, r[vec[i]]);
mn2 = min(mn2, c[vec[i]]);
mx1 = max(mx1, r[vec[i]]);
mx2 = max(mx2, c[vec[i]]);
int k = (mx1 - mn1 + 1) * (mx2 - mn2 + 1);
if(k >= vec[i] && k < vec[i + 1])
{
ans++;
posok.insert(k);
}
}
return ans;
}
/*
int32_t main(){
vector<int>R = {0,0,0,0,0};
vector<int>C = {0,1,2,3,4};
give_initial_chart(1,5,R,C);
cout << swap_seats(0,1) << '\n';
cout << swap_seats(0,3) << '\n';
cout << swap_seats(4,0) << '\n';
}*/
Compilation message (stderr)
seats.cpp: In function 'void upd1(int, int, int, int, int, int)':
seats.cpp:29:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
seats.cpp: In function 'void upd2(int, int, int, int, int, int)':
seats.cpp:42:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
seats.cpp: In function 'int qry1(int, int, int, int, int, int)':
seats.cpp:52:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
seats.cpp: In function 'int qry2(int, int, int, int, int, int)':
seats.cpp:60:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = tl + tr >> 1;
~~~^~~~
# | 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... |