BZOJ3052:[WC2013]糖果公园(树上莫队)
Description
.jpg)
Input
.jpg)
Output
.jpg)
Sample Input
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
Sample Output
131
27
84
HINT
.jpg)
.jpg)
Solution
还是改成括号序的写法吧……感觉好理解还好码……
一开始插入和删除函数是像下面这样分开写的,$vis$函数加加减减不知道哪里错了……如果有大爷看出来哪里错了和我说一声啊QAQ
void Ins(int x)
{
vis[x]++;
if (vis[x]==) Delicacy-=v[c[x]]*w[Keg[c[x]]], Keg[c[x]]--;
else Keg[c[x]]++, Delicacy+=v[c[x]]*w[Keg[c[x]]];
} void Del(int x)
{
vis[x]--;
if (vis[x]==) Keg[c[x]]++, Delicacy+=v[c[x]]*w[Keg[c[x]]];
else Delicacy-=v[c[x]]*w[Keg[c[x]]], Keg[c[x]]--;
} void Recov(int x,int val)
{
if (vis[x]==) Del(x), c[x]=val, Ins(x);
c[x]=val;
}
后来改成天下第一的写法对$vis$搞异或就过了……至今不知道为什么……
还有我才发现我二轮的树上莫队写的是对的啊……只不过$cmp$函数写错真的太开心了……
虽然多那30分也无济于事就是了……
话说我二轮的树上莫队既然是对的那我2月份写的那一发树分块糖果公园为什么T成狗啊
Code
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define N (200009)
#define LL long long
using namespace std; struct Que{int l,r,t,num,flag; LL ans;}Q[N];
struct Mdf{int pre,nxt,pos;}M[N];
struct Edge{int to,next;}edge[N<<];
LL Delicacy,v[N],w[N];
int n,m,q,x,y,opt,l=,r,flag,Q_num,M_num,Time,dfs_num;
int Keg[N*],vis[N],ID[N],a[N];
int Fir[N],Sec[N],f[N][],Depth[N],c[N];
int head[N],num_edge;
bool cmp1(Que a,Que b)
{
if (ID[a.l]==ID[b.l])
return ID[a.r]==ID[b.r]?a.t<b.t:ID[a.r]<ID[b.r];
else return ID[a.l]<ID[b.l];
}
bool cmp2(Que a,Que b) {return a.num<b.num;} void add(int u,int v)
{
edge[++num_edge].to=v;
edge[num_edge].next=head[u];
head[u]=num_edge;
} void Ins(int x)
{
if (vis[x]) Delicacy-=v[c[x]]*w[Keg[c[x]]], Keg[c[x]]--;
else Keg[c[x]]++, Delicacy+=v[c[x]]*w[Keg[c[x]]];
vis[x]^=;
} void Recov(int x,int val)
{
if (vis[x]) Ins(x),c[x]=val,Ins(x);
else c[x]=val;
} void MoQueue(int num)
{
while (Time<Q[num].t) Recov(M[Time+].pos,M[Time+].nxt), Time++;
while (Time>Q[num].t) Recov(M[Time].pos,M[Time].pre), Time--;
while (l<Q[num].l) Ins(a[l++]);
while (l>Q[num].l) Ins(a[--l]);
while (r<Q[num].r) Ins(a[++r]);
while (r>Q[num].r) Ins(a[r--]);
Q[num].ans=Delicacy;
if (Q[num].flag)
{
Ins(Q[num].flag);
Q[num].ans=Delicacy;
Ins(Q[num].flag);
}
} void DFS(int x,int fa)
{
f[x][]=fa;
for (int i=; i<=; ++i) f[x][i]=f[f[x][i-]][i-];
Depth[x]=Depth[fa]+; Fir[x]=++dfs_num;
a[dfs_num]=x;
for (int i=head[x]; i; i=edge[i].next)
if (edge[i].to!=fa) DFS(edge[i].to,x);
Sec[x]=++dfs_num; a[dfs_num]=x;
} int LCA(int x,int y)
{
if (Depth[x]<Depth[y]) swap(x,y);
for (int i=; i>=; --i)
if (Depth[f[x][i]]>=Depth[y]) x=f[x][i];
if (x==y) return x;
for (int i=; i>=; --i)
if (f[x][i]!=f[y][i]) x=f[x][i], y=f[y][i];
return f[x][];
}
int main()
{
scanf("%d%d%d",&n,&m,&q);
int unit=pow(n,2.0/3.0);
for (int i=; i<=(n*); ++i) ID[i]=(i-)/unit+;
for (int i=; i<=m; ++i) scanf("%lld",&v[i]);
for (int i=; i<=n; ++i) scanf("%lld",&w[i]);
for (int i=; i<=n-; ++i)
scanf("%d%d",&x,&y), add(x,y), add(y,x);
for (int i=; i<=n; ++i) scanf("%d",&c[i]);
DFS(,);
for (int i=; i<=q; ++i)
{
scanf("%d%d%d",&opt,&x,&y);
if (opt==)
{
if (Fir[x]>Fir[y]) swap(x,y);
int lca=LCA(x,y);
if (lca==x) x=Fir[x], y=Fir[y], flag=;
else x=Sec[x], y=Fir[y], flag=lca;
Q[++Q_num]=(Que){x,y,M_num,i,flag,};
}
else M[++M_num]=(Mdf){c[x],y,x}, c[x]=y;
}
for (int i=M_num; i>=; --i)
c[M[i].pos]=M[i].pre;
sort(Q+,Q+Q_num+,cmp1);
for (int i=; i<=Q_num; ++i)
MoQueue(i);
sort(Q+,Q+Q_num+,cmp2);
for (int i=; i<=Q_num; ++i)
printf("%lld\n",Q[i].ans);
}
BZOJ3052:[WC2013]糖果公园(树上莫队)的更多相关文章
- P4074 [WC2013]糖果公园 树上莫队带修改
题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...
- BZOJ.3052.[WC2013]糖果公园(树上莫队 带修改莫队)
题目链接 BZOJ 当然哪都能交(都比在BZOJ交好),比如UOJ #58 //67376kb 27280ms //树上莫队+带修改莫队 模板题 #include <cmath> #inc ...
- BZOJ 3052: [wc2013]糖果公园 | 树上莫队
题目: UOJ也能评测 题解 请看代码 #include<cstdio> #include<algorithm> #include<cstring> #includ ...
- 【WC2013】 糖果公园 - 树上莫队
问题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩.糖果公园的结构十分奇特,它由 n 个游览点构成, ...
- 【WC2013】糖果公园 [树上莫队]
题意: 一棵树,修改一个点的颜色,询问两点路径上每种颜色的权值$val[c]$*出现次数的权值$cou[w[c]]$的和 sro VFK 树上莫队 按照王室联邦的方法分块,块的大小直径个数有保证,并不 ...
- 洛谷P4074 [WC2013]糖果公园(莫队)
传送门 总算会树形莫队了…… 上次听说树形莫队是给树分块,实在看不懂.然后用括号序列的方法做总算能弄明白了 先说一下什么是括号序列,就是在$dfs$的时候,进入的时候记录一下,出去的时候也记录一下 拿 ...
- BZOJ3052 [wc2013] 糖果公园 【树上莫队】
树上莫队和普通的序列莫队很像,我们把树进行dfs,然后存一个长度为2n的括号序列,就是一个点进去当作左括号,出来当作右括号,然后如果访问从u到v路径,我们可以转化成括号序列的区间,记录x进去的时候编号 ...
- 【BZOJ-3052】糖果公园 树上带修莫队算法
3052: [wc2013]糖果公园 Time Limit: 200 Sec Memory Limit: 512 MBSubmit: 883 Solved: 419[Submit][Status] ...
- LUOGU P4074 [WC2013]糖果公园 (树上带修莫队)
传送门 解题思路 树上带修莫队,搞了两天..终于开O2+卡常大法贴边过了...bzoj上跑了183s..其实就是把树上莫队和带修莫队结合到一起,首先求出括号序,就是进一次出一次那种的,然后如果求两个点 ...
随机推荐
- C# 使用/配置Log4Net
1.首先在项目中添加Nuget程序包... 2.然后在NuGet窗体中搜索Log4Net,然后点击安装<安装过程可能会持续几分钟,请耐心等待> 3.在项目中添加一个Config文件,如已有 ...
- POJ Georgia and Bob-----阶梯博弈变形。
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6622 Accepted: 1932 D ...
- java的文件操作(1)
package com.test.file; import java.io.*; import java.util.ArrayList; import java.util.Date; import j ...
- Implementation:Dijkstra
#include <iostream> #include <cstdlib> #include <utility> #include <queue> u ...
- 用hmac验证客户端的合法性
服务器端程序 import os import hmac import socket def auth(conn): secret_key = b'rock' rand_b = os.urandom( ...
- Codeforces 981H:K Paths
传送门 考虑枚举一条路径 \(u,v\),求出所有边经过它的答案 只需要求出 \(u\) 的子树内选出 \(k\) 个可以重复的点,使得它们到 \(u\) 的路径不相交 不难发现,就是从 \(u\) ...
- Vue 2.0 pagination分页组件
最近写了一个分页组件,效果如下图: f-pagination.js文件 Vue.component('f-pagination',{ template:'<div class="fPa ...
- DOM增删操作(select动态增加和删除以及清空)
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- the cause of StringBuild class
如果我们对字符串进行拼接操作,每次拼接,都会创建一个新的String对象,既耗时,又浪费空间,而StringBuild类可以解决这个问题. 那么StringBuild类是如果解决的呢? 因为Strin ...
- node(6)angular介绍
一.angular 的介绍 AngularJS[1] 诞生于2009年,由Misko Hevery 等人创建,后为Google所收购.是一款优秀的前端JS框架,已经被用于Google的多款产品当中. ...