제출 #1046594

#제출 시각아이디문제언어결과실행 시간메모리
1046594Alihan_8Seats (IOI18_seats)C++17
100 / 100
577 ms91736 KiB
#include "seats.h"

#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ln '\n'

template <class F, class S>
bool chmin(F &u, const S &v){
	bool flag = false;
	
	if ( u > v ){
		u = v; flag |= true;
	}
	
	return flag;
}

template <class F, class S>
bool chmax(F &u, const S &v){
	bool flag = false;
	
	if ( u < v ){
		u = v; flag |= true;
	}
	
	return flag;
}

const int inf = 1e9;

const int N = 1e6 + 1;

vector <int> R, C, p;

int h, w, n;

struct SegTree{
	struct Info{
		int mn, cnt, s;
		
		Info(int mn = 0, int cnt = 1, int s = 0) : mn(mn), cnt(cnt), s(s) {}
	};
	
	Info T[N * 4];
	
	void merge(Info &rt, const Info &lf, const Info &rg){
		rt = Info();
		
		rt.mn = min(lf.mn, rg.mn + lf.s);
		rt.cnt = (lf.mn == rt.mn) * lf.cnt + (rt.mn == rg.mn + lf.s) * rg.cnt;
		rt.s = lf.s + rg.s;
	}
	
	void upd(int v, int l, int r, int p, int x){
		if ( l == r ){
			T[v] = Info(x, 1, x);
		
			return;
		}
		
		int m = (l + r) / 2;
		
		if ( p <= m ) upd(v * 2, l, m, p, x);
		else upd(v * 2 + 1, m + 1, r, p, x);
		
		merge(T[v], T[v * 2], T[v * 2 + 1]);
	}
	
	void upd(int p, int x){
		upd(1, 0, n - 1, p, x);
	}
	
	int get(int v, int l, int r, int p){
		if ( l == r ) return T[v].mn;
		
		int m = (l + r) / 2;
		
		return p <= m ? get(v * 2, l, m, p) : get(v * 2 + 1, m + 1, r, p);
	}
	
	int get(int p){ return get(1, 0, n - 1, p); } 
	
	int qry(){ return T[1].cnt; } 
} seg;

int z(int u, int v){ return u * w + v; }

int f(int j){
	if ( j < 0 || j >= n ) return -1;
	
	int x = j / w, y = j % w;
	
	int cnt = 0;
	
	for ( auto i: {0, -1} ){ 
		for ( auto k: {0, -1} ){
			int s = 0, q = 0;
			
			for ( auto a: {0, 1} ){
				for ( auto b: {0, 1} ){
					int u = x + i + a, v = y + k + b;
					
					if ( u >= 0 && v >= 0 && u < h && v < w ){
						s += p[z(u, v)] < p[j];
						q += p[z(u, v)] <= p[j];
					}
				}
			}
			
			cnt += (q & 1) - (s & 1);
		}
	}
	
	return cnt;
}

void upd(int x, int y){
	if ( x < 0 || y < 0 || x >= h || y >= w ) return;
	
	int j = z(x, y);
	
	seg.upd(p[j], f(j));
}

void give_initial_chart(int H, int W, std::vector<int> R_, std::vector<int> C_) {
	h = H, w = W, R = R_, C = C_, n = h * w;
	
	p.resize(n);
	
	for ( int i = 0; i < h; i++ ){
		for ( int j = 0; j < w; j++ ){
			int x = z(i, j);
			
			p[z(R[x], C[x])] = x;
		}
	}
	
	for ( int i = 0; i < n; i++ ){
		upd(R[i], C[i]);
	}
}

int swap_seats(int a, int b) {
	swap(p[z(R[a], C[a])], p[z(R[b], C[b])]);
	swap(C[a], C[b]); swap(R[a], R[b]);
	
	for ( auto i: {-1, 0, 1} ){
		for ( auto j: {-1, 0, 1} ){
			upd(R[a] + i, C[a] + j);
			upd(R[b] + i, C[b] + j);
		}
	}
	
	return seg.qry();
}
#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...