[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. String类中的toCharArray()方法

    toCharArray()方法  该方法的作用是返回一个字符数组,该字符数组中存放了当前字符串中的所有字符 public class toChar1{ public static void main( ...

  2. PQFitness

    <html> <head> <title></title> <style> *{margin:o;padding:0;} #header{ ...

  3. javascript类式继承模式#4——共享原型

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 【Perceptron Learning Algorithm】林轩田机器学习基石

    直接跳过第一讲.从第二讲Perceptron开始,记录这一讲中几个印象深的点: 1. 之前自己的直觉一直对这种图理解的不好,老按照x.y去理解. a) 这种图的每个坐标代表的是features:fea ...

  5. 最短路径(Floyd法)

    最短路径法: 算法的主要思想是:单独一条边的路径也不一定是最佳路径. 从任意一条单边路径开始.所有两点之间的距离是边的权的和,(如果两点之间没有边相连, 则为无穷大). 对于每一对顶点 u 和 v,看 ...

  6. Asp.net获取网站绝对路径的几种方法

    在编写ASP.NET应用程序的时候,有时候为了更好的进行控制静态文件的路径,以及网站部署过程中的虚拟路径等问题,采用绝对路径避免资源出现Not Found,下面先看看几种获取绝对路径的方法: 1. 以 ...

  7. Python全栈 MySQL 数据库 (SQL查询、备份、恢复、授权)

    ParisGabriel              每天坚持手写  一天一篇  决定坚持几年 为了梦想为了信仰    开局一张图   今天接着昨天的说   索引有4种:      普通 索引 :ind ...

  8. matlab使用摄像头人脸识别

    #关于matlab如何读取图片.视频.摄像头设备数据# 参见:http://blog.csdn.net/u010177286/article/details/45646173 但是,关于摄像头读取,上 ...

  9. HDU 4474 Yet Another Multiple Problem ( BFS + 同余剪枝 )

    没什么巧办法,直接搜就行. 用余数作为每个节点的哈希值. #include <cstdio> #include <cstring> #include <cstdlib&g ...

  10. JavaWeb笔记(十)非关系型数据库Redis

    Redis Redis是一款高性能的NOSQL系列的非关系型数据库 主流的NOSQL产品 键值(Key-Value)存储数据库 相关产品: Tokyo Cabinet/Tyrant.Redis.Vol ...