結果
問題 | No.1920 Territory |
ユーザー | blackyuki |
提出日時 | 2022-04-29 22:16:11 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,079 ms / 5,000 ms |
コード長 | 8,044 bytes |
コンパイル時間 | 3,067 ms |
コンパイル使用メモリ | 242,468 KB |
実行使用メモリ | 51,292 KB |
最終ジャッジ日時 | 2024-06-29 03:47:19 |
合計ジャッジ時間 | 21,606 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 13 ms
5,376 KB |
testcase_04 | AC | 13 ms
5,376 KB |
testcase_05 | AC | 14 ms
5,376 KB |
testcase_06 | AC | 13 ms
5,376 KB |
testcase_07 | AC | 13 ms
5,376 KB |
testcase_08 | AC | 12 ms
5,376 KB |
testcase_09 | AC | 12 ms
5,376 KB |
testcase_10 | AC | 11 ms
5,376 KB |
testcase_11 | AC | 12 ms
5,376 KB |
testcase_12 | AC | 12 ms
5,376 KB |
testcase_13 | AC | 1,065 ms
51,160 KB |
testcase_14 | AC | 1,073 ms
51,292 KB |
testcase_15 | AC | 1,060 ms
51,188 KB |
testcase_16 | AC | 1,075 ms
51,204 KB |
testcase_17 | AC | 1,079 ms
51,164 KB |
testcase_18 | AC | 1,030 ms
43,232 KB |
testcase_19 | AC | 1,011 ms
43,232 KB |
testcase_20 | AC | 1,010 ms
43,172 KB |
testcase_21 | AC | 1,014 ms
43,156 KB |
testcase_22 | AC | 1,001 ms
43,356 KB |
testcase_23 | AC | 395 ms
40,924 KB |
testcase_24 | AC | 441 ms
45,536 KB |
testcase_25 | AC | 400 ms
45,532 KB |
testcase_26 | AC | 401 ms
45,536 KB |
testcase_27 | AC | 421 ms
45,404 KB |
testcase_28 | AC | 422 ms
45,664 KB |
testcase_29 | AC | 452 ms
45,140 KB |
testcase_30 | AC | 449 ms
45,152 KB |
testcase_31 | AC | 743 ms
42,768 KB |
testcase_32 | AC | 748 ms
42,844 KB |
testcase_33 | AC | 625 ms
39,260 KB |
testcase_34 | AC | 629 ms
39,260 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(long long i=0;i<(long long)(n);i++) #define REP(i,k,n) for(long long i=k;i<(long long)(n);i++) #define pb emplace_back #define lb(v,k) (lower_bound(all(v),(k))-v.begin()) #define ub(v,k) (upper_bound(all(v),(k))-v.begin()) #define fi first #define se second #define pi M_PI #define PQ(T) priority_queue<T> #define SPQ(T) priority_queue<T,vector<T>,greater<T>> #define dame(a) {out(a);return 0;} #define decimal cout<<fixed<<setprecision(15); #define all(a) a.begin(),a.end() #define rsort(a) {sort(all(a));reverse(all(a));} #define dupli(a) {sort(all(a));a.erase(unique(all(a)),a.end());} #define popcnt __builtin_popcountll typedef long long ll; typedef pair<ll,ll> P; typedef tuple<ll,int,int> PP; typedef tuple<ll,ll,ll,ll> PPP; using vi=vector<ll>; using vvi=vector<vi>; using vvvi=vector<vvi>; using vvvvi=vector<vvvi>; using vpp=vector<PP>; using vvpp=vector<vpp>; using vvvpp=vector<vvpp>; using vvvvpp=vector<vvvpp>; using vp=vector<P>; using vvp=vector<vp>; using vb=vector<bool>; using vvb=vector<vb>; const ll inf=1001001001001001001; const ll INF=1001001001; const ll mod=1000000007; const double eps=1e-10; template<class T> bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;} template<class T> bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;} template<class T> void out(T a){cout<<a<<'\n';} template<class T> void outp(T a){cout<<'('<<a.fi<<','<<a.se<<')'<<'\n';} template<class T> void outvp(T v){rep(i,v.size())cout<<'('<<v[i].fi<<','<<v[i].se<<')';cout<<'\n';} template<class T> void outvvp(T v){rep(i,v.size())outvp(v[i]);} template<class T> void outv(T v){rep(i,v.size()){if(i)cout<<' ';cout<<v[i];}cout<<'\n';} template<class T> void outvv(T v){rep(i,v.size())outv(v[i]);} template<class T> bool isin(T x,T l,T r){return (l)<=(x)&&(x)<=(r);} template<class T> void yesno(T b){if(b)out("yes");else out("no");} template<class T> void YesNo(T b){if(b)out("Yes");else out("No");} template<class T> void YESNO(T b){if(b)out("YES");else out("NO");} template<class T> void outset(T s){auto itr=s.begin();while(itr!=s.end()){if(itr!=s.begin())cout<<' ';cout<<*itr;itr++;}cout<<'\n';} void outs(ll a,ll b){if(a>=inf-100)out(b);else out(a);} ll gcd(ll a,ll b){if(b==0)return a;return gcd(b,a%b);} ll modpow(ll a,ll b,ll m){ll res=1;a%=m;while(b){if(b&1)res=res*a%m;a=a*a%m;b>>=1;}return res;} class BIT{ vi bit;ll n; ll sum(ll i){ ll res=0; for(;i;i-=i&-i)res+=bit[i]; return res; } public: BIT(ll n_):n(n_){bit=vi(n+1);} void add(ll i,ll x){ for(i++;i<=n;i+=i&-i)bit[i]+=x; } ll get(ll a,ll b){ if(b<=a)return 0ll; return sum(b)-sum(a); } ll solve(ll x){ //sum([0,r])>=x となる最小のr if(x<=0)return -1; ll l=0,len=1;while(len<n)len<<=1; for(;len;len>>=1)if(l+len<n&&bit[l+len]<x){ x-=bit[l+len];l+=len; } return l; } }; class UF{ vi data;stack<P> st; public: UF(ll n){ data=vi(n,1); } bool merge(ll x,ll y,bool undo=false){ // outp(P(x,y)); x=root(x);y=root(y); if(data[x]>data[y])swap(x,y); if(undo){st.emplace(y,data[y]);st.emplace(x,data[x]);} if(x==y)return false; data[y]+=data[x];data[x]=-y; return true; } ll root(ll i){if(data[i]>0)return i;return root(-data[i]);} ll getsize(ll i){return data[root(i)];} bool same(ll a,ll b){return root(a)==root(b);} void undo(){rep(i,2){data[st.top().fi]=st.top().se;st.pop();}} }; int main() { ll h=INF,w=INF,A,B;cin>>A>>B; ll n=A+B; vvi tate,yoko; tate.pb(vi{0,0,w}); tate.pb(vi{h,0,w}); yoko.pb(vi{0,0,h}); yoko.pb(vi{w,0,h}); vi sx,sy; sx.pb(0);sx.pb(h);sy.pb(0);sy.pb(w); rep(i,n){ ll a,b,c,d; if(i<A){ cin>>b>>a>>c;d=b; } else{ cin>>a>>b>>d;c=a; } sx.pb(a);sx.pb(c);sy.pb(b);sy.pb(d); if(a==c)tate.pb(vi{a,b,d}); else yoko.pb(vi{b,a,c}); } dupli(sx);dupli(sy); sort(all(tate));sort(all(yoko)); vvi ad(sx.size()),er(sx.size()); vvi ad2(sy.size()),er2(sy.size()); rep(i,tate.size()){ tate[i][0]=lb(sx,tate[i][0]); tate[i][1]=lb(sy,tate[i][1]); tate[i][2]=lb(sy,tate[i][2]); ad2[tate[i][1]].pb(i); er2[tate[i][2]].pb(i); } rep(i,yoko.size()){ yoko[i][0]=lb(sy,yoko[i][0]); yoko[i][1]=lb(sx,yoko[i][1]); yoko[i][2]=lb(sx,yoko[i][2]); ad[yoko[i][1]].pb(i); er[yoko[i][2]].pb(i); } // outv(sx);outv(sy); ll cnt_ver=0,cnt_edge=0,cnt_ren=0; ll idx=0; BIT bit(sy.size()); rep(i,sx.size()){ set<ll> S; for(ll j:er[i]){ S.insert(yoko[j][0]); bit.add(yoko[j][0],-1); } while(idx<tate.size()&&tate[idx][0]==i){ ll l=tate[idx][1],r=tate[idx][2]; cnt_ver+=bit.get(l+1,r); S.insert(l);S.insert(r); idx++; } for(ll j:ad[i]){ S.insert(yoko[j][0]); bit.add(yoko[j][0],1); } cnt_ver+=S.size(); } // out(cnt_ver); { ll idx=0; BIT bit(sy.size()); rep(i,sx.size()){ for(ll j:ad[i])bit.add(yoko[j][0],1); while(idx<tate.size()&&tate[idx][0]==i){ cnt_edge+=bit.get(tate[idx][1]+1,tate[idx][2])+1; idx++; } for(ll j:er[i])bit.add(yoko[j][0],-1); } } { ll idx=0; BIT bit(sx.size()); rep(i,sy.size()){ for(ll j:ad2[i])bit.add(tate[j][0],1); while(idx<yoko.size()&&yoko[idx][0]==i){ cnt_edge+=bit.get(yoko[idx][1]+1,yoko[idx][2])+1; idx++; } for(ll j:er2[i])bit.add(tate[j][0],-1); } } // outvv(yoko); // cout<<endl; // outvv(tate); UF uf(yoko.size()+tate.size()); { BIT bit(yoko.size()); set<ll> S; ll idx=0; rep(i,sx.size()){ for(ll j:ad[i]){ auto itr=S.lower_bound(j); if(itr!=S.begin()){ itr--; ll prev=*itr; ll tmp=bit.get(0,prev+1); if(tmp){ itr++; uf.merge(prev,*itr); itr--; bit.add(prev,-tmp); bit.add(prev+1,tmp); } } S.insert(j); ll tmp=bit.get(0,j+1); bit.add(j,-tmp); bit.add(j+1,tmp); } while(idx<tate.size()&&tate[idx][0]==i){ vi cl={tate[idx][1],-inf,-inf},cr={tate[idx][2]+1,-inf,-inf}; ll l=lb(yoko,cl),r=lb(yoko,cr); auto it1=S.lower_bound(l),it2=S.lower_bound(r); if(it1!=it2){ it2--; uf.merge(yoko.size()+idx,*it1); bit.add(l,1); bit.add(*it2,-1); } idx++; } for(ll j:er[i]){ auto itr=S.lower_bound(j); { ll tmp=bit.get(0,j+1); if(tmp){ itr++; uf.merge(j,*itr); itr--; } } if(itr!=S.begin()){ itr--; ll tmp=bit.get(0,*itr+1); if(tmp){ uf.merge(*itr,j); bit.add(*itr,-tmp); bit.add(*itr+1,tmp); } } S.erase(j); } } } rep(i,tate.size()+yoko.size())if(uf.root(i)==i)cnt_ren++; // out(cnt_edge); // out(cnt_ren); out(cnt_edge-cnt_ver+cnt_ren-1); }