結果

問題 No.1920 Territory
ユーザー 👑 kmjpkmjp
提出日時 2022-05-12 00:36:51
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 620 ms / 5,000 ms
コード長 3,856 bytes
コンパイル時間 2,647 ms
コンパイル使用メモリ 222,616 KB
実行使用メモリ 181,840 KB
最終ジャッジ日時 2023-09-26 16:39:14
合計ジャッジ時間 18,386 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
164,324 KB
testcase_01 AC 82 ms
164,220 KB
testcase_02 AC 82 ms
164,232 KB
testcase_03 AC 93 ms
166,528 KB
testcase_04 AC 93 ms
166,368 KB
testcase_05 AC 93 ms
164,908 KB
testcase_06 AC 92 ms
166,332 KB
testcase_07 AC 93 ms
166,532 KB
testcase_08 AC 87 ms
164,280 KB
testcase_09 AC 88 ms
164,328 KB
testcase_10 AC 88 ms
164,216 KB
testcase_11 AC 86 ms
164,220 KB
testcase_12 AC 86 ms
164,276 KB
testcase_13 AC 606 ms
177,828 KB
testcase_14 AC 593 ms
177,724 KB
testcase_15 AC 620 ms
177,680 KB
testcase_16 AC 602 ms
177,700 KB
testcase_17 AC 580 ms
177,704 KB
testcase_18 AC 555 ms
174,832 KB
testcase_19 AC 528 ms
174,848 KB
testcase_20 AC 557 ms
174,612 KB
testcase_21 AC 548 ms
174,900 KB
testcase_22 AC 546 ms
174,824 KB
testcase_23 AC 248 ms
177,640 KB
testcase_24 AC 297 ms
181,840 KB
testcase_25 AC 210 ms
178,736 KB
testcase_26 AC 211 ms
179,220 KB
testcase_27 AC 212 ms
179,228 KB
testcase_28 AC 214 ms
179,284 KB
testcase_29 AC 318 ms
180,548 KB
testcase_30 AC 313 ms
180,556 KB
testcase_31 AC 451 ms
174,020 KB
testcase_32 AC 448 ms
174,020 KB
testcase_33 AC 332 ms
172,488 KB
testcase_34 AC 338 ms
172,440 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N,M;
int L[1210101],R[1210101],D[1210101],U[1210101];
vector<int> LRdel[1210101],LRadd[1210101],UDdel[1210101],UDadd[1210101];


template<int um> class UF {
	public:
	vector<int> par,rank,cnt;
	UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
	void reinit(int num=um) {int i; FOR(i,num) rank[i]=0,cnt[i]=1,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int count(int x) { return cnt[operator[](x)];}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		cnt[y]=cnt[x]=cnt[x]+cnt[y];
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<1<<21> uf;


template<class V, int ME> class BIT {
public:
	V bit[1<<ME];
	V operator()(int e) {if(e<0) return 0;V s=0;e++;while(e) s+=bit[e-1],e-=e&-e; return s;}
	void add(int e,V v) { e++; while(e<=1<<ME) bit[e-1]+=v,e+=e&-e;}
};
BIT<int,21> bt;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	MINUS(L);
	MINUS(R);
	MINUS(U);
	MINUS(D);
	
	ll NE=0,NV=2*(N+M);
	FOR(i,N) {
		cin>>y>>l>>r;
		y*=3;
		l=l*3-1;
		r=r*3+1;
		LRadd[l].push_back(y);
		LRdel[r].push_back(y);
		L[y]=l;
		R[y]=r;
	}
	FOR(i,M) {
		cin>>x>>l>>r;
		x*=3;
		l=l*3-1;
		r=r*3+1;
		UDadd[l].push_back(x);
		UDdel[r].push_back(x);
		D[x]=l;
		U[x]=r;
	}
	
	for(x=0;x<=1000000;x++) {
		FORR(y,LRadd[x]) bt.add(y,1);
		if(D[x]!=-1) {
			NE+=1+bt(U[x]-1)-bt(D[x]);
			NV+=bt(U[x]-1)-bt(D[x]);
		}
		FORR(y,LRdel[x]) bt.add(y,-1);
	}
	
	for(y=0;y<=1000000;y++) {
		FORR(x,UDadd[y]) bt.add(x,1);
		if(L[y]!=-1) NE+=1+bt(R[y]-1)-bt(L[y]);
		FORR(x,UDdel[y]) bt.add(x,-1);
	}
	
	set<pair<int,int>> S;
	set<int> T;
	S.insert({-1,-1});
	S.insert({1<<20,1<<20});
	T.insert(-1);
	T.insert(1<<20);
	FOR(x,1000000) {
		if(D[x]!=-1) {
			int mi=1<<20,ma=-1;
			while(1) {
				auto it=S.lower_bound({U[x]+1,0});
				it--;
				if(it->second<D[x]) break;
				if(it->first<D[x]&&U[x]<it->second) {
					auto it2=T.lower_bound(D[x]);
					if(*it2>U[x]) break;
				}
				uf((1<<20)+x,it->first);
				mi=min(mi,it->first);
				ma=max(ma,it->second);
				S.erase(it);
			}
			if(mi<=ma) S.insert({mi,ma});
			
		}
		FORR(e,LRadd[x]) {
			T.insert(e);
			auto it=S.lower_bound({e,0});
			it--;
			if(it->second>e) {
				int a=it->first;
				int b=it->second;
				assert(a<e&&e<b);
				S.erase(it);
				auto it2=T.find(e);
				S.insert({a,*prev(it2)});
				S.insert({e,e});
				S.insert({*next(it2),b});
			}
			else {
				S.insert({e,e});
			}
		}
		FORR(e,LRdel[x]) {
			auto it=S.lower_bound({e+1,0});
			it--;
			int a=it->first;
			int b=it->second;
			assert(a<=e&&e<=b);
			S.erase(it);
			auto it2=T.find(e);
			if(a==e) {
				if(b!=e) {
					S.insert({*next(it2),b});
				}
			}
			else {
				if(b==e) {
					S.insert({a,*prev(it2)});
				}
				else {
					S.insert({a,b});
				}
			}
			
			T.erase(e);
		}
	}
	int con=0;
	FOR(i,1<<20) {
		if(uf[i]==i&&L[i]!=-1) con++;
		if(uf[i+(1<<20)]==i+(1<<20)&&D[i]!=-1) con++;
	}
	//cout<<NE<<" "<<NV<<" "<<con<<endl;
	cout<<NE-NV+con<<endl;
	
	
}
int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0