結果

問題 No.1920 Territory
ユーザー 👑 NachiaNachia
提出日時 2022-04-29 22:22:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 5,000 ms
コード長 4,439 bytes
コンパイル時間 1,429 ms
コンパイル使用メモリ 98,468 KB
実行使用メモリ 19,704 KB
最終ジャッジ日時 2024-06-29 03:55:25
合計ジャッジ時間 7,539 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 5 ms
5,376 KB
testcase_05 AC 6 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 6 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 266 ms
17,652 KB
testcase_14 AC 265 ms
17,520 KB
testcase_15 AC 257 ms
17,652 KB
testcase_16 AC 260 ms
17,652 KB
testcase_17 AC 260 ms
17,652 KB
testcase_18 AC 254 ms
17,936 KB
testcase_19 AC 257 ms
17,908 KB
testcase_20 AC 275 ms
18,036 KB
testcase_21 AC 264 ms
18,036 KB
testcase_22 AC 260 ms
18,020 KB
testcase_23 AC 116 ms
14,708 KB
testcase_24 AC 134 ms
17,012 KB
testcase_25 AC 142 ms
19,700 KB
testcase_26 AC 140 ms
19,700 KB
testcase_27 AC 141 ms
19,580 KB
testcase_28 AC 141 ms
19,704 KB
testcase_29 AC 154 ms
16,756 KB
testcase_30 AC 156 ms
16,756 KB
testcase_31 AC 189 ms
15,860 KB
testcase_32 AC 191 ms
15,988 KB
testcase_33 AC 221 ms
16,504 KB
testcase_34 AC 219 ms
16,628 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
using i64 = long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)


struct DSU{
	vector<int> A;
	DSU(int n){ A.assign(n, -1); }
	int leader(int a){
		if(A[a] < 0) return a;
		return A[a] = leader(A[a]);
	}
	int merge(int u, int v){
		u = leader(u);
		v = leader(v);
		if(u == v) return u;
		if(A[u] == A[v]) A[u]--;
		else if(A[u] > A[v]) swap(u,v);
		A[v] = u;
		return u;
	}
	int add(){
		int res = A.size();
		A.push_back(-1);
		return res;
	}
	int num_groups(){
		int ans = 0;
		rep(i,A.size()) if(A[i] < 0) ans++;
		return ans;
	}
};


void coordcompress(vector<int*> A, vector<int*> B){
	int n = A.size();
	vector<int> I(n);
	rep(i,n) I[i] = i;
	sort(I.begin(),I.end(),[&](int l,int r){return *A[l]<*A[r];});
	vector<int> S(n);
	rep(i,n) S[i] = *A[i];
	sort(S.begin(), S.end());
	rep(i,n) *A[I[i]] = i;
	for(auto b : B) *b = lower_bound(S.begin(), S.end(), *b) - S.begin();
}


struct BIT{
	vector<int> A;
	BIT(int n){ A.assign(n+1, 0); }
	void add(int i, int x){
		i++;
		while(i < (int)A.size()){
			A[i] += x;
			i += i & -i;
		}
	}
	int sum(int r){
		int res = 0;
		while(r){
			res += A[r];
			r -= r & -r;
		}
		return res;
	}
	int sum(int l, int r){ return sum(r) - sum(l); }
};

#include <cassert>

struct RMQ{
	int N = 0;
	vector<int> A;
	void aggregate(int i){ A[i] = max(A[i*2], A[i*2+1]); }
	RMQ(int n = 0){
		N = 1; while(N < n) N *= 2;
		A.assign(N*2, -1);
	}
	int prod(int l, int r){
		assert(0 <= l);
		assert(r <= N);
		l += N; r += N;
		int res = -1;
		while(l < r){
			if(l & 1) res = max(res, A[l++]);
			if(r & 1) res = max(res, A[--r]);
			l /= 2; r /= 2;
		}
		return res;
	}
	void set(int p, int x){
		assert(p < N);
		p += N;
		A[p] = x;
		while(p != 1){
			p /= 2;
			aggregate(p);
		}
	}
	void dump(){
		cout << "[ ";
		rep(i,N) cout << A[N+i] << " ";
		cout << "]" << endl;
	}
};


struct VLine{ int x, l, r; };


int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	// int W, H, N; cin >> W >> H >> N;
	int W = 1'000'000'000;
	int H = 1'000'000'000;
	int N; cin >> N;
	int M; cin >> M;

	vector<VLine> VL;
	vector<VLine> HL;
	/*
	rep(i,N){
		int xl,xr,yl,yr; cin >> xl >> yl >> xr >> yr;
		if(xl == xr) VL.push_back({ xl,yl,yr+1 });
		if(yl == yr) HL.push_back({ yl,xl,xr+1 });
	}
	*/
	rep(i,N){
		int a,b,c; cin >> a >> b >> c;
		VL.push_back({a,b,c+1});
	}
	rep(i,M){
		int a,b,c; cin >> a >> b >> c;
		HL.push_back({a,b,c+1});
	}

	VL.push_back({ 0,0,H+1 });
	VL.push_back({ W,0,H+1 });
	HL.push_back({ 0,0,W+1 });
	HL.push_back({ H,0,W+1 });

	if(VL.size() < HL.size()) swap(VL, HL);

	{
		vector<int*> XA, XB, YA, YB;
		for(auto& vl : VL){
			XA.push_back(&vl.x);
			YB.push_back(&vl.l); YB.push_back(&vl.r);
		}
		for(auto& hl : HL){
			YA.push_back(&hl.x);
			XB.push_back(&hl.l); XB.push_back(&hl.r);
		}
		coordcompress(move(XA), move(XB));
		coordcompress(move(YA), move(YB));
	}

	W = VL.size();
	H = HL.size();

	i64 ans = 0;

	vector<int> cid(W+1, -1);
	RMQ spmin = RMQ(W+1);
	spmin.set(W, W - W);
	cid[W] = 0;
	DSU dsu = DSU(1);
	BIT spcnt = BIT(W+1);
	RMQ knownmin = RMQ(W+1);

	vector<vector<int>> inqueries(H);
	vector<int> midqueries(H);
	vector<vector<int>> outqueries(H);
	for(auto vl : VL){
		if(vl.l == vl.r) continue;
		inqueries[vl.l].push_back(vl.x);
		outqueries[vl.r-1].push_back(vl.x);
	}
	rep(i,H) midqueries.at(HL[i].x) = i;

	rep(y,H){
		for(auto x : inqueries[y]){
			int nx = W - spmin.prod(x+1, W+1);
			if(cid[nx] < 0){
				cid[nx] = dsu.add();
				knownmin.set(nx, W - nx);
			}
			cid[x] = cid[nx];
			spcnt.add(x, 1);
			spmin.set(x, W - x);
			knownmin.set(x, W - x);
		}
		rep(dummy_counter,1){
			int hli = midqueries[y];
			int l = HL[hli].l, r = HL[hli].r;
			int spl = spmin.prod(l, r);
			if(spl < 0) break;
			spl = W - spl;
			ans += spcnt.sum(spl+1, r);
			while(true){
				int nx = knownmin.prod(spl+1, r);
				if(nx < 0) break;
				nx = W - nx;
				ans--;
				knownmin.set(nx, -1);
				cid[nx] = -1;
			}
		}
		for(auto x : outqueries[y]){
			int col = cid[x];
			int nx = spmin.prod(x+1, W+1);
			nx = W - nx;
			if(cid[x] >= 0 && cid[nx] >= 0) dsu.merge(cid[x], cid[nx]);
			if(cid[nx] >= 0) col = cid[nx];
			if(cid[x] >= 0){ cid[x] = -1; knownmin.set(x, -1); }
			cid[nx] = col;
			if(cid[nx] >= 0) knownmin.set(nx, W - nx);
			spcnt.add(x, -1);
			spmin.set(x, -1);
		}
	}

	ans += dsu.num_groups();
	ans --;
	ans --;
	cout << ans << '\n';

	return 0;
}

0