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>
using namespace std;
typedef long long ll;
const int INF = 1e9+100;
struct Node{
int prf, sum, cnt;
Node(){}
Node(int _prf, int _sum, int _cnt): prf(_prf), sum(_sum), cnt(_cnt) {}
Node operator + (const Node &N) const{
int tmp = sum + N.prf;
if (tmp < prf) return Node(tmp, sum + N.sum, N.cnt);
if (tmp > prf) return Node(prf, sum + N.sum, cnt);
return Node(prf, sum + N.sum, cnt + N.cnt);
}
};
struct Seg{
Node tree[2202000];
int sz;
void init(int n, int a[]){
sz = n;
for (int i=sz;i<sz*2;i++) tree[i] = Node(a[i-sz], a[i-sz], 1);
for (int i=sz-1;i;i--) tree[i] = tree[i<<1] + tree[i<<1|1];
}
void update(int p, int x){
p += sz;
if (tree[p].prf==x) return;
tree[p] = Node(x, x, 1);
for (p>>=1;p;p>>=1) tree[p] = tree[p<<1] + tree[p<<1|1];
}
Node query(int l, int r){
Node retl(INF, 0, 0), retr(INF, 0, 0);
++r;
for (l+=sz, r+=sz;l<r;l>>=1, r>>=1){
if (l&1) retl = retl + tree[l++];
if (r&1) retr = tree[--r] + retr;
}
return retl + retr;
}
}tree;
vector<vector<int>> a, chk;
int n, m, d[1001000], X[1001000], Y[1001000], dx[4] = {0, 0, 1, 1}, dy[4] = {0, 1, 0, 1};
void calc(int x, int y, int o){
pair<int, int> adj[4];
for (int k=0;k<4;k++){
adj[k] = {a[x+dx[k]][y+dy[k]], k};
}
sort(adj, adj+4);
int tmp[5] = {0};
tmp[1] = 1;
tmp[2] = (adj[0].second+adj[1].second==3)?1:0;
tmp[3] = 1;
tmp[4] = 0;
for (int i=1;i<=4;i++) if (adj[i-1].first < INF){
if (o) d[adj[i-1].first] += (tmp[i] - tmp[i-1]);
else d[adj[i-1].first] -= (tmp[i]-tmp[i-1]);
}
}
void give_initial_chart(int H, int W, std::vector<int> R, std::vector<int> C) {
n = H, m = W;
a.resize(n+2, vector<int>(m+2, INF));
chk.resize(n+2, vector<int>(m+2, 0));
for (int i=1;i<=n*m;i++){
a[R[i-1] + 1][C[i-1] + 1] = i;
X[i] = R[i-1] + 1;
Y[i] = C[i-1] + 1;
}
for (int i=0;i<=n;i++){
for (int j=0;j<=m;j++){
calc(i, j, 1);
}
}
tree.init(n*m+1, d);
//for (int i=1;i<=n*m;i++) tree.update(i, d[i]);
}
int swap_seats(int A, int B) {
++A, ++B;
int C[2] = {A, B};
for (auto p:C){
for (int i=-1;i<=0;i++){
for (int j=-1;j<=0;j++) if (!chk[X[p]+i][Y[p]+j]){
calc(X[p]+i, Y[p]+j, 0);
chk[X[p]+i][Y[p]+j] = 1;
}
}
}
swap(a[X[A]][Y[A]], a[X[B]][Y[B]]);
swap(X[A], X[B]);
swap(Y[A], Y[B]);
for (auto p:C){
for (int i=-1;i<=0;i++){
for (int j=-1;j<=0;j++) if (chk[X[p]+i][Y[p]+j]){
calc(X[p]+i, Y[p]+j, 1);
chk[X[p]+i][Y[p]+j] = 0;
}
}
}
for (auto p:C){
for (int i=-1;i<=1;i++){
for (int j=-1;j<=1;j++) if (a[X[p]+i][Y[p]+j] < INF){
tree.update(a[X[p]+i][Y[p]+j], d[a[X[p]+i][Y[p]+j]]);
}
}
}
auto N = tree.query(1, n*m);
assert(N.prf==4);
return N.cnt;
}
# | 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... |