이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
template<class I>bool chmax(I&a,I b){return a<b?(a=b,true):false;}
template<class I>bool chmin(I&a,I b){return b<a?(a=b,true):false;}
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#pragma GCC optimize("Ofast")
#define debug(...) ((void)0)
#endif
#include "seats.h"
int H, W;
vector<int> R, C;
vector<vector<int>> A;
vector<int> pre;
bool bld;
struct ZCK {
struct NOD {
int val, cnt, tag;
NOD(): val(0), cnt(0), tag(0) {}
NOD(int v): val(v), cnt(1), tag(0) {}
};
void mer(NOD &u, NOD a, NOD b){
if(a.val > b.val) swap(a, b);
u.val = a.val, u.cnt = b.cnt;
if(u.val == b.val) u.cnt += b.cnt;
}
NOD mer(NOD a, NOD b){
if(a.val > b.val) swap(a, b);
if(a.val == b.val) a.cnt += b.cnt;
return a;
}
int n; vector<NOD> nds;
void init(int nn){
n = nn; nds.resize(n * 2);
for(int i = 0; i < n; ++i) nds[i + n] = NOD(pre[i]);
for(int i = n-1; i > 0; --i) mer(nds[i], nds[i<<1], nds[i<<1|1]);
}
void upd(int u, int v){ nds[u].val += v; if(u < n) nds[u].tag += v; }
void push(int u){
for(int h = __lg(n); h >= 0; --h){
int i = u >> h;
if(nds[i>>1].tag == 0) continue;
upd(i, nds[i>>1].tag);
upd(i^1, nds[i>>1].tag);
nds[i>>1].tag = 0;
}
}
void pull(int u){
for(; u > 1; u >>= 1){
mer(nds[u>>1], nds[u], nds[u^1]);
nds[u>>1].val += nds[u>>1].tag;
}
}
void mod(int l, int r, int v){
int tl = l + n, tr = r + n - 1;
push(tl), push(tr);
for(l += n, r += n; l < r; l >>= 1, r >>= 1){
if(l & 1) upd(l++, v);
if(r & 1) upd(--r, v);
}
pull(tl), pull(tr);
}
int que(int l, int r){
push(l+n), push(r+n-1);
for(int i = 1; i < n * 2; ++i) debug(i, nds[i].val, nds[i].cnt, nds[i].tag);
NOD ans(1e9 + 7);
for(l += n, r += n; l < r; l >>= 1, r >>= 1){
if(l & 1) ans = mer(ans, nds[l++]);
if(r & 1) ans = mer(ans, nds[--r]);
}
debug(ans.val, ans.cnt);
return ans.cnt;
}
int que(){ return que(0, n); }
} sgt;
inline int eek(int i, int j){ return (i < 0 or j < 0 or i >= H or j >= W ? H * W : A[i][j]); }
void add(int l, int r, int v){
if(l == r) return;
if(bld) pre[l] += v, pre[r] -= v;
else sgt.mod(l, r, v);
}
void upd(int i, int j, int v){
vector<int> tmp({ eek(i, j), eek(i-1, j), eek(i, j-1), eek(i-1, j-1) });
sort(AI(tmp));
add(tmp[0], tmp[1], v);
add(tmp[2], tmp[3], v);
}
void give_initial_chart(int H, int W, vector<int> R, vector<int> C) {
::H = H, ::W = W;
::R = R, ::C = C;
A.assign(H, vector<int>(W, 0));
for(int i = 0; i < H * W; ++i) A[R[i]][C[i]] = i;
pre.assign(H * W + 1, 0); bld = true;
for(int i = 0; i <= H; ++i) for(int j = 0; j <= W; ++j) upd(i, j, 1);
for(int i = 1; i <= H * W; ++i) pre[i] += pre[i-1];
sgt.init(H * W);
bld = false;
}
int swap_seats(int a, int b) {
for(int d: {0, 1}) for(int e: {0, 1}) upd(R[a] + d, C[a] + e, -1);
for(int d: {0, 1}) for(int e: {0, 1}) upd(R[b] + d, C[b] + e, -1);
swap(A[R[a]][C[a]], A[R[b]][C[b]]);
swap(R[a], R[b]), swap(C[a], C[b]);
for(int d: {0, 1}) for(int e: {0, 1}) upd(R[a] + d, C[a] + e, 1);
for(int d: {0, 1}) for(int e: {0, 1}) upd(R[b] + d, C[b] + e, 1);
return sgt.que();
}
# | 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... |