結果

問題 No.1197 モンスターショー
ユーザー chocoruskchocorusk
提出日時 2020-08-22 18:12:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 323 ms / 3,000 ms
コード長 3,965 bytes
コンパイル時間 1,452 ms
コンパイル使用メモリ 135,680 KB
実行使用メモリ 24,164 KB
最終ジャッジ日時 2024-04-23 17:58:21
合計ジャッジ時間 9,195 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 101 ms
19,432 KB
testcase_08 AC 71 ms
12,800 KB
testcase_09 AC 146 ms
19,696 KB
testcase_10 AC 217 ms
19,204 KB
testcase_11 AC 114 ms
8,824 KB
testcase_12 AC 65 ms
6,512 KB
testcase_13 AC 78 ms
13,984 KB
testcase_14 AC 173 ms
14,392 KB
testcase_15 AC 108 ms
8,492 KB
testcase_16 AC 233 ms
22,700 KB
testcase_17 AC 223 ms
23,060 KB
testcase_18 AC 92 ms
9,916 KB
testcase_19 AC 211 ms
19,000 KB
testcase_20 AC 46 ms
8,640 KB
testcase_21 AC 96 ms
5,376 KB
testcase_22 AC 13 ms
5,376 KB
testcase_23 AC 192 ms
15,068 KB
testcase_24 AC 141 ms
15,116 KB
testcase_25 AC 78 ms
11,084 KB
testcase_26 AC 130 ms
11,564 KB
testcase_27 AC 188 ms
21,072 KB
testcase_28 AC 147 ms
12,596 KB
testcase_29 AC 121 ms
14,680 KB
testcase_30 AC 94 ms
16,564 KB
testcase_31 AC 96 ms
13,428 KB
testcase_32 AC 86 ms
5,376 KB
testcase_33 AC 67 ms
11,284 KB
testcase_34 AC 303 ms
24,164 KB
testcase_35 AC 297 ms
24,164 KB
testcase_36 AC 307 ms
24,040 KB
testcase_37 AC 320 ms
24,036 KB
testcase_38 AC 299 ms
24,056 KB
testcase_39 AC 287 ms
24,052 KB
testcase_40 AC 323 ms
23,928 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
struct LinkCutTree{
	struct Node{
		Node *l, *r, *p;
		bool rev;
		int idx, val, sz, szl;
		ll cost, sum, suml, costs, sum2;
		Node(int val, int idx, ll cost):cost(cost), costs(cost), sum(0), sum2(0), suml(0), idx(idx), val(val), sz(val), szl(0), rev(false), l(nullptr), r(nullptr), p(nullptr){}
		bool is_root(){
			return !p || (p->l!=this && p->r!=this);
		}
	};
	vector<Node*> v;
	LinkCutTree(int n){
		v.resize(n);
		for(int i=0; i<n; i++) v[i]=new Node(0, i, 0);
	}
	void toggle(Node *t){
		if(!t) return;
		swap(t->sum, t->sum2);
		swap(t->l, t->r);
		t->rev^=true;
	}
	void push(Node *t){
		if(!t) return;
		if(t->rev){
			toggle(t->l);
			toggle(t->r);
			t->rev=false;
		}
	}
	void update(Node *t){
		t->sz=t->val+t->szl, t->costs=t->cost;
		if(t->l) t->sz+=t->l->sz, t->costs+=t->l->costs;
		if(t->r) t->sz+=t->r->sz, t->costs+=t->r->costs;
		t->sum=t->suml+t->cost*t->szl;
		t->sum2=t->suml+t->cost*t->szl;
		if(t->l) t->sum+=t->l->sum, t->sum2+=t->l->sum2, t->sum+=t->l->costs*(t->val+t->szl), t->sum2+=t->l->sz*t->cost;
		if(t->r) t->sum+=t->r->sum, t->sum2+=t->r->sum2, t->sum+=t->r->sz*t->cost, t->sum2+=t->r->costs*(t->val+t->szl);
		if(t->l && t->r) t->sum+=t->l->costs*t->r->sz, t->sum2+=t->r->costs*t->l->sz;
	}
	void rotate(Node *t){
		Node *p=t->p, *pp=p->p, *ch;
		if(p->l==t) ch=t->r;
		else ch=t->l;
		if(pp){
			if(pp->l==p) pp->l=t;
			else if(pp->r==p) pp->r=t;
		}
		t->p=pp;
		if(p->l==t) t->r=p;
		else t->l=p;
		p->p=t;
		if(p->l==t) p->l=ch;
		else p->r=ch;
		if(ch) ch->p=p;
		update(p);
		update(t);
	}
	int state(Node *t){
		if(t->is_root()) return 0;
		else if(t->p->l==t) return 1;
		else return -1;
	}
	void splay(Node *t){
		push(t);
		while(state(t)){
			if(!state(t->p)){
				push(t->p); push(t);
				rotate(t);
			}else{
				push(t->p->p); push(t->p); push(t);
				if(state(t->p)==state(t)){
					rotate(t->p);
					rotate(t);
				}else{
					rotate(t);
					rotate(t);
				}
			}
		}
	}
	Node *expose(Node *t){
		Node *rp=nullptr;
		for(Node *now=t; now; now=now->p){
			splay(now);
			if(rp){
				now->szl-=rp->sz;
				now->suml-=rp->sum;
			}
			if(now->r){
				now->szl+=now->r->sz;
				now->suml+=now->r->sum;
			}
			now->r=rp;
			update(now);
			rp=now;
		}
		splay(t);
		return rp;
	}
	void link(Node *c, Node *p){
		expose(c);
		expose(p);
		p->r=c;
		c->p=p;
		update(p);
	}
	void cut(Node *c){
		expose(c);
		c->l->p=nullptr;
		c->l=nullptr;
		update(c);
	}
	void evert(Node *t){
		expose(t);
		toggle(t);
		push(t);
	}
};
int main()
{
	int n, k, q;
	scanf("%d %d %d", &n, &k, &q);
	LinkCutTree lct(n);
	using node=LinkCutTree::Node;
    int c[100010];
    for(int i=0; i<k; i++){
        scanf("%d", &c[i]);
        c[i]--;
        lct.v[c[i]]->val++;
    }
    for(int i=0; i<n-1; i++){
        int a, b;
        scanf("%d %d", &a, &b);
        a--; b--;
        node *e=new node(0, i+n, 1);
        lct.v.push_back(e);
		lct.link(e, lct.v[a]);
		lct.evert(e);
		lct.link(e, lct.v[b]);
    }
    while(q--){
        int t; scanf("%d", &t);
        if(t==1){
            int p, d;
            scanf("%d %d", &p, &d);
            p--; d--;
            lct.expose(lct.v[c[p]]);
            lct.v[c[p]]->val--;
            c[p]=d;
            lct.expose(lct.v[c[p]]);
            lct.v[c[p]]->val++;
        }else{
            int e; scanf("%d", &e);
            e--;
            lct.evert(lct.v[e]);
            printf("%lld\n", lct.v[e]->sum);
        }
    }
	return 0;
}
0