이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "seats.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define bit(x) (1LL << (x))
#define getbit(x, i) (((x) >> (i)) & 1)
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
mt19937_64 rd(chrono::steady_clock::now().time_since_epoch().count());
int rand(int l, int r) {
return l + rd() % (r - l + 1);
}
const int N = 1e6 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
typedef tuple <int, int, int, int> pir;
pir st[N << 2];
set <int> sr[N], sc[N];
int minx[N];
int miny[N];
int maxx[N];
int maxy[N];
int x[N];
int y[N];
int n,m,sz,ans;
bool check(int i) {
int dx = maxx[i] - minx[i] + 1;
int dy = maxy[i] - miny[i] + 1;
return (dx * dy == i + 1);
}
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
n = H;
m = W;
sz = n * m;
for (int i = 0; i < n * m; i++) {
x[i] = R[i], y[i] = C[i];
sr[x[i]].insert(i);
sc[y[i]].insert(i);
minx[i] = maxx[i] = x[i];
miny[i] = miny[i] = y[i];
}
for (int i = 0; i < n * m; i++) {
if (i > 0) {
mini(minx[i], minx[i - 1]);
mini(miny[i], miny[i - 1]);
maxi(maxx[i], maxx[i - 1]);
maxi(maxy[i], maxy[i - 1]);
}
ans += check(i);
}
}
int brute() {
int ans = 0;
int minx = oo, miny = oo, maxx = -oo, maxy = -oo;
for (int i = 0; i < n * m; i++) {
mini(minx, x[i]);
maxi(maxx, x[i]);
mini(miny, y[i]);
maxi(maxy, y[i]);
int dx = maxx - minx + 1;
int dy = maxy - miny + 1;
ans += (dx * dy == i + 1);
}
return ans;
}
int sub2() {
vector <int> events;
for (int i = 0; i < n; i++)
events.push_back(*sr[i].begin());
for (int i = 0; i < m; i++)
events.push_back(*sc[i].begin());
sort(ALL(events));
int minx = oo, maxx = -oo, miny = oo, maxy = -oo;
int res = 0;
for (int it = 0; it < (int) events.size(); it++) {
if (it + 1 == (int) events.size()) {
res++;
break;
}
int i = events[it];
int j = events[it + 1];
mini(minx, x[i]);
mini(miny, y[i]);
maxi(maxx, x[i]);
maxi(maxy, y[i]);
int t = (maxx - minx + 1) * (maxy - miny + 1);
res += (i <= t && t <= j);
}
return res;
}
int sub3(int l, int r) {
if (l > r)
swap(l, r);
for (int i = l; i <= r; i++) {
ans -= check(i);
minx[i] = maxx[i] = x[i];
miny[i] = maxy[i] = y[i];
if (i > 0) {
mini(minx[i], minx[i - 1]);
mini(miny[i], miny[i - 1]);
maxi(maxx[i], maxx[i - 1]);
maxi(maxy[i], maxy[i - 1]);
}
ans += check(i);
}
return ans;
}
int swap_seats(int a, int b) {
sr[x[a]].erase(a);
sr[x[b]].erase(b);
sc[y[a]].erase(a);
sc[y[b]].erase(b);
swap(x[a], x[b]);
swap(y[a], y[b]);
sr[x[a]].insert(a);
sr[x[b]].insert(b);
sc[y[a]].insert(a);
sc[y[b]].insert(b);
if (n * m <= 10000)
return brute();
if (max(n, m) <= 1000)
return sub2();
if (abs(a - b) <= 10000)
return sub3(a, b);
return -1;
}
//#include "grader.cpp"
컴파일 시 표준 에러 (stderr) 메시지
seats.cpp: In function 'void give_initial_chart(int, int, std::vector<int>, std::vector<int>)':
seats.cpp:57:17: warning: operation on 'miny[i]' may be undefined [-Wsequence-point]
57 | miny[i] = miny[i] = y[i];
| ~~~~~~~~^~~~~~~~~~~~~~~~
# | 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... |