제출 #991161

#제출 시각아이디문제언어결과실행 시간메모리
991161Sharky자리 배치 (IOI18_seats)C++17
33 / 100
2354 ms108148 KiB
// #include "seats.h" #include <bits/stdc++.h> using namespace std; #define i32 int32_t void give_initial_chart(i32 H, i32 W, vector<i32> R, vector<i32> C); i32 swap_seats(i32 a, i32 b); struct Node { int val, freq; Node operator + (Node o) { if (val == o.val) return {val, freq + o.freq}; if (val < o.val) return {val, freq}; if (val > o.val) return {o.val, o.freq}; } }; const int inf = 1e9; struct SegTree { int size = 1; vector<Node> seg; vector<int> lazy; void init(int n) { while (size < n) size *= 2; seg.resize(2 * size + 10); lazy.assign(2 * size + 10, 0); } void build(int l, int r, int id) { if (l == r) { seg[id] = {inf, 1}; return; } int mid = (l + r) / 2; build(l, mid, id * 2); build(mid + 1, r, id * 2 + 1); seg[id] = seg[id * 2] + seg[id * 2 + 1]; } void push(int id) { if (seg[id].val == inf) seg[id].val = lazy[id]; else seg[id].val += lazy[id]; if (id < size) { for (int i = 0; i < 2; i++) { lazy[id * 2 + i] += lazy[id]; } } lazy[id] = 0; } void update(int ql, int qr, int tos, int l, int r, int id) { if (ql > qr) return; push(id); if (ql <= l && r <= qr) { lazy[id] += tos; push(id); return; } if (qr < l || r < ql) return; int mid = (l + r) / 2; update(ql, qr, tos, l, mid, id * 2); update(ql, qr, tos, mid + 1, r, id * 2 + 1); seg[id] = seg[id * 2] + seg[id * 2 + 1]; } Node query(int ql, int qr, int l, int r, int id) { push(id); if (ql <= l && r <= qr) return seg[id]; if (qr < l || r < ql) return {inf, 1}; int mid = (l + r) / 2; return query(ql, qr, l, mid, id * 2) + query(ql, qr, mid + 1, r, id * 2 + 1); } } st; int n, h, w; vector<int> rr, cc; vector<vector<int>> g; int I(int i, int j) { return i * w + j + 1; } int query() { Node node = st.query(1, h * w, 1, n, 1); return (node.val == 4 ? node.freq : 0); } void give_initial_chart(i32 H, i32 W, vector<i32> R, vector<i32> C) { h = H, w = W, n = (h + 1) * (w + 1) + 1; st.init(n + 1); // cout << n + 1 << '\n'; st.build(1, n, 1); g = vector<vector<int>> (h + 2, vector<int> (w + 2, n + 1)); rr.resize(n), cc.resize(n); for (int i = 0; i < h * w; i++) { g[R[i] + 1][C[i] + 1] = i + 1; rr[i + 1] = R[i] + 1, cc[i + 1] = C[i] + 1; } for (int i = 0; i <= h; i++) { for (int j = 0; j <= w; j++) { vector<int> tmp; tmp.push_back(g[i][j]); tmp.push_back(g[i][j+1]); tmp.push_back(g[i+1][j]); tmp.push_back(g[i+1][j+1]); sort(tmp.begin(), tmp.end()); st.update(tmp[0], tmp[1] - 1, 1, 1, n, 1); // cout << st.query() << '\n'; // cout << tmp[0] << ' ' << tmp[1] - 1 << ' ' << 1 << '\n'; st.update(tmp[2], tmp[3] - 1, inf, 1, n, 1); // cout << st.query() << '\n'; // cout << tmp[2] << ' ' << tmp[3] - 1 << ' ' << "inf" << '\n'; } } // cout << "ans: " << st.query() << '\n'; } i32 swap_seats(i32 a, i32 b) { a++, b++; vector<pair<int, int>> e; for (int i = 0; i <= 1; i++) for (int j = 0; j <= 1; j++) { e.push_back({rr[a] - i, cc[a] - j}); e.push_back({rr[b] - i, cc[b] - j}); } sort(e.begin(), e.end()); e.erase(unique(e.begin(), e.end()), e.end()); for (auto& [i, j] : e) { vector<int> tmp; tmp.push_back(g[i][j]); tmp.push_back(g[i][j+1]); tmp.push_back(g[i+1][j]); tmp.push_back(g[i+1][j+1]); sort(tmp.begin(), tmp.end()); st.update(tmp[0], tmp[1] - 1, -1, 1, n, 1); st.update(tmp[2], tmp[3] - 1, -inf, 1, n, 1); } swap(rr[a], rr[b]); swap(cc[a], cc[b]); swap(g[rr[a]][cc[a]], g[rr[b]][cc[b]]); for (auto& [i, j] : e) { vector<int> tmp; tmp.push_back(g[i][j]); tmp.push_back(g[i][j+1]); tmp.push_back(g[i+1][j]); tmp.push_back(g[i+1][j+1]); sort(tmp.begin(), tmp.end()); st.update(tmp[0], tmp[1] - 1, 1, 1, n, 1); st.update(tmp[2], tmp[3] - 1, inf, 1, n, 1); } i32 ans = query(); // cout << "ans: " << ans << '\n'; return ans; } #ifndef EVAL #include <cstdio> #include <cstdlib> #include <vector> namespace { int read_int() { int x; if (scanf("%d", &x) != 1) { fprintf(stderr, "Error while reading input\n"); exit(1); } return x; } } // namespace int main() { int H = read_int(); int W = read_int(); int Q = read_int(); std::vector<int> R(H * W), C(H * W); for (int i = 0; i < H * W; ++i) { R[i] = read_int(); C[i] = read_int(); } std::vector<int> A(Q), B(Q); for (int j = 0; j < Q; ++j) { A[j] = read_int(); B[j] = read_int(); } give_initial_chart(H, W, R, C); for (int j = 0; j < Q; ++j) { int answer = swap_seats(A[j], B[j]); printf("%d\n", answer); } return 0; } #endif

컴파일 시 표준 에러 (stderr) 메시지

seats.cpp: In member function 'Node Node::operator+(Node)':
seats.cpp:16:5: warning: control reaches end of non-void function [-Wreturn-type]
   16 |     }
      |     ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...