結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 5 ms
6,940 KB
testcase_05 AC 5 ms
6,944 KB
testcase_06 AC 6 ms
6,940 KB
testcase_07 AC 5 ms
6,944 KB
testcase_08 AC 6 ms
6,944 KB
testcase_09 AC 5 ms
6,940 KB
testcase_10 AC 5 ms
6,944 KB
testcase_11 AC 5 ms
6,940 KB
testcase_12 AC 5 ms
6,940 KB
testcase_13 AC 271 ms
17,580 KB
testcase_14 AC 276 ms
17,520 KB
testcase_15 AC 263 ms
17,528 KB
testcase_16 AC 266 ms
17,652 KB
testcase_17 AC 272 ms
17,644 KB
testcase_18 AC 268 ms
17,904 KB
testcase_19 AC 268 ms
17,908 KB
testcase_20 AC 267 ms
18,040 KB
testcase_21 AC 269 ms
18,036 KB
testcase_22 AC 267 ms
17,968 KB
testcase_23 AC 115 ms
14,684 KB
testcase_24 AC 139 ms
17,012 KB
testcase_25 AC 144 ms
19,700 KB
testcase_26 AC 149 ms
19,700 KB
testcase_27 AC 148 ms
19,568 KB
testcase_28 AC 142 ms
19,700 KB
testcase_29 AC 153 ms
16,624 KB
testcase_30 AC 156 ms
16,756 KB
testcase_31 AC 189 ms
15,992 KB
testcase_32 AC 198 ms
16,036 KB
testcase_33 AC 222 ms
16,628 KB
testcase_34 AC 224 ms
16,624 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