結果

問題 No.1920 Territory
ユーザー SSRSSSRS
提出日時 2022-04-29 21:54:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 5,000 ms
コード長 4,358 bytes
コンパイル時間 1,344 ms
コンパイル使用メモリ 96,228 KB
実行使用メモリ 19,700 KB
最終ジャッジ日時 2024-06-29 03:19:13
合計ジャッジ時間 9,263 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 5 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 260 ms
17,652 KB
testcase_14 AC 269 ms
17,652 KB
testcase_15 AC 269 ms
17,648 KB
testcase_16 AC 264 ms
17,652 KB
testcase_17 AC 274 ms
17,524 KB
testcase_18 AC 273 ms
17,908 KB
testcase_19 AC 271 ms
17,912 KB
testcase_20 AC 272 ms
17,912 KB
testcase_21 AC 275 ms
17,908 KB
testcase_22 AC 279 ms
18,036 KB
testcase_23 AC 116 ms
14,708 KB
testcase_24 AC 138 ms
16,888 KB
testcase_25 AC 148 ms
19,576 KB
testcase_26 AC 149 ms
19,572 KB
testcase_27 AC 145 ms
19,700 KB
testcase_28 AC 146 ms
19,572 KB
testcase_29 AC 161 ms
16,628 KB
testcase_30 AC 162 ms
16,632 KB
testcase_31 AC 191 ms
15,988 KB
testcase_32 AC 187 ms
15,860 KB
testcase_33 AC 205 ms
16,496 KB
testcase_34 AC 208 ms
16,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//https://atcoder.jp/contests/joi2014ho/submissions/30150120
//Nachia さんありがとう
#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; };
struct HLine{ int y, l, r; };


int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
  int W = 200001, H = 200001;
  int N, M;
  cin >> N >> M;
	vector<VLine> VL;
	vector<HLine> HL;
  for (int i = 0; i < N; i++){
    int a, b, c;
    cin >> a >> b >> c;
    VL.push_back({a, b, c + 1});
  }
  for (int i = 0; i < M; i++){
    int p, q, r;
    cin >> p >> q >> r;
    HL.push_back({p, q, r + 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 });
	{
		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.y);
			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].y) = 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 --;
	cout << ans - 1 << '\n';

	return 0;
}

0