[WC2013]糖果公园

思路:

  带修改树上莫队(模板);

来,上代码:

#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的更多相关文章

  1. COGS1817. [WC2013]糖果公园

    1817. [WC2013]糖果公园 ★★★☆   输入文件:park.in   输出文件:park.out   简单对比时间限制:8 s   内存限制:512 MB [题目描述] Candyland ...

  2. bzoj 3052: [wc2013]糖果公园 带修改莫队

    3052: [wc2013]糖果公园 Time Limit: 250 Sec  Memory Limit: 512 MBSubmit: 506  Solved: 189[Submit][Status] ...

  3. 洛谷 P4074 [WC2013]糖果公园 解题报告

    P4074 [WC2013]糖果公园 糖果公园 树上待修莫队 注意一个思想,dfn序处理链的方法,必须可以根据类似异或的东西,然后根据lca分两种情况讨论 注意细节 Code: #include &l ...

  4. 【BZOJ3052】[wc2013]糖果公园 带修改的树上莫队

    [BZOJ3052][wc2013]糖果公园 Description Input Output Sample Input Sample Input Sample Output 84 131 27 84 ...

  5. [BZOJ3052][UOJ#58][WC2013]糖果公园

    [BZOJ3052][UOJ#58][WC2013]糖果公园 试题描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来 ...

  6. 【BZOJ】3052: [wc2013]糖果公园 树分块+带修改莫队算法

    [题目]#58. [WC2013]糖果公园 [题意]给定n个点的树,m种糖果,每个点有糖果ci.给定n个数wi和m个数vi,第i颗糖果第j次品尝的价值是v(i)*w(j).q次询问一条链上每个点价值的 ...

  7. 【Luogu P4074】[WC2013]糖果公园(树上带修改莫队)

    题目描述 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 \(n\) 个游 ...

  8. 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 ...

  9. P4074 [WC2013]糖果公园 树上莫队带修改

    题目链接 Candyland 有一座糖果公园,公园里不仅有美丽的风景.好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园游玩. 糖果公园的结构十分奇特,它由 nn 个游览点构 ...

随机推荐

  1. ElasticSearch学习笔记(四)-- 分布式

    1. 分布式介绍及cerebro cerebro插件 点击release下载 解压运行 访问9000端口,连接es的9200端口 2. 构建集群 新增一个节点 3. 副本与分片 再加入一个节点 4.  ...

  2. 网络策略中使用的 VLAN 属性

    TechNet 库 Windows Server Windows Server 2008 R2 und Windows Server 2008 按类别提供的 Windows Server 内容 Win ...

  3. Spring进阶—如何用Java代码实现邮件发送(一)

    相关文章: <Spring进阶—如何用Java代码实现邮件发送(二)> 在一些项目里面如进销存系统,对一些库存不足发出预警提示消息,招聘网站注册用户验证email地址等都需要用到邮件发送技 ...

  4. 《Cracking the Coding Interview》——第13章:C和C++——题目6

    2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...

  5. 【Minimum Window】cpp

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  6. ymal

    https://blog.csdn.net/beginya/article/details/76768968 https://www.jianshu.com/p/97222440cd08

  7. [转] mysql分区性能初探

    本文转自:http://www.cnblogs.com/acpp/archive/2010/08/09/1795464.html 一,      分区概念  分区允许根据指定的规则,跨文件系统分配单个 ...

  8. 软工实践Alpha冲刺(4/10)

    队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...

  9. HDU 3033 组合背包变形 I love sneakers!

    I love sneakers! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...

  10. 【bzoj3670】[Noi2014]动物园 KMP-next数组

    题目描述 近日,园长发现动物园中好吃懒做的动物越来越多了.例如企鹅,只会卖萌向游客要吃的.为了整治动物园的不良风气,让动物们凭自己的真才实学向游客要吃的,园长决定开设算法班,让动物们学习算法. 某天, ...