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>
#define x first
#define y second
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair <ll,ll> pl;
typedef pair <int,int> pi;
typedef vector <int> vec;
const int INF = 1e7;
vec r,c;
int ans[1000005],n,m,rans;
vector <int> b[1000005];
pi a[1000005];
struct Tree {pi x;int cnt;}t[4000005];
pi la[4000005];
vec le;
pi pre[20000005];
Tree f(Tree l,Tree r) {
if(l.x < r.x) return l;
else if(l.x > r.x) return r;
else return {l.x,l.cnt+r.cnt};
}
void build(int x,int l,int r) {
if(l == r) {
t[x] = {{0,0},1};
return;
}
int mid = (l + r) >> 1;
build(x*2,l,mid), build(x*2+1,mid+1,r);
t[x] = f(t[x*2],t[x*2+1]);
}
void push(int x,int l,int r) {
t[x].x.x += la[x].x;
t[x].x.y += la[x].y;
if(l != r) {
la[x*2].x += la[x].x;
la[x*2].y += la[x].y;
la[x*2+1].x += la[x].x;
la[x*2+1].y += la[x].y;
}
la[x] = {0,0};
}
void update(int x,int l,int r,int rl,int rr,pi val) {
push(x,l,r);
if(rr > INF-10) return;
if(rl > r||l > rr) return;
if(rl <= l&&r <= rr) {
la[x].x += val.x;
la[x].y += val.y;
push(x,l,r);
return;
}
int mid = (l + r) >> 1;
update(x*2,l,mid,rl,rr,val), update(x*2+1,mid+1,r,rl,rr,val);
t[x] = f(t[x*2],t[x*2+1]);
}
void pro(int x,int y,int val) {
vec tmp;
tmp.push_back(b[x-1][y-1]);
tmp.push_back(b[x-1][y]);
tmp.push_back(b[x][y-1]);
tmp.push_back(b[x][y]);
sort(tmp.begin(),tmp.end());
//cout << x << ' ' << y << '\n';
//cout << tmp[0] << ' ' << tmp[1]-1 << " : " << val << '\n';
//cout << tmp[2] << ' ' << tmp[3]-1 << " : " << val << '\n';
//tmp[0] = min(max(1,tmp[0]),n*m+1);
//tmp[1] = min(n*m,tmp[1]);
//tmp[2] = min(max(1,tmp[2]),n*m+1);
//tmp[3] = min(n*m,tmp[3]);
tmp[0] = max(1,tmp[0]);
tmp[1] = min(n*m+1,tmp[1]);
tmp[2] = max(1,tmp[2]);
tmp[3] = min(n*m+1,tmp[3]);
le.push_back(tmp[0]);
le.push_back(tmp[1]);
le.push_back(tmp[2]);
le.push_back(tmp[3]);
pre[tmp[0]].x += val;
pre[tmp[1]].x -= val;
pre[tmp[2]].y += val;
pre[tmp[3]].y -= val;
//update(1,1,n*m,max(1,tmp[0]),min(n*m,tmp[1]-1),{val,0});
//update(1,1,n*m,max(1,tmp[2]),min(n*m,tmp[3]-1),{0,val});
}
void Flush() {
sort(le.begin(),le.end());
le.erase(unique(le.begin(),le.end()),le.end());
pi su = make_pair(0,0);
for(int i = 0;i < le.size()-1;i++) {
su.x += pre[le[i]].x;
su.y += pre[le[i]].y;
update(1,1,n*m,le[i],le[i+1]-1,su);
}
for(int i : le) pre[i] = make_pair(0,0);
le.clear();
}
void give_initial_chart(int H,int W,vec R,vec C) {
r = R, c = C; n = H, m = W;
for(int i = 0;i < n*m;i++) a[i+1] = {r[i]+1,c[i]+1};
for(int i = 0;i <= n+1;i++) {
b[i].resize(m+2,INF);
}
for(int i = 1;i <= n*m;i++) b[a[i].x][a[i].y] = i;
build(1,1,n*m);
for(int i = 1;i <= n+1;i++) {
for(int j = 1;j <= m+1;j++) {
pro(i,j,1);
}
}
}
int swap_seats(int A,int B) {
A++, B++;
swap(a[A],a[B]);
//debug(1,1,n*m);
//cout << '\n';
for(int i = 0;i <= 1;i++) {
for(int j = 0;j <= 1;j++) {
if(a[A].x+i >= 1&&a[A].y+j >= 1&&a[A].x+i <= n+1&&a[A].y+j <= m+1) pro(a[A].x+i,a[A].y+j,-1);
if(a[B].x+i >= 1&&a[B].y+j >= 1&&a[B].x+i <= n+1&&a[B].y+j <= m+1) pro(a[B].x+i,a[B].y+j,-1);
}
}
swap(b[a[A].x][a[A].y],b[a[B].x][a[B].y]);
for(int i = 0;i <= 1;i++) {
for(int j = 0;j <= 1;j++) {
if(a[A].x+i >= 1&&a[A].y+j >= 1&&a[A].x+i <= n+1&&a[A].y+j <= m+1) pro(a[A].x+i,a[A].y+j,1);
if(a[B].x+i >= 1&&a[B].y+j >= 1&&a[B].x+i <= n+1&&a[B].y+j <= m+1) pro(a[B].x+i,a[B].y+j,1);
}
}
Flush();
//debug(1,1,n*m);
//cout << t[1].x.x << ' ' << t[1].x.y << ' ' << t[1].cnt << '\n';
return (t[1].x == make_pair(4,0) ? t[1].cnt : 0);
}
Compilation message (stderr)
seats.cpp: In function 'void Flush()':
seats.cpp:103:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int i = 0;i < le.size()-1;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... |