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;
typedef long long ll;
typedef unsigned int ul;
typedef unsigned long long ull;
#define MASK(i) (1ULL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll gcd(ll a, ll b){return __gcd(a, b);}
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T the_array_itself, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: the_array_itself) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct SegmentTree{
int n;
vector<pair<int,int>> a;
vector<int> lazy;
SegmentTree(int _n){
n = _n;
a.resize(n * 4 + 4);
lazy.resize(n * 4 + 4);
build_tree(0, n-1, 1);
}
pair<int, int> combine(pair<int, int> a, pair<int, int> b){
pair<int, int> c = {min(a.first, b.first), 0};
if (a.first == c.first) c.second += a.second;
if (b.first == c.first) c.second += b.second;
return c;
}
void build_tree(int l, int r, int id){
if (l == r){a[id] = {0, 1}; return;}
int mid = (l + r) >> 1;
build_tree(l, mid, id * 2); build_tree(mid + 1, r, id * 2 + 1);
a[id] = combine(a[id * 2], a[id * 2 + 1]);
}
void apply(int id, int val){
a[id].first += val;
lazy[id] += val;
}
void down(int id){
apply(id * 2, lazy[id]); apply(id * 2 + 1, lazy[id]);
lazy[id] = 0;
}
void update(int u, int v, int val, int l, int r, int id){
if (u <= l && r <= v){
apply(id, val);
return;
}
down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, val, l, mid, id * 2);
if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
a[id] = combine(a[id * 2], a[id * 2 + 1]);
}
void update(int l, int r, int val){
update(l, r, val, 0, n-1, 1);
}
pair<int,int> get(){
return a[1];
}
};
const int INF = 1e9 + 69, dummy = 69420;
int h, w;
vector<int> r, c;
vector<vector<int>> a;
SegmentTree st(1);
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
h = H, w = W;
r = R; c = C;
if (h > w){
swap(h, w);
swap(r, c);
}
st = SegmentTree(h*w);
a = vector<vector<int>> (h, vector<int> (w));
for(int i = 0; i < h*w; ++i) {
a[r[i]][c[i]] = i;
}
for(int i = 0; i <= h; ++i) for(int j = 0; j <= w; ++j){
vector<int> val;
for(int x = -1; x <= 0; ++x) for(int y = -1; y <= 0; ++y){
if (i + x >= 0 && i + x < h && j + y >= 0 && j + y < w){
val.push_back(a[i+x][j+y]);
}
}
sort(ALL(val));
if (val.size() == 4){
st.update(val[2], val[3] - 1, dummy);
}
val.push_back(h * w);
st.update(val[0], val[1] - 1, 1);
}
}
void update(pair<int, int> p, int v){
for(int i = p.first; i <= p.first + 1; ++i) for(int j = p.second; j <= p.second + 1; ++j){
vector<int> val;
for(int x = -1; x <= 0; ++x) for(int y = -1; y <= 0; ++y){
if (i + x >= 0 && i + x < h && j + y >= 0 && j + y < w){
val.push_back(a[i+x][j+y]);
}
}
sort(ALL(val));
if (val.size() == 4){
st.update(val[2], val[3] - 1, dummy * v);
}
val.push_back(h * w);
st.update(val[0], val[1] - 1, v);
}
}
int swap_seats(int x, int y) {
pair<int, int> p1 = {r[x], c[x]}, p2 = {r[y], c[y]};
swap(c[x], c[y]);
swap(r[x], r[y]);
update(p1, -1);
update(p2, -1);
swap(a[p1.first][p1.second], a[p2.first][p2.second]);
update(p2, 1);
update(p1, 1);
pair<int, int> cur = st.get();
if (cur.first != 4) cur.second = 0;
return cur.second;
}
// int main(void){
// ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// clock_t start = clock();
// int n, m;cin >> n >> m;
// vector<int> r(n * m), c(n*m);
// for(int i = 0; i < n*m; ++i) cin >> r[i] >> c[i];
// give_initial_chart(n, m, r, c);
// int q; cin >> q;
// while(q--){
// int u, v; cin >> u >> v;
// cout << swap_seats(u, v) << "\n";
// }
// cerr << "Time elapsed: " << clock() - start << " ms!\n";
// return 0;
// }
# | 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... |