結果
問題 | No.1197 モンスターショー |
ユーザー | HIR180 |
提出日時 | 2020-08-22 13:55:20 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 4,897 bytes |
コンパイル時間 | 5,448 ms |
コンパイル使用メモリ | 261,460 KB |
実行使用メモリ | 87,340 KB |
最終ジャッジ日時 | 2024-10-15 18:28:16 |
合計ジャッジ時間 | 38,613 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 9 ms
7,552 KB |
testcase_01 | AC | 10 ms
7,808 KB |
testcase_02 | AC | 9 ms
7,680 KB |
testcase_03 | AC | 8 ms
7,808 KB |
testcase_04 | AC | 9 ms
7,680 KB |
testcase_05 | AC | 8 ms
7,680 KB |
testcase_06 | AC | 8 ms
7,552 KB |
testcase_07 | AC | 337 ms
71,040 KB |
testcase_08 | AC | 246 ms
39,680 KB |
testcase_09 | AC | 807 ms
64,696 KB |
testcase_10 | AC | 1,770 ms
63,312 KB |
testcase_11 | AC | 700 ms
27,136 KB |
testcase_12 | AC | 322 ms
17,548 KB |
testcase_13 | AC | 319 ms
44,900 KB |
testcase_14 | AC | 1,102 ms
46,020 KB |
testcase_15 | AC | 654 ms
25,596 KB |
testcase_16 | AC | 1,673 ms
75,332 KB |
testcase_17 | AC | 1,672 ms
75,792 KB |
testcase_18 | AC | 535 ms
30,612 KB |
testcase_19 | AC | 1,530 ms
63,016 KB |
testcase_20 | AC | 192 ms
26,228 KB |
testcase_21 | AC | 608 ms
13,160 KB |
testcase_22 | AC | 53 ms
12,416 KB |
testcase_23 | AC | 1,655 ms
49,704 KB |
testcase_24 | AC | 995 ms
48,620 KB |
testcase_25 | AC | 404 ms
34,544 KB |
testcase_26 | AC | 902 ms
36,256 KB |
testcase_27 | AC | 1,123 ms
68,564 KB |
testcase_28 | AC | 1,049 ms
40,516 KB |
testcase_29 | AC | 585 ms
47,104 KB |
testcase_30 | AC | 378 ms
53,188 KB |
testcase_31 | AC | 536 ms
42,848 KB |
testcase_32 | AC | 468 ms
10,804 KB |
testcase_33 | AC | 308 ms
35,072 KB |
testcase_34 | AC | 2,589 ms
79,776 KB |
testcase_35 | AC | 2,527 ms
79,780 KB |
testcase_36 | AC | 2,457 ms
79,776 KB |
testcase_37 | AC | 2,435 ms
79,776 KB |
testcase_38 | AC | 2,493 ms
79,780 KB |
testcase_39 | AC | 2,401 ms
79,780 KB |
testcase_40 | TLE | - |
testcase_41 | AC | 10 ms
7,808 KB |
testcase_42 | AC | 10 ms
7,552 KB |
ソースコード
//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 = 500; 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); } } } }