3052: [wc2013]糖果公园

本文详细解析了一种树上莫队算法实现方法,通过将树分块并维护路径信息来高效解决特定类型的问题。文章介绍了算法的具体实现过程,包括时间复杂度分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

3052: [wc2013]糖果公园

Time Limit: 200 Sec   Memory Limit: 512 MB
Submit: 962   Solved: 464
[ Submit][ Status][ Discuss]

Description

Input

Output

Sample Input

Sample Input

Sample Output

84
131
27
84

HINT


Source

[ Submit][ Status][ Discuss]


树上莫队,,今天一天几乎都花在这上面了
首先是按照bzoj1086的方法把树分块,每个块的大小控制在N^(2/3)
对于每个询问,假设路径为(u,v)且u所在的块的编号不大于v
把所有询问操作按照u为第一关键字,v为第二关键字,时间为第三关键字排序
我们维护的信息是当前这条路径的ans(除去lca)
转移的时候,设下一次的路径是(x,y)
先暴力转移时间
然后暴力从u走到x,路径上的点的信息取反
同样地从v走到y
ans的话,,把lca的信息补上即可
为什么这样做是对的呢???
vfleaking大神讲的非常有道理
最后是时间复杂度(苟蒻也只会证明这个了。。GG)
我们把树许多个分成了N^(2/3)的块
于是块的数量就不超过N(1/3)
显然,在同一块内,你怎么跑单次的复杂度都不超过O(N^(2/3))
对于u所在的块确定是,v所在的块若在块内跑O(N^(2/3)),若要跑向右边的块,,总和是N^(2/3)*N^(1/3) = N
至于每次确定两端点的块时,时间修改的总和是O(N)  右子树跑O(N^(1/3))次,左子树也是一样
所以时间的修改是O(N^(5/3)),路径同理
这个复杂度,,强行没有N^2反正可以过


写的时候Movetime函数写残了,,好蠢啊
点取反再取回来是要特判的!!!
得从cur for 到 target才对呀!!!!
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<cmath>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 1E5 + 10;
const int Siz = 1234;
typedef long long LL;

struct Mo{
	int pos,a,b;
	Mo(int _pos = 0,int _a = 0,int _b = 0) {pos = _pos; a = _a; b = _b;}
}M[maxn];

struct Qu{
	int L,R,t,beL,beR;
	Qu(int _L = 0,int _R = 0,int _beL = 0,int _beR = 0,int _t = 0) {
		L = _L; R = _R; beL = _beL; beR = _beR; t = _t;
		if (beL > beR) swap(L,R),swap(beL,beR);
	}
	bool operator < (const Qu &b) const {
		if (beL < b.beL) return 1;
		if (beL > b.beL) return 0;
		if (beR < b.beR) return 1;
		if (beR > b.beR) return 0;
		return t < b.t;	
	}
}Q[maxn]; 

int n,m,q,cnt,Root,top,t,s[maxn],co[maxn],fa[maxn][20],
	A,B,Lca,num[maxn],c[maxn],depth[maxn],belong[maxn];
LL now,ans[maxn],va[maxn],w[maxn];
bool vis[maxn];

vector <int> v[maxn];

int getint()
{
	int ret = 0;
	char ch = getchar();
	while (ch < '0' || '9' < ch) ch = getchar();
	while ('0' <= ch && ch <= '9') ret = ret*10 + ch - '0',ch = getchar();
	return ret;
}

void dfs(int x,int from)
{
	for (int i = 1; i < 20; i++) fa[x][i] = fa[fa[x][i-1]][i-1];
	int k = top;
	for (int i = 0; i < v[x].size(); i++) {
		int to = v[x][i];
		if (to == from) continue;
		depth[to] = depth[x] + 1;
 		fa[to][0] = x;
 		dfs(to,x);
 		if (top - k >= Siz) {
			++cnt;
			while (top != k) belong[s[top--]] = cnt;
 		}
	}
	s[++top] = x;
}

void XorV(int x)
{
	if (vis[x]) {
		now -= va[num[x]]*w[co[num[x]]];
		vis[x] = 0; --co[num[x]];
	}
	else {
		++co[num[x]]; vis[x] = 1;
		now += va[num[x]]*w[co[num[x]]];
	}
}

void XorPath(int p,int q)
{
	if (depth[p] < depth[q]) swap(p,q);
	while (depth[p] > depth[q]) XorV(p),p = fa[p][0];
	if (p == q) return;
	while (p != q) {
		XorV(p); p = fa[p][0];
		XorV(q); q = fa[q][0];
	}
}

int LCA(int p,int q) 
{
	if (depth[p] < depth[q]) swap(p,q);
	int Log; for (Log = 0; depth[p] - (1<<Log) >= 1; Log++); --Log;
	for (int j = Log; j >= 0; j--)
		if (depth[p] - (1<<j) >= depth[q])
			p = fa[p][j];
	if (p == q) return p;
	for (int j = Log; j >= 0; j--)
		if (fa[p][j] != fa[q][j])
			p = fa[p][j],q = fa[q][j];
	return fa[p][0];
}

void Movetime(int cur,int target)
{
	if (cur < target) {
		for (int i = cur + 1; i <= target; i++) {
			if (!M[i].pos) continue;
			bool flag = 0; 
			if (vis[M[i].pos]) XorV(M[i].pos),flag = 1;
			num[M[i].pos] = M[i].b; 
			if (flag) XorV(M[i].pos);
		}
	}
	else {
		for (int i = cur; i > target; i--) {
			if (!M[i].pos) continue;
			bool flag = 0; 
			if (vis[M[i].pos]) XorV(M[i].pos),flag = 1;
			num[M[i].pos] = M[i].a; 
			if (flag) XorV(M[i].pos);
		}
	}
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	n = getint(); m = getint(); q = getint();
	for (int i = 1; i <= m; i++) va[i] = getint();
	for (int i = 1; i <= n; i++) w[i] = getint();
	for (int i = 1; i < n; i++) {
		int x = getint(),y = getint();
		v[x].push_back(y);
		v[y].push_back(x);
	}
	for (int i = 1; i <= n; i++) num[i] = c[i] = getint();
	Root = n/2; depth[Root] = 1; dfs(Root,0); int tot = 0;
	while (top) belong[s[top--]] = cnt;
	for (int i = 1; i <= q; i++) {
		int typ = getint(),x = getint(),y = getint();
		if (typ) Q[++tot] = Qu(x,y,belong[x],belong[y],i);
		else M[i] = Mo(x,c[x],y),c[x] = y;
	}
	sort(Q + 1,Q + tot + 1);
	A = B = Lca = Root; t = 0;
	for (int i = 1; i <= tot; i++) {
		Movetime(t,Q[i].t); t = Q[i].t;
		XorPath(A,Q[i].L); A = Q[i].L;
		XorPath(B,Q[i].R); B = Q[i].R;
		Lca = LCA(A,B); XorV(Lca);
		ans[Q[i].t] = now; XorV(Lca);
	}
	for (int i = 1; i <= q; i++)
		if (ans[i])
			printf("%lld\n",ans[i]);
	return 0;
}

# P4074 [WC2013] 糖果公园 ## 题目描述 Candyland 有一座糖果公园公园里不仅有美丽的风景、好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩。 糖果公园的结构十分奇特,它由 $n$ 个游览点构成,每个游览点都有一个糖果发放处,我们可以依次将游览点编号为 $1$ 至 $n$。有 $n - 1$ 条双向道路连接着这些游览点,并且整个糖果公园都是连通的,即从任何一个游览点出发都可以通过这些道路到达公园里的所有其它游览点。 糖果公园所发放的糖果种类非常丰富,总共有 $m$ 种,它们的编号依次为 $1$ 至 $m$。每一个糖果发放处都只发放某种特定的糖果,我们用 $C_i$ 来表示 $i$ 号游览点的糖果。 来到公园里游玩的游客都不喜欢走回头路,他们总是从某个特定的游览点出发前往另一个特定的游览点,并游览途中的景点,这条路线一定是唯一的。他们经过每个游览点,都可以品尝到一颗对应种类的糖果。 大家对不同类型糖果的喜爱程度都不尽相同。 根据游客们的反馈打分,我们得到了糖果的美味指数, 第 $i$ 种糖果的美味指数为 $V_i$。另外,如果一位游客反复地品尝同一种类的糖果,他肯定会觉得有一些腻。根据量化统计,我们得到了游客第 $i$ 次品尝某类糖果的新奇指数 $W_i$。如果一位游客第 $i$ 次品尝第 $j$ 种糖果,那么他的愉悦指数 $H$ 将会增加对应的美味指数与新奇指数的乘积,即 $V_j \times W_i$。这位游客游览公园的愉悦指数最终将是这些乘积的和。 当然,公园中每个糖果发放点所发放的糖果种类不一定是一成不变的。有时,一些糖果点所发放的糖果种类可能会更改(也只会是 $m$ 种中的一种),这样的目的是能够让游客们总是感受到惊喜。 糖果公园的工作人员小 A 接到了一个任务,那就是根据公园最近的数据统计出每位游客游玩公园的愉悦指数。但数学不好的小 A 一看到密密麻麻的数字就觉得头晕,作为小 A 最好的朋友,你决定帮他一把。 ## 输入格式 从文件 `park.in` 中读入数据。 第一行包含三个正整数 $n, m, q$, 分别表示游览点个数、 糖果种类数和操作次数。 第二行包含 $m$ 个正整数 $V_1, V_2, \ldots, V_m$。 第三行包含 $n$ 个正整数 $W_1, W_2, \ldots, W_n$。 第四行到第 $n + 2$ 行,每行包含两个正整数 $A_i, B_i$,表示这两个游览点之间有路径可以直接到达。 第 $n + 3$ 行包含 $n$ 个正整数 $C_1, C_2, \ldots, C_n$。 接下来 $q$ 行, 每行包含三个整数 $Type, x, y$,表示一次操作: - 若 $Type$ 为 $0$,则 $1 \leq x \leq n$, $1 \leq y \leq m$,表示将编号为 $x$ 的游览点发放的糖果类型改为 $y$; - 若 $Type$ 为 $1$,则 $1 \leq x, y \leq n$,表示对出发点为 $x$,终止点为 $y$ 的路线询问愉悦指数。 ## 输出格式 输出到文件 `park.out` 中。 按照输入的先后顺序,对于每个 $Type$ 为 $1$ 的操作输出一行,用一个正整数表示答案。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 3 5 1 9 2 7 6 5 1 2 3 3 1 3 4 1 2 3 2 1 1 2 1 4 2 0 2 1 1 1 2 1 4 2 ``` ### 输出 #1 ``` 84 131 27 84 ``` ## 说明/提示 【样例解释】 我们分别用 ![](https://cdn.luogu.com.cn/upload/image_hosting/isw3ib3u.png) 代表 $C_i$ 为 $1$、 $2$、 $3$ 的节点,在修改之前: ![](https://cdn.luogu.com.cn/upload/image_hosting/ttkzii1u.png) 在将 $C_2$ 修改为 $1$ 之后: ![](https://cdn.luogu.com.cn/upload/image_hosting/izro364w.png) 【数据规模与约定】 对于所有的数据: $1 \leq V_i, W_i \leq 10^6$,$1 \leq A_i, B_i \leq n$, $1 \leq C_i \leq m$, $W_1, W_2, \ldots, W_n$ 是非递增序列,即对任意 $1 < i \leq n$, 满足 $W_i \le W_{i-1}$。 其它的限制条件如下表所示: ![QQ20180113072014.png](https://cdn.luogu.com.cn/upload/image_hosting/g6884nx1.png) #include <bits/stdc++.h> #define ll long long using namespace std; const ll N=2e6; struct node{ ll l,r,f,t,ans; }a[N+5]; struct node1{ ll x,y; }b[N+5]; ll n,m,q; ll c[N+5],v[N+5],w[N+5]; ll cnta,cntb; ll lenb,blk[N+5]; ll fst[N+5],lst[N+5],dfn[N+5],dfnn; ll xl[N+5],xll; ll sum[N+5]; vector<ll> e[N+5]; ll lp=1,rp,s,now; ll cntc[N+5],jl[N+5]; ll st[N+5][21],dep[N+5]; inline ll read() { ll x=0,f=1; char c=getchar(); while (c<'0' || c>'9') { if (c=='-') f=-1; c=getchar(); } while (c>='0' && c<='9') { x=x*10+c-'0'; c=getchar(); } return x*f; } bool cmp(node l1,node l2){ if(blk[l1.l]==blk[l2.l]){ if(blk[l1.r]==blk[l2.r]){ return l1.t<l2.t; } return blk[l1.r]<blk[l2.r]; } return blk[l1.l]<blk[l2.l]; } bool cmpf(node l1,node l2){ return l1.f<l2.f; } ll get(ll col,ll x){ return sum[x]*v[col]; } void dfs(ll x,ll fa){ fst[x]=++dfnn; dfn[dfnn]=x; dep[x]=dep[fa]+1; st[x][0]=fa; xl[x]=++xll; for(ll y:e[x]){ if(y==fa) continue; dfs(y,x); } lst[x]=++dfnn; dfn[dfnn]=x; return; } void add(ll p){ ll x=dfn[p],col=c[x]; jl[x]++; if(jl[x]==2){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void del(ll p){ ll x=dfn[p],col=c[x]; jl[x]--; if(jl[x]==1){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void addd(ll col){ s-=get(col,cntc[col]); cntc[col]++; s+=get(col,cntc[col]); return; } void dell(ll col){ s-=get(col,cntc[col]); cntc[col]--; s+=get(col,cntc[col]); return; } void solve(ll l,ll r,ll t,ll &ans){ while(l<lp){ lp--; add(lp); } while(lp<l){ del(lp); lp++; } while(rp<r){ rp++; add(rp); } while(r<rp){ del(rp); rp--; } while(now<t){ now++; // cout<<l<<"oooooo"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); } while(now>t){ // cout<<l<<"rrrrrrrrr"<<r<<endl; ll col=c[b[now].x],x=b[now].x; if(((l<=fst[x] && fst[x]<=r) || (l<=lst[x] && lst[x]<=r)) && jl[x]!=2){ dell(col); swap(c[b[now].x],b[now].y); col=c[b[now].x]; addd(col); } else swap(c[b[now].x],b[now].y); now--; } // cout<<lp<<"ooooooooooooooo"<<rp<<endl; ans=s; return; } ll lca(ll x,ll y){ if(dep[x]<dep[y]) swap(x,y); for(ll i=20;i>=0;i--){ ll xx=st[x][i]; if(dep[xx]>=dep[y]) x=xx; } if(x==y) return x; for(ll i=20;i>=0;i--){ ll xx=st[x][i],yy=st[y][i]; if(xx!=yy) x=xx,y=yy; } return st[x][0]; } int main(){ // freopen("P4074_4.in","r",stdin); n=read(),m=read(),q=read(); lenb=pow(n*2.0,2.0/3.0); ll sx=1; for(ll i=1;i<=2*n;i++){ blk[i]=sx; if(i%lenb==0) sx++; } for(ll i=1;i<=m;i++) v[i]=read(); for(ll i=1;i<=n;i++){ w[i]=read(); sum[i]=sum[i-1]+w[i]; } for(ll i=1;i<n;i++){ ll x,y; x=read(),y=read(); e[x].push_back(y); e[y].push_back(x); } for(ll i=1;i<=n;i++) c[i]=read(); for(ll i=1;i<=q;i++){ ll op; op=read(); if(op==0){ cntb++; b[cntb].x=read(),b[cntb].y=read(); } else{ cnta++; a[cnta].l=read(),a[cnta].r=read(); a[cnta].t=cntb,a[cnta].f=cnta; } } dfs(1,0); for(ll i=1;i<=20;i++){ for(ll j=1;j<=n;j++){ st[j][i]=st[st[j][i-1]][i-1]; // cout<<j<<" "<<i<<" "<<st[4][0]<<endl; } } // for(ll i=1;i<=n;i++){ // cout<<fst[i]<<"ooo"<<lst[i]<<endl; // } // return 0; sort(a+1,a+cnta+1,cmp); for(ll i=1;i<=cnta;i++){ ll x=a[i].l,y=a[i].r,lcaa=lca(x,y),xx,yy,ff=0; if(lcaa==x){ ff=1; xx=fst[x],yy=fst[y]; } else if(lcaa==y){ ff=1; xx=fst[y],yy=fst[x]; } else{ if(xl[x]>xl[y]) swap(x,y); xx=lst[x],yy=fst[y]; } // cout<<xx<<" "<<yy<<" "<<ff<<endl; solve(xx,yy,a[i].t,a[i].ans); if(!ff){ ll col=c[lcaa]; // cout<<lcaa<<endl; a[i].ans+=w[cntc[col]+1]*v[col]; } // printf("%lld\n",i); } sort(a+1,a+cnta+1,cmpf); for(ll i=1;i<=cnta;i++) printf("%lld\n",a[i].ans); return 0; } TLE,求调
08-02
调一下代码(WC2013糖果公园,1<=n,m,q<=1e5),RE了: #include<bits/stdc++.h> using namespace std; long long n, m, Q, block, top, v[200010], w[200010], cn[200010]; vector<long long> vec[200010]; long long ans, cnt1, cnt2, pt, pl, pr; long long stk[200010], t1[200010], t2[200010]; long long dep[200010], q[200010], fa[26][200010]; long long sum[200010], cnt[200010], st[200010], pp[200010]; struct node{ long long l, r, pp; long long time, qtime; friend bool operator<(node p, node q){ return (p.l / block != q.l / block) ? (p.l < q.l) : ((p.r / block != q.r / block ? (p.r < q.r) : (p.time < q.time))); } }b1[200010]; struct node2{ long long l, r, time; }b2[200010]; void dfs(long long x, long long fa){ stk[++ top] = x; t1[x] = top; for(auto i : vec[x]){ if(i == fa) continue; dfs(i, x); } stk[++ top] = x; t2[x] = top; } void bfs(long long rt){ memset(dep, 0x3f, sizeof(dep)); dep[0] = 0, dep[rt] = 1; long long hh = 0, tt = 0; q[0] = rt; while(hh <= tt){ long long t = q[hh ++]; for(auto i : vec[t]){ if(dep[i] > dep[t] + 1){ dep[i] = dep[t] + 1; q[++ tt] = i; fa[i][0] = t; for(long long k = 1; k <= 25; ++ k){ fa[i][k] = fa[fa[i][k - 1]][k - 1]; } } } } } long long lca(long long x, long long y){ if(dep[x] < dep[y]) swap(x, y); for(long long i = 25; i >= 0; -- i){ if(dep[fa[x][i]] >= dep[y]){ x = fa[x][i]; } } if(x == y) return x; for(long long i = 25; i >= 0; -- i){ if(fa[x][i] != fa[y][i]){ x = fa[x][i], y = fa[y][i]; } } return fa[x][0]; } void add(long long x){ st[x] ^= 1; if(st[x]){ cnt[cn[x]] ++; ans += v[cn[x]] * w[cnt[cn[x]]]; } else{ ans -= v[cn[x]] * w[cnt[cn[x]]]; cnt[cn[x]] --; } } int main(){ scanf("%lld %lld %lld", &n, &m, &Q); for(long long i = 1; i <= m; ++ i){ scanf("%lld", &v[i]); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &w[i]); } for(long long i = 1; i < n; ++ i){ long long x, y; scanf("%lld %lld", &x, &y); vec[x].push_back(y); vec[y].push_back(x); } for(long long i = 1; i <= n; ++ i){ scanf("%lld", &cn[i]); } dfs(1, -1); bfs(1); for(long long i = 1; i <= Q; ++ i){ long long op, l, r; scanf("%lld %lld %lld", &op, &l, &r); if(op == 0){ b2[++ cnt2].l = l; b2[cnt2].r = r; } else{ if(t1[l] > t1[r]) swap(l, r); long long p = lca(l, r); if(p == l){ b1[++ cnt1].l = t1[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = 0; } else{ b1[++ cnt1].l = t2[l]; b1[cnt1].r = t1[r]; b1[cnt1].pp = p; } b1[cnt1].time = cnt1; b1[cnt1].qtime = cnt2; } } block = max(1ll, (long long)cbrt(top * top)); sort(b1 + 1, b1 + cnt1 + 1); pt = 0, pl = 1, pr = 0; for(long long i = 1; i <= cnt1; ++ i){ while(pr < b1[i].r) ++ pr, add(stk[pr]); while(pr > b1[i].r) add(stk[pr]), -- pr; while(pl > b1[i].l) -- pl, add(stk[pl]); while(pl < b1[i].l) add(stk[pl]), ++ pl; while(pt < b1[i].qtime){ ++ pt; if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } } while(pt > b1[i].qtime){ if(st[b2[pt].l]){ add(b2[pt].l); swap(b2[pt].r, cn[b2[pt].l]); add(b2[pt].l); } else{ swap(b2[pt].r, cn[b2[pt].l]); } -- pt; } if(b1[i].pp){ add(b1[i].pp); } pp[b1[i].time] = ans; if(b1[i].pp){ add(b1[i].pp); } } for(long long i = 1; i <= cnt1; ++ i){ printf("%lld\n", pp[i]); } return 0; }
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值