이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
const int N = 1e6 + 7;
const int Inf = 1e9 + 7;
struct Node {
int mn, cnt;
Node(int x) {
mn = x;
cnt = 1;
}
Node() {
mn = Inf;
cnt = 0;
}
Node operator +(const Node &other) {
Node res;
res.mn = min(mn, other.mn);
if (res.mn == mn) res.cnt += cnt;
if (res.mn == other.mn) res.cnt += other.cnt;
return res;
}
};
Node t[4 * N];
int mod[4 * N];
void update(int v, int val) {
t[v].mn += val;
mod[v] += val;
}
void push(int v) {
if (mod[v]) {
update(2 * v + 1, mod[v]);
update(2 * v + 2, mod[v]);
mod[v] = 0;
}
}
void build(int l, int r, int v, vector <int> &a) {
if (l == r) {
t[v] = Node(a[l]);
} else {
int m = (l + r) >> 1;
build(l, m, 2 * v + 1, a);
build(m + 1, r, 2 * v + 2, a);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
void modify(int ql, int qr, int val, int l, int r, int v) {
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
update(v, val);
} else {
push(v);
int m = (l + r) >> 1;
modify(ql, qr, val, l, m, 2 * v + 1);
modify(ql, qr, val, m + 1, r, 2 * v + 2);
t[v] = t[2 * v + 1] + t[2 * v + 2];
}
}
int n;
vector <int> pos;
vector <int> p;
void give_initial_chart(int N, int M, vector<int> R, vector<int> C) {
assert(N == 1);
n = M;
pos.resize(n);
p.resize(n);
for (int i = 0; i < n; ++i) {
pos[i] = C[i];
}
for (int i = 0; i < n; ++i) p[pos[i]] = i;
vector <int> start(n);
for (int i = 0; i < n; ++i) start[i] = i;
build(0, n - 1, 0, start);
for (int i = 0; i + 1 < n; ++i) {
int id = max(p[i], p[i + 1]);
modify(id, n - 1, -1, 0, n - 1, 0);
}
}
int swap_seats(int x, int y) {
set <pair <int, int>> go;
if (pos[x] > 0) go.insert({pos[x] - 1, pos[x]});
if (pos[x] + 1 < n) go.insert({pos[x], pos[x] + 1});
if (pos[y] > 0) go.insert({pos[y] - 1, pos[y]});
if (pos[y] + 1 < n) go.insert({pos[y], pos[y] + 1});
for (auto pr : go) {
int id = max(p[pr.first], p[pr.second]);
modify(id, n - 1, 1, 0, n - 1, 0);
}
swap(p[pos[x]], p[pos[y]]);
swap(pos[x], pos[y]);
for (auto pr : go) {
int id = max(p[pr.first], p[pr.second]);
modify(id, n - 1, -1, 0, n - 1, 0);
}
return t[0].cnt;
}
#ifdef LOCAL
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n, m, q;
cin >> n >> m >> q;
vector <int> R(n * m), C(n * m);
for (int i = 0; i < n * m; ++i) {
cin >> R[i] >> C[i];
}
vector <int> aq(q), bq(q);
for (int i = 0; i < q; ++i) {
cin >> aq[i] >> bq[i];
}
give_initial_chart(n, m, R, C);
for (int j = 0; j < q; ++j) {
cout << swap_seats(aq[j], bq[j]) << '\n';
}
}
#endif
# | 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... |