結果
問題 | No.1790 Subtree Deletion |
ユーザー | HIR180 |
提出日時 | 2021-12-21 01:07:21 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 172 ms / 3,000 ms |
コード長 | 7,898 bytes |
コンパイル時間 | 3,912 ms |
コンパイル使用メモリ | 196,232 KB |
実行使用メモリ | 53,504 KB |
最終ジャッジ日時 | 2024-09-15 15:29:50 |
合計ジャッジ時間 | 7,144 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 32 ms
39,680 KB |
testcase_01 | AC | 32 ms
39,680 KB |
testcase_02 | AC | 32 ms
39,680 KB |
testcase_03 | AC | 166 ms
53,376 KB |
testcase_04 | AC | 170 ms
53,376 KB |
testcase_05 | AC | 169 ms
53,504 KB |
testcase_06 | AC | 171 ms
53,376 KB |
testcase_07 | AC | 172 ms
53,376 KB |
testcase_08 | AC | 45 ms
39,680 KB |
testcase_09 | AC | 164 ms
53,376 KB |
testcase_10 | AC | 169 ms
53,376 KB |
testcase_11 | AC | 169 ms
53,376 KB |
testcase_12 | AC | 147 ms
52,736 KB |
testcase_13 | AC | 133 ms
50,560 KB |
testcase_14 | AC | 59 ms
42,624 KB |
ソースコード
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <ctime> #include <cassert> #include <string> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <cassert> #include <iomanip> #include <chrono> #include <random> #include <bitset> #include <complex> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; #define int long long //#define L __int128 typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define eb emplace_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define a first #define b second #define fi first #define sc second #define rng(i,a,b) for(int i=(int)(a);i<(int)(b);i++) #define rep(i,x) for(int i=0;i<x;i++) #define repn(i,x) for(int i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) #define all(x) x.begin(),x.end() #define si(x) int(x.size()) #ifdef LOCAL #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl #else #define dmp(x) void(0) #endif template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;} template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;} template<class t> using vc=vector<t>; template<class t,class u> ostream& operator<<(ostream& os,const pair<t,u>& p){ return os<<"{"<<p.fi<<","<<p.sc<<"}"; } template<class t> ostream& operator<<(ostream& os,const vc<t>& v){ os<<"{"; for(auto e:v)os<<e<<","; return os<<"}"; } //https://codeforces.com/blog/entry/62393 struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.it/splitmix64.c x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(pair<int,int> x)const{ return operator()(uint64_t(x.first)<<32|x.second); } }; //unordered_set -> dtype, null_type //unordered_map -> dtype(key), dtype(value) using namespace __gnu_pbds; template<class t,class u> using hash_table=gp_hash_table<t,u,custom_hash>; template<class T> void g(T &a){ cin >> a; } template<class T> void o(const T &a,bool space=false){ cout << a << (space?' ':'\n'); } //ios::sync_with_stdio(false); const ll mod = 998244353; //const ll mod = 1000000007; mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count()); template<class T> void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } ll modpow(ll x,ll n){ ll res=1; while(n>0){ if(n&1) res=res*x%mod; x=x*x%mod; n>>=1; } return res; } #define _sz 1 ll F[_sz],R[_sz]; void make(){ F[0] = 1; for(int i=1;i<_sz;i++) F[i] = F[i-1]*i%mod; R[_sz-1] = modpow(F[_sz-1], mod-2); for(int i=_sz-2;i>=0;i--) R[i] = R[i+1] * (i+1) % mod; } ll C(int a,int b){ if(b < 0 || a < b) return 0; return F[a]*R[b]%mod*R[a-b]%mod; } #define SZ 200005 int n; vector<P>edge[SZ]; int up[20][SZ],dep[SZ], in[SZ], out[SZ], val[SZ]; struct dbling{ bool ready = 0; int id = 0; void dfs(int v,int u,int d,int upp){ up[0][v] = u; dep[v] = d; in[v] = id++; if(u != -1) val[in[v]] = upp; //cout<<in[v]<<" "<<upp<<endl; rep(i,edge[v].size()){ if(edge[v][i].a == u) continue; dfs(edge[v][i].a, v, d+1, edge[v][i].b); } out[v] = id-1; } void prepare(){ dfs(1,-1,0,0); rep(j,19){ rep(i,SZ){ if(up[j][i] == -1) up[j+1][i] = up[j][i]; else up[j+1][i] = up[j][up[j][i]]; } } ready = 1; } int get(int a,int b){ if(dep[a] > dep[b]) swap(a,b); int D = dep[b]-dep[a]; rep(i,20){ if(((D>>i)&1)) b = up[i][b]; } if(a == b) return a; for(int i=19;i>=0;i--){ if(up[i][a] != up[i][b]){ a = up[i][a]; b = up[i][b]; } } return up[0][a]; } int dist(int a,int b){ int c = get(a,b); return dep[a]+dep[b]-2*dep[c]; } int dist(int a,int b,int c){ //assuming c is lca of (a,b) return dep[a]+dep[b]-2*dep[c]; } int go_up(int v,int a){ if(dep[v] < a) return -1; rep(i,20) if(((a>>i)&1)) v = up[i][v]; return v; } //a ---- b //と並べたときのx番目 (1-indexed) //存在しない場合は-1を返す //verified : opencup Warsaw I int v_on_path(int a,int b,int x){ int c = get(a,b); int d = dist(a,b,c); if(x <= 0 || x > d+1) return -1; if(x <= dep[a]-dep[c]+1){ return go_up(a, x-1); } else{ x = d+2-x; return go_up(b, x-1); } } }kaede; template<typename F, typename T> struct segtree{ int sz; vector<T>seg; const F f; const T e; segtree(int n, const F f, const T e): f(f), e(e) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2*sz, e); } void init(int n){ sz = 1; while(sz < n) sz <<= 1; seg.assign(2*sz, e); } //mrg...apply f or not void update(int a, T p, bool mrg){ a += sz-1; if(mrg) seg[a] = f(seg[a], p); else seg[a] = p; while(a){ a = (a-1)/2; seg[a] = f(seg[a*2+1], seg[a*2+2]); } } void make(vector<T>a){ rep(i, a.size()) seg[i+sz-1] = a[i]; for(int i=sz-2;i>=0;i--) seg[i] = f(seg[i*2+1], seg[i*2+2]); } T query(int a, int b){ a += sz-1, b += sz-1; T L = e, R = e; while(a <= b){ if((a&1) == 0) { L = f(L, seg[a++]); } if((b&1) == 1) { R = f(seg[b--], R); } if(a > b) break; a = (a-1) >> 1; b = (b-1) >> 1; } return f(L, R); } int find_left(int a, int b, int k, int l, int r, T v){ if(r < a || b < l) return INF; if(a <= l && r <= b){ if(f(seg[k], v) == seg[k]){ while(k < sz-1){ if(f(seg[k*2+1], v) == seg[k*2+1]) k = k*2+1; else k = k*2+2; } return k - (sz-1); } return INF; } int ret = find_left(a, b, k*2+1, l, (l+r)/2, v); if(ret != INF) return ret; return find_left(a, b, k*2+2, 1+(l+r)/2, r, v); } //NOT VERIFIED int find_right(int a, int b, int k, int l, int r, T v){ if(r < a || b < l) return -INF; if(a <= l && r <= b){ if(f(seg[k], v) == seg[k]){ while(k < sz-1){ if(f(seg[k*2+2], v) == seg[k*2+2]) k = k*2+2; else k = k*2+1; } return k - (sz-1); } return -INF; } int ret = find_right(a, b, k*2+2, 1+(l+r)/2, r, v); if(ret != -INF) return ret; return find_right(a, b, k*2+1, l, (l+r)/2, v); } //[a, b]でf(*, v)=*となる最小の*を返す 無ければINF int find_left(int a, int b, T v){ return find_left(a, b, 0, 0, sz-1, v); } //NOT VERIFIED //[a, b]でf(*, v)=*となる最大の*を返す 無ければ-INF int find_right(int a, int b, T v){ return find_right(a, b, 0, 0, sz-1, v); } }; //グローバルにおきたい時は //f, eを定義して auto f = [](int a, int b){ return a^b; }; int e = 0; segtree<decltype(f), decltype(e)>seg((1<<17), f, e); void solve(){ cin>>n; rep(i, n-1){ int a, b, c; cin>>a>>b>>c; edge[a].eb(b, c); edge[b].eb(a, c); } kaede.prepare(); for(int i=1;i<n;i++) seg.update(i, val[i], true); set<int>zan; for(int i=1;i<n;i++) zan.insert(i); int q; cin >> q; while(q--){ int t, x; cin >> t >> x; if(t == 1){ int beg = in[x], en = out[x]; auto it = zan.lower_bound(beg); while(it != zan.end() and (*it) <= en){ seg.update(*it, 0, false); it = zan.erase(it); } } else{ int beg = in[x]+1, en = out[x]; o(seg.query(beg, en)); } } } signed main(){ cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20); int t; t = 1; //cin >> t; while(t--) solve(); }