Submission #438518

#TimeUsernameProblemLanguageResultExecution timeMemory
438518fhvirusSeats (IOI18_seats)C++17
100 / 100
1535 ms107228 KiB
// 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 VAL {
		int val, cnt;
		VAL(int v = 0, int c = 1): val(v), cnt(c) {}
		VAL operator+ (int v) { return VAL(val + v, cnt); }
		VAL operator+ (VAL v) {
			if(val < v.val) return *this;
			if(val > v.val) return v;
			return VAL(val, cnt + v.cnt);
		}
	};
	struct NOD {
		VAL val; int tag;
		NOD (int v = 0): val(v), tag(0) {}
		VAL get() { return val + tag; }
	};
	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) nds[i].val = nds[i<<1].get() + nds[i<<1|1].get();
	}
	void pull(int p){ for(p >>= 1; p > 0; p >>= 1) nds[p].val = nds[p<<1].get() + nds[p<<1|1].get(); }
	void mod(int l, int r, int v){ 
		int tl = l + n, tr = r + n - 1;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1){
			if(l & 1) nds[l++].tag += v;
			if(r & 1) nds[--r].tag += v;
		}
		pull(tl), pull(tr);
	}
	int que(){ return nds[1].get().cnt; }
} 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 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...