結果
問題 | No.1054 Union add query |
ユーザー | hamamu |
提出日時 | 2020-05-18 00:26:10 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 15,562 bytes |
コンパイル時間 | 2,449 ms |
コンパイル使用メモリ | 222,344 KB |
実行使用メモリ | 80,488 KB |
最終ジャッジ日時 | 2024-10-01 14:06:56 |
合計ジャッジ時間 | 7,587 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | AC | 253 ms
44,036 KB |
testcase_07 | AC | 225 ms
44,036 KB |
testcase_08 | AC | 252 ms
43,084 KB |
testcase_09 | WA | - |
testcase_10 | AC | 269 ms
78,208 KB |
ソースコード
#include "bits/stdc++.h" #include <random> using namespace std; using ll =long long; using dd =double; using pll =pair<ll, ll>; using tll =tuple<ll,ll,ll>; using vll =vector<ll>; using vdd =vector<dd>; using vpll =vector<pll>; using vtll =vector<tll>; using vvll =vector<vll>; using vvdd =vector<vdd>; using vvpll =vector<vpll>; using vvtll =vector<vtll>; using vvvll =vector<vvll>; using vvvdd=vector<vvdd>; using vvvpll =vector<vvpll>; using vvvtll =vector<vvtll>; using vvvvll=vector<vvvll>;using vvvvdd=vector<vvvdd>;using vvvvpll=vector<vvvpll>;using vvvvtll=vector<vvvtll>; constexpr ll INF = 1LL << 60; constexpr dd EPS = 1e-11; //cin,cout高速化のおまじない+桁数指定 struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(std::numeric_limits<double>::max_digits10); } } fast; #define REPS(i, S, E) for (ll i = (S); i <= (E); i++) #define rep(i, S, E) for (ll i = (S); i <= (E); i++) #define REP(i, N) REPS(i, 0, (N)-1) #define DEPS(i, S, E) for (ll i = (E); i >= (S); i--) #define dep(i, E, S) for (ll i = (E); i >= (S); i--) #define DEP(i, N) DEPS(i, 0, (N)-1) #define EACH(e, v) for (auto&& e : v) #define ALL(v) (v).begin(), (v).end() #define RALL(v) (v).rbegin(), (v).rend() template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; } template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; } template<class T> inline T MaxE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmax(m,v[i]); return m;}//v[S]~v[E]の最大値 template<class T> inline T MinE(vector<T>&v,ll S,ll E){T m=v[S]; REPS(i,S,E)chmin(m,v[i]); return m;}//v[S]~v[E]の最小値 template<class T> inline T MaxE(vector<T> &v, ll N) { return MaxE(v, 0, N - 1); } //先頭N個中の最大値 template<class T> inline T MinE(vector<T> &v, ll N) { return MinE(v, 0, N - 1); } template<class T> inline T MaxE(vector<T> &v) { return MaxE(v, (ll)v.size()); } template<class T> inline T MinE(vector<T> &v) { return MinE(v, (ll)v.size()); } template<class T> inline ll MaxI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]>v[m])m=i;} return m;} template<class T> inline ll MinI(vector<T>&v,ll S,ll E){ll m=S; REPS(i,S,E){if(v[i]<v[m])m=i;} return m;} template<class T> inline ll MaxI(vector<T> &v, ll N) { return MaxI(v, 0, N - 1); } template<class T> inline ll MinI(vector<T> &v, ll N) { return MinI(v, 0, N - 1); } template<class T> inline ll MaxI(vector<T> &v) { return MaxI(v, (ll)v.size()); } template<class T> inline ll MinI(vector<T> &v) { return MinI(v, (ll)v.size()); } template<class T> inline T Sum(vector<T> &v, ll S,ll E){ T s=v[S]; REPS(i,S+1,E)s+=v[i]; return s; } template<class T> inline T Sum(vector<T> &v, ll N) { return Sum(v, 0, N - 1); } template<class T> inline T Sum(vector<T> &v) { return Sum(v, v.size()); } template<class T> inline ll sz(T &v){ return (ll)v.size(); } template<class T> inline T POW(T a, ll n){ T r=1; for (; n>0; n>>=1, a*=a){ if (n&1)r*=a; } return r; } inline ll POW(int a, ll n){ return POW((ll)a, n); } inline ll CEIL(ll a, ll b){ return (a+b-1)/b; } ll Gcd(ll a, ll b){ return (b==0) ? a : Gcd(b, a%b); } ll Lcm(ll a, ll b){ return a * b / Gcd(a, b); } inline ll MSB(ll a){ for (ll o=63, x=-1;;){ ll m=(o+x)/2; if (a<(1LL<<m))o=m; else x=m; if (o-x==1)return x; } } inline ll Upper2N(ll s) { return 1LL<<MSB(s)<<1; }//s<2^nなる最小の2^n inline vll Bit2Idx(ll m){vll v; for(ll i=0;m;m>>=1,i++) if(m&1)v.push_back(i); return move(v);} inline ll BitNum(ll m){ for (ll c=0;; m>>=1){ c+=m&1; if (!m)return c; } } inline ll Bit(ll s, ll i){ return (s>>i)&1; } //sの第ibit i=0-63 [[nodiscard]] inline ll BitOn(ll s, ll i){ return s|(1LL<<i); } //sの第ibitON i=0-63 [[nodiscard]] inline ll BitOff(ll s, ll i){ return s&~(1LL<<i); } //sの第ibitOFF i=0-63 [[nodiscard]] inline ll BitCut(ll s, ll i){ return s & (1LL<<++i)-1; }//sの第0~第ibitを抽出 inline vll str2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'0'); return move(v);} inline vll ALP2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'A'); return move(v);} inline vll alp2num(string &s, ll N) {vll v(N); REP(i, N) v[i]=(ll)(s[i]-'a'); return move(v);} template<class T> inline ll FloorIdx(vector<T> &v, T x){ return upper_bound(ALL(v), x)-v.begin()-1; } template<class T> inline ll CeilIdx(vector<T> &v, T x){ return lower_bound(ALL(v), x)-v.begin(); } template<class T> inline ll UnderNumOf(vector<T> &v, T x){ //sort済vのx以下の個数を得る return upper_bound(v.begin(), v.end(), x) - v.begin(); } template<class T, class Pr> inline ll UnderNumOf(vector<T> &v, T x, Pr pred){ //x以下個数 return upper_bound(v.begin(), v.end(), x, pred) - v.begin(); } template<class T> inline ll OverNumOf(vector<T> &v, T x){ //sort済vのx以上の個数を得る return (ll)v.size() - (lower_bound(v.begin(), v.end(), x) - v.begin()); } template<class T, class Pr> inline ll OverNumOf(vector<T> &v, T x, Pr pred){ //x以上の個数 return (ll)v.size() - (lower_bound(v.begin(), v.end(), x, pred) - v.begin()); } template<class T=ll> inline vector<T> cinv(ll N){ vector<T> v(N); REP(i, N)cin>>v[i]; return move(v);} template<class T=ll, class S=ll> inline vector<pair<T, S>> cinv2(ll N){ vector<pair<T, S>> v(N); REP(i,N){cin>>v[i].first>>v[i].second;} return move(v); } template<class T=ll,class S=ll,class R=ll> inline vector<tuple<T, S, R>> cinv3(ll N){ vector<tuple<T,S,R>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i]);} return move(v);} template<class T=ll,class S=ll,class R=ll,class Q=ll> inline vector<tuple<T,S,R,Q>> cinv4(ll N){ vector<tuple<T,S,R,Q>> v(N); REP(i,N){cin>>get<0>(v[i])>>get<1>(v[i])>>get<2>(v[i])>>get<3>(v[i]);} return move(v);} template<class T=ll> inline vector<vector<T>> cinvv(ll N, ll M){ vector<vector<T>> vv(N); REP(i, N) vv[i] = cinv(M); return move(vv);} template<class T> inline void coutv(vector<T> &v, char deli=' '){ ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?'\n':deli);} } template<class T> inline void coutv(deque<T> &v, char deli=' '){ ll N=(ll)v.size(); REP(i,N){cout << v[i] << ((i==N-1)?'\n':deli);} } template<class T> void bye(T a){cout << a << '\n'; exit(0);} #if defined(_DEBUG) ll dumpW = 5; template<class T> void Dump(vector<T> &v){ REP(i, (ll)v.size()) { cerr << ((i==0)?"[":" ") << setw(dumpW) << v[i]; } cerr << "]"; } void Dump(vll &v){ REP(i, (ll)v.size()) { cerr << ((i==0)?"[":" ") << setw(dumpW); if (v[i] == INF) cerr << "INF"; else if (v[i] == -INF) cerr << "-INF"; else cerr << v[i]; } cerr << "]"; } template<class T> void Dump(vector<vector<T>> &v){ ll N=(ll)v.size(); string d[2][2]={{" ", "["}, {"\n", "]\n"}}; REP(i, N) { cerr << d[0][i==0]; Dump(v[i]); cerr << d[1][i==N-1]; } } template<class T> void Dump(vector<vector<vector<T>>> &v){ ll N=(ll)v.size(); string d[2]={" <", "[ <"}; REP(i, N) { cerr << d[i==0] << i << ">\n"; Dump(v[i]); } cerr << "]\n"; } template<class T> void Dump(vector<vector<vector<vector<T>>>> &v){ ll N=(ll)v.size(); string d[2]={" ---<", "[ ---<"}; REP(i, N) { cerr << d[i==0] << i << ">---\n"; Dump(v[i]); } cerr << "]\n"; } template<class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v){ ll N=(ll)v.size(); string d[2]={" ======<", "[ ======<"}; REP(i, N) { cerr << d[i==0] << i << ">======\n"; Dump(v[i]); } cerr << "]\n"; } #else template<class T> void Dump(vector<T> &v){} template<class T> void Dump(vector<vector<T>> &v){} template<class T> void Dump(vector<vector<vector<T>>> &v){} template<class T> void Dump(vector<vector<vector<vector<T>>>> &v){} template<class T> void Dump(vector<vector<vector<vector<vector<T>>>>> &v){} #endif template<ll MOD> struct mll_{ ll val; mll_(ll v = 0): val(v % MOD){ if (val < 0) val += MOD; } mll_ operator - () const { return -val; } mll_ operator + (const mll_ &b) const { return val + b.val; } mll_ operator - (const mll_ &b) const { return val - b.val; } mll_ operator * (const mll_ &b) const { return val * b.val; } mll_ operator / (const mll_ &b) const { return mll_(*this) /= b; } mll_ operator + (ll b) const { return *this + mll_(b); } mll_ operator - (ll b) const { return *this - mll_(b); } mll_ operator * (ll b) const { return *this * mll_(b); } friend mll_ operator + (ll a, const mll_ &b) { return b + a; } friend mll_ operator - (ll a, const mll_ &b) { return -b + a; } friend mll_ operator * (ll a, const mll_ &b) { return b * a; } friend mll_ operator / (ll a, const mll_ &b) { return mll_(a)/b; } mll_ &operator += (const mll_ &b) { val=(val+b.val)%MOD; return *this; } mll_ &operator -= (const mll_ &b) { val=(val+MOD-b.val)%MOD; return *this; } mll_ &operator *= (const mll_ &b) { val=(val*b.val)%MOD; return *this; } mll_ &operator /= (const mll_ &b) { ll c=b.val, d=MOD, u=1, v=0; while (d){ ll t = c / d; c -= t * d; swap(c, d); u -= t * v; swap(u, v); } val = val * u % MOD; if (val < 0) val += MOD; return *this; } mll_ &operator += (ll b) { return *this += mll_(b); } mll_ &operator -= (ll b) { return *this -= mll_(b); } mll_ &operator *= (ll b) { return *this *= mll_(b); } mll_ &operator /= (ll b) { return *this /= mll_(b); } bool operator == (const mll_ &b) { return val == b.val; } bool operator != (const mll_ &b) { return val != b.val; } bool operator == (ll b) { return *this == mll_(b); } bool operator != (ll b) { return *this != mll_(b); } friend bool operator == (ll a, const mll_ &b) { return mll_(a) == b.val; } friend bool operator != (ll a, const mll_ &b) { return mll_(a) != b.val; } friend ostream &operator << (ostream &os, const mll_ &a) { return os << a.val; } friend istream &operator >> (istream &is, mll_ &a) { return is >> a.val; } static mll_ Combination(ll a, ll b){ chmin(b, a-b); if (b<0) return mll_(0); mll_ c = 1; REP(i, b) c *= a-i; REP(i, b) c /= i+1; return c; } }; using mll = mll_<1000000007LL>; //1000000007LL;// 998244353LL; using vmll = std::vector<mll>; using vvmll = std::vector<vmll>; using vvvmll = std::vector<vvmll>; using vvvvmll = std::vector<vvvmll>; using vvvvvmll = std::vector<vvvvmll>; template <class T, class E> struct LazySegmentTree{ using F = function<T(T, T)>; using G = function<T(T, E)>; using H = function<E(E, E)>; F f; //!< データ同士の合成関数 G g; //!< 作用関数 H h; //!< 作用素同士の合成関数 T ti; //!< データの単位元 実際の単位元をセットしないとだめ E ei; //!< 作用素の単位元 実際の単位元ではなくても、使わない値ならOK ll n=1; //!< データ数の2べき上界 vector<T> dat; vector<E> laz; LazySegmentTree() {} LazySegmentTree(vector<T> &v, F f, G g, H h, T ti, E ei) { Init(v, f, g, h, ti, ei); } void Init(vector<T> &v, F f, G g, H h, T ti, E ei){ this->f=f; this->g=g; this->h=h; this->ti=ti; this->ei=ei; while (n<(ll)v.size()) n<<=1; dat.resize(2*n-1, ti); laz.resize(2*n-1, ei); for (ll i=0; i<(ll)v.size(); i++) dat[n-1+i] = v[i]; for (ll i=n-2; i>=0; i--) dat[i] = f(dat[2*i+1], dat[2*i+2]); } void Update(ll a, ll b, E x){ update(a, b+1, x, 0, 0, n); } //[a,b]にxを作用 void update(ll a, ll b, E x, ll k, ll l, ll r){ //注目ノードk=[l,r) if (r<=a || b<=l) eval(k); //[l,r)が[a,b)と重ならない時 else if (a<=l && r<=b){ //[l,r)が[a,b)の内側の時 laz[k] = h(laz[k], x); eval(k); //親ノードが後でdat[k]を見るため、ここで評価しておく必要あり } else { //[l,r)が[a,b)から一部はみ出す時 eval(k); //子ノードを処理する前に評価しておく必要あり update(a, b, x, k*2+1, l, (l+r)/2); update(a, b, x, k*2+2, (l+r)/2, r); dat[k] = f(dat[k*2+1], dat[k*2+2]); } } T Range(ll a, ll b) { return range(a, b+1, 0, 0, n); }//[a,b]の値を取得 T range(ll a, ll b, ll k, ll l, ll r){ //注目ノードk=[l,r) if (r<=a || b<=l) return ti; //[l,r)が[a,b)と重ならない時 eval(k); //自分や子の値を見る前に評価が必要 if (a<=l && r<=b) return dat[k];//[l,r)が[a,b)の内側の時 return f(range(a, b, k*2+1, l, (l+r)/2), range(a, b, k*2+2, (l+r)/2, r)); //[l,r)が[a,b)から一部はみ出す時 } void eval(ll k){ if (laz[k] == ei) return; if (k<=n-2){ //子に作用素を配る laz[k*2+1] = h(laz[k*2+1], laz[k]); laz[k*2+2] = h(laz[k*2+2], laz[k]); } //自分に作用させる dat[k] = g(dat[k], laz[k]); laz[k] = ei; } void Dump(ll w=5){ auto disp=[&](ll x){ if (x==-INF) cerr << setw(w) << "-INF"; else if (x== INF) cerr << setw(w) << "INF"; else cerr << setw(w) << x; }; REP(i, n) for (ll k=i+n-1, m=1, p=1; k>=0; p=m, m*=k%2, k=(k==0)?-1:(k-1)/2){ if (m){ cerr << ((k<n-1)?" ┬ ":""); disp(dat[k]); cerr<<" /"; disp(laz[k]); } else{ cerr << ((p&~m) ?" ┘ ":""); } if (k==0) cerr << '\n'; } cerr << '\n'; } }; class UnionFindTree{ public: vll par, rank, size, tail; vll pList; UnionFindTree(ll num): par(num), rank(num), size(num, 1), tail(num), pList(num,-1) { REP(i, num) par[i] = i; rep(i, 0, num-1) tail[i]=i; } bool Same(ll x, ll y) { return Root(x) == Root(y); } ll Root(ll x) { return (par[x] == x) ? x : par[x]=Root(par[x]); } ll Size(ll x){ return size[Root(x)]; } void Unite(ll x, ll y){ x = Root(x); y = Root(y); if (x == y) return; if (rank[x] < rank[y]){ par[x] = y; pList[x] = tail[y]; tail[y] = tail[x]; } else{ par[y] = x; pList[y] = tail[x]; tail[x] = tail[y]; } if (rank[x] == rank[y]) rank[x]++; size[x] = size[y] = size[x] + size[y]; } }; void solve() { ll N, Q; cin >> N >> Q; vector<tuple<ll, ll, ll>> TAB = cinv3<ll, ll, ll>(Q); rep(i, 0, Q-1){ ll t; ll a; ll b; tie(t, a, b) = TAB[i]; get<1>(TAB[i])--; if (t==1){ get<2>(TAB[i])--; } } UnionFindTree uft2(N); rep(i, 0, Q-1){ ll t; ll a; ll b; tie(t, a, b) = TAB[i]; if (t!=1) continue; uft2.Unite(a, b); } vll segidx(N); rep(i, 0, N-1){ if (uft2.par[i] != i) continue; ll cnt=0; ll v = uft2.tail[i]; while (v!=-1){ segidx[v] = cnt++; v=uft2.pList[v]; } } vll ini(N); //ini[0] = 9; LazySegmentTree<ll,ll> sgt( ini, [](ll x, ll y){ return max(x, y); },//データ同士の合成関数 [](ll x, ll y){ return x + y; },//データxに作用素yを作用させる関数 [](ll x, ll y){ return x + y; },//作用素の合成関数 -INF, //データの単位元 実際の単位元をセットしないとだめ 0); //作用素の単位元 実際の単位元ではなくても、使わない値ならOK //sgt.Dump(); vll ans; UnionFindTree uft(N); rep(i, 0, Q-1){ ll t; ll a; ll b; tie(t, a, b) = TAB[i]; if (t==1){ uft.Unite(a, b); } else if (t==2){ ll u = uft.Root(a); ll idxA = segidx[u]; ll v = uft.tail[u]; ll idxB = segidx[v]; sgt.Update(idxB, idxA, b); //sgt.Dump(); } else{ ll idxA = segidx[a]; ll val = sgt.Range(idxA, idxA); ans.push_back(val); //sgt.Dump(); } } coutv(ans, '\n'); } int main(){ #if 1 solve(); #else ll t; cin >> t; rep(i, 0, t-1){ solve(); } #endif return 0; }