結果
問題 | No.1197 モンスターショー |
ユーザー | HIR180 |
提出日時 | 2020-08-22 13:54:08 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 4,897 bytes |
コンパイル時間 | 4,691 ms |
コンパイル使用メモリ | 263,336 KB |
実行使用メモリ | 79,936 KB |
最終ジャッジ日時 | 2024-10-15 08:10:58 |
合計ジャッジ時間 | 54,607 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 16 ms
45,724 KB |
testcase_01 | AC | 22 ms
42,124 KB |
testcase_02 | AC | 16 ms
40,532 KB |
testcase_03 | AC | 16 ms
42,288 KB |
testcase_04 | AC | 16 ms
39,920 KB |
testcase_05 | AC | 16 ms
41,792 KB |
testcase_06 | AC | 16 ms
40,916 KB |
testcase_07 | AC | 393 ms
78,652 KB |
testcase_08 | AC | 276 ms
60,984 KB |
testcase_09 | AC | 1,052 ms
72,104 KB |
testcase_10 | AC | 2,151 ms
71,988 KB |
testcase_11 | AC | 809 ms
54,296 KB |
testcase_12 | AC | 307 ms
49,528 KB |
testcase_13 | AC | 386 ms
62,612 KB |
testcase_14 | AC | 1,348 ms
63,256 KB |
testcase_15 | AC | 704 ms
53,136 KB |
testcase_16 | AC | 2,172 ms
77,568 KB |
testcase_17 | AC | 2,126 ms
78,208 KB |
testcase_18 | AC | 596 ms
55,016 KB |
testcase_19 | AC | 1,819 ms
72,092 KB |
testcase_20 | AC | 206 ms
54,704 KB |
testcase_21 | AC | 499 ms
47,128 KB |
testcase_22 | AC | 57 ms
48,108 KB |
testcase_23 | AC | 1,828 ms
49,708 KB |
testcase_24 | AC | 1,069 ms
48,624 KB |
testcase_25 | AC | 422 ms
34,544 KB |
testcase_26 | AC | 925 ms
36,256 KB |
testcase_27 | AC | 1,348 ms
68,568 KB |
testcase_28 | AC | 1,193 ms
40,516 KB |
testcase_29 | AC | 619 ms
47,140 KB |
testcase_30 | AC | 421 ms
53,188 KB |
testcase_31 | AC | 607 ms
42,848 KB |
testcase_32 | AC | 373 ms
10,804 KB |
testcase_33 | AC | 328 ms
35,072 KB |
testcase_34 | TLE | - |
testcase_35 | TLE | - |
testcase_36 | TLE | - |
testcase_37 | TLE | - |
testcase_38 | TLE | - |
testcase_39 | TLE | - |
testcase_40 | TLE | - |
testcase_41 | -- | - |
testcase_42 | -- | - |
ソースコード
//Let's join Kaede Takagaki Fan Club !! #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; 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 mp make_pair #define eps 1e-7 #define INF 1000000000 #define fi first #define sc second #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() template<class T> void dmp(T a){ rep(i,a.size()) cout << a[i] << " "; cout << endl; } template<class T> bool chmax(T&a, T b){ if(a < b){ a = b; return 1; } return 0; } template<class T> bool chmin(T&a, T b){ if(a > b){ a = b; return 1; } return 0; } 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; template<class T> void add(T&a,T b){ a+=b; if(a >= mod) a-=mod; } #define SZ 100005 vector<int>edge[SZ]; P ar[SZ*2]={}; int pos[SZ]={},id=0,up[SZ],dep[SZ]; P mn[2][20][SZ*2]={}; int sz[SZ*2]={},in[SZ],out[SZ],rev[SZ*2]; struct adj_finder{ //SZは元の木の頂点数より大 //外部でedge[]に隣接状況を持って置く必要あり void dfs(int v,int u,int d){ pos[v] = in[v] = id; up[v] = u; dep[v] = d; rev[id] = v; ar[id++] = mp(d,id); for(int i=0;i<edge[v].size();i++){ if(edge[v][i] == u) continue; dfs(edge[v][i],v,d+1); rev[id] = v; ar[id++] = mp(d,id); } out[v] = id-1; } void prepare(){ dfs(1,-1,0); for(int i=0;i<id;i++){ mn[0][0][i] = ar[i]; mn[1][0][i] = mp(INF,INF); } for(int j=0;j<19;j++){ for(int i=0;i<id;i++){ if(i+(1<<j) >= id) rep(x,2) mn[x][j+1][i] = mn[x][j][i]; else{ mn[0][j+1][i] = min(mn[0][j][i], mn[0][j][i+(1<<j)]); mn[1][j+1][i] = mp(INF,INF); int best_v = rev[mn[0][j+1][i].second]; rep(x,2){ if(mn[x][j][i].second != INF && rev[mn[x][j][i].second] != best_v) mn[1][j+1][i] = min(mn[1][j+1][i], mn[x][j][i]); if(mn[x][j][i+(1<<j)].second != INF && rev[mn[x][j][i+(1<<j)].second] != best_v) mn[1][j+1][i] = min(mn[1][j+1][i], mn[x][j][i+(1<<j)]); } } } } for(int i=1;i<SZ*2;i++){ for(int j=0;j<20;j++){ if((1<<j) <= i && i <= (2<<j)){ sz[i] = j; break; } } } } int get1(int a,int b){ int len = b-a+1; int ty = sz[len]; P p = min(mn[0][ty][a],mn[0][ty][b-(1<<ty)+1]); return p.second; } int get_dist(int a, int b){ int c = rev[get1(min(pos[a],pos[b]), max(pos[a],pos[b]))]; return dep[a]+dep[b]-2*dep[c]; } int get2(int a,int b){ int len = b-a+1; int ty = sz[len]; P p = mp(INF,INF); int x = rev[get1(a,b)]; rep(i,2){ if(mn[i][ty][a].second != INF && rev[mn[i][ty][a].second] != x) p = min(p, mn[i][ty][a]); if(mn[i][ty][b-(1<<ty)+1].second != INF && rev[mn[i][ty][b-(1<<ty)+1].second] != x) p = min(p, mn[i][ty][b-(1<<ty)+1]); } return rev[p.second]; } int near_v(int u,int v){ if(in[u] <= in[v] && out[v] <= out[u]){ return get2(pos[v], out[u]); } else return up[u]; } }kaede; int n, k, q, c[100005]; int cnt[100005]; constexpr int B = 334; vector<P1>Q; ll sum[100005]; int sub[100005]; void dfs(int v, int u, int d){ sum[1] += 1LL * d * cnt[v]; rep(i, edge[v].size()){ if(edge[v][i] == u) continue; dfs(edge[v][i], v, d+1); sub[v] += sub[edge[v][i]]; } sub[v] += cnt[v]; } void dfs2(int v, int u){ rep(i, edge[v].size()){ if(edge[v][i] == u) continue; sum[edge[v][i]] = sum[v] + k - 2*sub[edge[v][i]]; dfs2(edge[v][i], v); } } int main(){ scanf("%d%d%d", &n, &k, &q); repn(i, k){ scanf("%d", &c[i]); } rep(i, n-1){ int a, b; scanf("%d%d", &a, &b); edge[a].pb(b); edge[b].pb(a); } kaede.prepare(); rep(i, q){ int ty; scanf("%d", &ty); int a, b = -1; if(ty == 1) scanf("%d%d", &a, &b); else scanf("%d", &a); Q.pb(mp(ty, mp(a, b))); } for(int i=0;i<q;i+=B){ memset(cnt, 0, sizeof(cnt)); memset(sub, 0, sizeof(sub)); repn(j, k) cnt[c[j]] ++; sum[1] = 0; dfs(1, -1, 0); dfs2(1, -1); vector<int>vec; for(int j=i;j<i+B && j<q;j++){ if(Q[j].fi == 1) vec.pb(Q[j].sc.fi); } SORT(vec); ERASE(vec); vector<int>gen; rep(i, vec.size()) gen.pb(c[vec[i]]); for(int j=i;j<i+B && j<q;j++){ if(Q[j].fi == 1) { c[Q[j].sc.fi] = Q[j].sc.sc; } else{ ll ans = sum[Q[j].sc.fi]; rep(k, vec.size()){ ans -= kaede.get_dist(Q[j].sc.fi, gen[k]); ans += kaede.get_dist(Q[j].sc.fi, c[vec[k]]); } printf("%lld\n", ans); } } } }