This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Challenge: Accepted
#include <bits/stdc++.h>
#include "seats.h"
#pragma GCC optimize("Ofast")
using namespace std;
#ifdef zisk
void debug(){cout << endl;}
template<class T, class ... U> void debug(T a, U ... b){cout << a << " ", debug(b...);}
template<class T> void pary(T l, T r) {
while (l != r) cout << *l << " ", l++;
cout << endl;
}
#else
#define debug(...) 0
#define pary(...) 0
#endif
#define ll long long
#define maxn 1000005
#define mod 1000000007
#define pii pair<int, int>
#define ff first
#define ss second
#define io ios_base::sync_with_stdio(0);cin.tie(0);
struct segtree{
int seg[4*maxn], cnt[4*maxn], tag[4*maxn];
void push(int cur, int l, int r) {
seg[cur] += tag[cur];
if (r - l > 1) {
tag[cur*2] += tag[cur];
tag[cur*2+1] += tag[cur];
}
tag[cur] = 0;
}
void pull(int cur, int l, int r) {
int m = (l + r) / 2;
push(cur*2, l, m), push(cur*2+1, m, r);
seg[cur] = min(seg[cur*2], seg[cur*2+1]);
cnt[cur] = (seg[cur*2] == seg[cur] ? cnt[cur*2] : 0) + (seg[cur*2+1] == seg[cur] ? cnt[cur*2+1] : 0);
}
void init(int cur, int l, int r) {
if (r <= l) return;
cnt[cur] = r - l;
seg[cur] = tag[cur] = 0;
if (r - l == 1) return;
int m = (l + r) / 2;
init(cur*2, l, m), init(cur*2+1, m, r);
}
void modify(int cur, int l, int r, int ql, int qr, int v) {
if (r <= l || ql >= r || qr <= l) return;
push(cur, l, r);
if (ql <= l && qr >= r) {
tag[cur] += v;
return;
}
int m = (l + r) / 2;
modify(cur*2, l, m, ql, qr, v);
modify(cur*2+1, m, r, ql, qr, v);
pull(cur, l, r);
}
int getval(int n) {
push(1, 0, n);
if (seg[1] == 4) return cnt[1];
else return 0;
}
} tr;
vector<int> a, pos;
int n;
int m;
void upd(int x, int y, int v) {
int tmp[4] = {a[x*m+y], a[x*m+y+1], a[(x+1)*m+y], a[(x+1)*m+y+1]};
sort(tmp, tmp+4);
tr.modify(1, 0, n, tmp[0], tmp[1], v);
tr.modify(1, 0, n, tmp[2], tmp[3], v);
}
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
m = W+2;
n = H*W;
tr.init(1, 0, n);
a.resize((H+2)*(W+2), maxn);
pos.resize(n);
for (int i = 0;i < n;i++) {
R[i]++, C[i]++;
pos[i] = R[i] * m + C[i];
a[R[i] * m + C[i]] = i;
}
for (int i = 0;i <= H;i++) {
for (int j = 0;j <= W;j++) {
upd(i, j, 1);
}
}
}
int swap_seats(int u, int v) {
int ax = pos[u] / m, ay = pos[u] % m;
int bx = pos[v] / m, by = pos[v] % m;
for (int i = -1;i <= 0;i++) {
for (int j = -1;j <= 0;j++) {
upd(ax+i, ay+j, -1);
upd(bx+i, by+j, -1);
}
}
swap(a[pos[u]], a[pos[v]]);
for (int i = -1;i <= 0;i++) {
for (int j = -1;j <= 0;j++) {
upd(ax+i, ay+j, 1);
upd(bx+i, by+j, 1);
}
}
swap(pos[u], pos[v]);
return tr.getval(n);
}
/*
2 3 2
0 0
1 0
1 1
0 1
0 2
1 2
0 5
0 5
*/
# | 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... |