AC日记——[WC2013]糖果公园 cogs 1817
思路:
带修改树上莫队(模板);
来,上代码:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 100005
#define ll long long struct QueryType {
ll u,v,t,id;
};
struct QueryType qu[maxn]; struct ChangeType {
ll to,x,h;
};
struct ChangeType cha[maxn]; ll n,m,q,ti[maxn],totq,totc,vi[maxn],wi[maxn];
ll siz,deep[maxn],f[maxn],top[maxn],bel[maxn],size[maxn];
ll E[maxn<<],V[maxn<<],head[maxn],cnt,dis[maxn],ans[maxn]; bool if_[maxn]; inline void in(ll &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void pre(ll now,ll fa)
{
deep[now]=deep[fa]+,size[now]=;
bel[now]=((++cnt)+)/siz,f[now]=fa;
for(ll i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
pre(V[i],now),size[now]+=size[V[i]];
}
} void dfs(ll now,ll chain)
{
ll pos=;top[now]=chain;
for(ll i=head[now];i;i=E[i])
{
if(V[i]==f[now]) continue;
if(size[V[i]]>size[pos]) pos=V[i];
}
if(pos==) return ;
dfs(pos,chain);
for(ll i=head[now];i;i=E[i])
{
if(V[i]==pos||V[i]==f[now]) continue;
dfs(V[i],V[i]);
}
} ll solve_lca(ll x,ll y)
{
while(top[x]!=top[y])
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} bool cmp(QueryType aa,QueryType bb)
{
if(bel[aa.u]==bel[bb.u])
{
if(bel[aa.v]==bel[bb.v]) return aa.t<bb.t;
else return bel[aa.v]<bel[bb.v];
}
else return bel[aa.u]<bel[bb.u];
} inline void change(ll x)
{
if(if_[cha[x].to])
{
cnt-=wi[ti[dis[cha[x].to]]]*vi[dis[cha[x].to]];
ti[dis[cha[x].to]]--;
}
cha[x].h=dis[cha[x].to];
dis[cha[x].to]=cha[x].x;
if(if_[cha[x].to])
{
ti[cha[x].x]++;
cnt+=wi[ti[cha[x].x]]*vi[cha[x].x];
}
} inline void unchange(ll x)
{
if(if_[cha[x].to])
{
cnt-=wi[ti[cha[x].x]]*vi[cha[x].x];
ti[cha[x].x]--;
}
dis[cha[x].to]=cha[x].h;
if(if_[cha[x].to])
{
ti[cha[x].h]++;
cnt+=wi[ti[cha[x].h]]*vi[cha[x].h];
}
} inline void updata(ll x)
{
if(if_[x])
{
cnt-=wi[ti[dis[x]]]*vi[dis[x]];
ti[dis[x]]--;
}
else
{
ti[dis[x]]++;
cnt+=wi[ti[dis[x]]]*vi[dis[x]];
}
if_[x]=!if_[x];
} inline void out(ll x)
{
if(x>) out(x/);
putchar(x%+);
} int main()
{
freopen("park.in","r",stdin);
freopen("park.out","w",stdout);
in(n),in(m),in(q);ll u,v;siz=sqrt(n);
for(ll i=;i<=m;i++) in(vi[i]);
for(ll i=;i<=n;i++) in(wi[i]);
for(ll i=;i<n;i++)
{
in(u),in(v);
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
for(ll i=;i<=n;i++) in(dis[i]);
cnt=,pre(,),dfs(,);ll ty;
for(ll i=;i<=q;i++)
{
in(ty);
if(ty)
{
in(qu[++totq].u),in(qu[totq].v),qu[totq].t=totc,qu[totq].id=totq;
if(bel[qu[totq].u]>bel[qu[totq].v]) swap(qu[totq].u,qu[totq].v);
}
else in(cha[++totc].to),in(cha[totc].x);
}
sort(qu+,qu+totq+,cmp),u=,v=,cnt=;ll t=;
for(ll no=;no<=totq;no++)
{
while(t<qu[no].t) change(++t);
while(t>qu[no].t) unchange(t--);
ll lca=solve_lca(u,qu[no].u);
while(u!=lca) updata(u),u=f[u];u=qu[no].u;
while(u!=lca) updata(u),u=f[u];u=qu[no].u;
lca=solve_lca(v,qu[no].v);
while(v!=lca) updata(v),v=f[v];v=qu[no].v;
while(v!=lca) updata(v),v=f[v];v=qu[no].v;
lca=solve_lca(u,v);
updata(lca),ans[qu[no].id]=cnt,updata(lca);
}
for(ll i=;i<=totq;i++) out(ans[i]),putchar('\n');
fclose(stdin),fclose(stdout);
return ;
}
AC日记——[WC2013]糖果公园 cogs 1817的更多相关文章
- COGS1817. [WC2013]糖果公园
		
1817. [WC2013]糖果公园 ★★★☆ 输入文件:park.in 输出文件:park.out 简单对比时间限制:8 s 内存限制:512 MB [题目描述] Candyland ...
 - bzoj 3052: [wc2013]糖果公园 带修改莫队
		
3052: [wc2013]糖果公园 Time Limit: 250 Sec Memory Limit: 512 MBSubmit: 506 Solved: 189[Submit][Status] ...
 - 洛谷 P4074 [WC2013]糖果公园 解题报告
		
P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...
 - 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队
		
[BZOJ3052][wc2013]糖果公园 Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 ...
 - [BZOJ3052][UOJ#58][WC2013]糖果公园
		
[BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...
 - 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法
		
[题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...
 - 【Luogu P4074】[WC2013]糖果公园(树上带修改莫队)
		
题目描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 \(n\) 个游 ...
 - BZOJ3052:[WC2013]糖果公园(树上莫队)
		
Description Input Output Sample Input 4 3 51 9 27 6 5 12 33 13 41 2 3 21 1 21 4 20 2 11 1 21 4 2 Sam ...
 - P4074 [WC2013]糖果公园   树上莫队带修改
		
题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...
 
随机推荐
- 《Cracking the Coding Interview》——第4章:树和图——题目9
			
2014-03-19 05:07 题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径.路径的起点和终点都可以是树的任意节点. 解法:我偷了个懒,直接把这棵树看成一个无 ...
 - python学习笔记五:模块和包
			
一.模块用import导入 cal.py: #!/usr/bin/python def add(x,y): return x+y if __name__ == '__main__': print ad ...
 - ACM二分搜索算法
			
二分搜索算法就是把要搜索的数据在搜索文本中根据情况进行折半,比如要在2 6 4 9 3 8 7 3 5中找到找到4的位置,那么可以考虑先把数据进行排序,然后把拍好后的数据的中间的那个数据和要查找的数据 ...
 - CS局域网射击
			
2/3D游戏:3D 辅助插件:角色控制器 游戏制作难度系数:中级 用到的其他工具:network 一.解决由于子弹射击速度过快而无法打到物体的问题 //方法一: ; Vector3 originalP ...
 - Python全栈工程师(包、模块 的导入)
			
ParisGabriel 每天坚持手写 一天一篇 决定坚持几年 为了梦想 为了信仰 Python人工智能从入门到精通 $ pip3 install tenso ...
 - 孤荷凌寒自学python第十四天python代码的书写规范与条件语句及判断条件式
			
孤荷凌寒自学python第十四天python代码的书写规范与条件语句及判断条件式 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 在我学习过的所有语言中,对VB系的语言比较喜欢,而对C系和J系 ...
 - windows auto activate
			
目前所支持的windows镜像都是未激活状态,未激活状态下很多功能无法使用. 以后将要实现的功能是,windows虚机启动后,网络正常后能与KMS服务器通信,自动激活key 目前想到两种办法: 1.b ...
 - HDU 4027 Can you answer these queries(线段树 + 观察 )
			
这题主要考察观察能力. 2^63最多只需要开7次根号就会变成1,当数字变成1之后就不需要再对其进行操作. 对于含有大于1数字的区间,向下更新. 对于数字全为1的区间,直接返回. #include &l ...
 - 软工实践Alpha冲刺(10/10)
			
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 完成所有界面的链接,整理与测试 展示GitHub当日代码/ ...
 - table & colgroup
			
table & colgroup // <caption>版本信息</caption> table = ` <table class="versions ...