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 ff first
#define ss second
#define all(x) x.begin(), x.end()
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
template<typename T>
using matrix = vector<vector<T>>;
int n, m;
vector<int> v, rev;
inline int translate(int x, int y){
return m*x+y;
}
inline pii detranslate(int id){
return {id/m,id%m};
}
int getcount(pii coord, int t){
int dx[] = {0,0,1,1}, dy[] = {0,1,0,1};
int ret = 0;
for(int i = 0; i < 4; i++){
int x = dx[i]+coord.ff, y = dy[i]+coord.ss;
if(x < n && y < m && x >=0 && y >= 0){
ret+=v[translate(x,y)] <= t;
}
}
return ret;
}
struct SegTree{
struct seg{
int mn, ct, sum;
inline seg operator+(seg b){
b.sum+=sum;
b.mn+=sum;
if(mn == b.mn)
b.ct+=ct;
if(mn < b.mn){
b.ct = ct;
b.mn = mn;
}
return b;
}
};
vector<seg> tree;
int q, val, sz;
void build(int id, int l, int r){
if(l == r){
tree[id] = {0,1,0};
return;
}
int e = id*2+1, d = id*2+2, m = (l+r)>>1;
build(e,l,m);
build(d,m+1,r);
tree[id] = tree[e]+tree[d];
}
SegTree(int n = 1){
tree.resize(4*n+4);
sz = n;
build(0,0,sz-1);
}
void update(int id, int l, int r){
if(l > q || r < q)
return;
if(q <= l && r <= q){
tree[id].mn+=val;
tree[id].sum+=val;
return;
}
int e = id*2+1, d = id*2+2, m = (l+r)>>1;
update(e,l,m);
update(d,m+1,r);
tree[id] = tree[e] + tree[d];
}
inline void makeupd(int l, int v){
val = v;
q = l;
update(0,0,sz-1);
}
} st;
void update(int x, int y, int type = 1){
if(x < 0 || x == n || y < 0 || y == m)
return;
int dx[] = {0,0,-1,-1}, dy[] = {0,-1,0,-1};
int val = v[translate(x,y)];
int delta = 0;
for(int i = 0; i < 4; i++){
int xi = x+dx[i], yi = y + dy[i];
if(getcount({xi,yi},val-1)%2)
delta-=type;
else delta+=type;
}
// cout << "update " << val << ' ' << delta << '\n';
st.makeupd(val,delta);
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
//cout << 'a' << endl;
n = H;
m = W;
v = vector<int>(n*m);
rev = vector<int>(n*m);
for(int i = 0; i < n*m; i++){
v[R[i]*m+C[i]] = i;
rev[i] = R[i]*m+C[i];
}
st = SegTree(n*m);
for(int x = 0; x < n; x++){
for(int y = 0; y < m; y++){
update(x,y);
}
}
}
int swap_seats(int a, int b) {
array<pii,2> c({detranslate(rev[a]), detranslate(rev[b])});
int dx[] = {0,0,0,1,1,1,-1,-1,-1};
int dy[] = {0,-1,1,0,-1,1,0,-1,1};
set<pii> changed;
for(int i = 0; i < 9; i ++){
for(int j = 0; j < 2; j++){
int x = c[j].ff + dx[i], y = c[j].ss + dy[i];
changed.insert({x,y});
}
}
for(auto [x,y] : changed)
update(x,y,-1);
swap(v[rev[a]],v[rev[b]]);
swap(rev[a],rev[b]);
for(auto [x,y] : changed)
update(x,y);
//cout << "-> " << st.tree[0].mn << ' ' << st.tree[0].ct << '\n';
return st.tree[0].ct;
}
# | 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... |