1817. [WC2013]糖果公园

★★★☆   输入文件:park.in   输出文件:park.out   简单对比
时间限制:8 s   内存限制:512 MB

【题目描述】

Candyland 有一座糖果公园,公园里不仅有美丽的风景、好玩的游乐项目,还有许多免费糖果的发放点,这引来了许多贪吃的小朋友来糖果公园玩。

糖果公园的结构十分奇特,它由 n 个游览点构成,每个游览点都有一个糖果发放处,我们可以依次将游览点编号为 1 至 n。有 n−1 条双向道路连接着这些游览点,并且整个糖果公园都是连通的,即从任何一个游览点出发都可以通过这些道路到达公园里的所有其它游览点。

糖果公园所发放的糖果种类非常丰富,总共 m 种,它们的编号依次为 1 至 m。每一个糖果发放处都只发放某种特定的糖果,我们用 ci 来表示 i 号游览点的糖果。

来到公园里游玩的游客都不喜欢走回头路,他们总是从某个特定的游览点出发前往另一个特定的游览点,并游览途中的景点,这条路线一定是唯一的。他们经过每个游览点,都可以品尝到一颗对应种类的糖果。

大家对不同类型的糖果的喜爱程度都不尽相同。根据游客们的反馈打分,我们得到了糖果的美味指数,第 i 种糖果的美味指数为 vi。另外,如果一位游客反复地品尝同一种类的糖果,他肯定会觉得有一些腻。根据量化统计,我们得到了游客第 i 次品尝某类糖果的新奇指数 wi,如果一位游客第 i 次品尝第 j 种糖果,那么他的愉悦指数 H 将会增加对应的美味指数与新奇指数的乘积,即 vjwi。这位游客游览公园的愉悦指数最终将是这些乘积的和。

当然,公园中每个糖果发放点所发放的糖果种类不一定是一成不变的。有时,一些糖果点所发放的糖果种类可能会更改(也只会是 m 种中的一种),这样的目的是能够让游客们总是感受到惊喜。

糖果公园的工作人员小 A 接到了一个任务,那就是根据公园最近的数据统计出每位游客游玩公园的愉悦指数。但数学不好的小 A 一看到密密麻麻的数字就觉得头晕,作为小 A 最好的朋友,你决定帮他一把。

【输入格式

第一行包含三个正整数 n,m,q,分别表示游览点个数、糖果种类数和操作次数。

第二行包含 m 个正整数 v1,v2,…,vm。

第三行包含 n 个正整数 w1,w2,…,wn。

第四行到第 n+2 行,每行包含两个正整数 ai,bi,表示这两个游览点之间有路径可以直接到达。

第 n+3 行包含 n 个正整数 c1,c2,…,cn。

接下来 q 行,每行包含三个整数 t,x,y,表示一次操作:

若 t 为 0,则 1≤x≤n,1≤y≤m,表示编号为 x 的游览点发放的糖果类型改为 y;

若 t 为 1,则 1≤x,y≤n,表示对出发点为 x,终止点为 y 的路线询问愉悦指数。

【输出格式

按照输入的先后顺序,对于每个 t 为 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

【样例输出】

84
131
27
84

【数据范围】

    对于所有的数据,1≤vi,wi≤10^6,1≤ai,bi≤n,1≤ci≤m,w1,w2,…,wn 是非递增序列,即对任意 1<i≤n,满足 wi≤wi−1。

莫队上树

分享:WC2013解题报告

#include<cstdio>
#include<algorithm>
#define FRE(name) freopen(#name".in","r",stdin);freopen(#name".out","w",stdout);
using namespace std;
inline int read(){
int x=,ch=getchar();
while(ch<'!') ch=getchar();
while(ch>'!') x=x*+ch-'',ch=getchar();
return x;
}
typedef long long ll;
const int N=1e5+;
struct edge{int to,next;}e[N<<];
struct ques{int u,v,id,t;}query[N];
struct change_col{int pos,new_col,last;}change[N];
int n,m,q,tot,head[N],v[N],w[N],col[N],last[N],sum[N];
int root,dep[N],fa[N],son[N],siz[N],top[N];
int block_num,block_size,belong[N],stack[N];
ll ans,res[N];bool vis[N];
inline bool cmp(const ques &a,const ques &b){
if(belong[a.u]==belong[b.u]&&belong[a.v]==belong[b.v])return a.t<b.t;
if(belong[a.u]==belong[b.u])return belong[a.v]<belong[b.v];
return belong[a.u]<belong[b.u];
}
inline void add(int x,int y){
e[++tot].to=y;e[tot].next=head[x];head[x]=tot;
e[++tot].to=x;e[tot].next=head[y];head[y]=tot;
}
int dfs_block(int rt){
int size=;siz[rt]=;
for(int i=head[rt];i;i=e[i].next){
if(e[i].to!=fa[rt]){
fa[e[i].to]=rt;
dep[e[i].to]=dep[rt]+;
size+=dfs_block(e[i].to);
siz[rt]+=siz[e[i].to];
if(siz[e[i].to]>siz[son[rt]]) son[rt]=e[i].to;
if(size>=block_size){
block_num++;
for(;size;size--) belong[stack[stack[]--]]=block_num;
}
}
}
stack[++stack[]]=rt;
return size+;
}
void dfs_top(int rt){
if(!son[rt]) return ;
top[son[rt]]=top[rt];dfs_top(son[rt]);
for(int i=head[rt];i;i=e[i].next){
if(e[i].to!=fa[rt]&&e[i].to!=son[rt]){
top[e[i].to]=e[i].to;
dfs_top(e[i].to);
}
}
}
inline int lca(int x,int y){
for(;top[x]!=top[y];x=fa[top[x]]){
if(dep[top[x]]<dep[top[y]]) swap(x,y);
}
return dep[x]<dep[y]?x:y;
}
inline void update(int x){
if(vis[x]){
ans-=(ll)v[col[x]]*(ll)w[sum[col[x]]];
sum[col[x]]--;
vis[x]=;
}
else{
sum[col[x]]++;
ans+=(ll)v[col[x]]*(ll)w[sum[col[x]]];
vis[x]=;
}
}
inline void modify(int x,int new_col){
if(!vis[x])
col[x]=new_col;
else{
update(x);
col[x]=new_col;
update(x);
}
}
inline void solve(int u,int v){
while(u!=v){
if(dep[u]>dep[v])
update(u),u=fa[u];
else
update(v),v=fa[v];
}
}
int main(){
FRE(park);
n=read();m=read();q=read();block_size=;
for(int i=;i<=m;i++) v[i]=read();
for(int i=;i<=n;i++) w[i]=read();
for(int i=,x,y;i<n;i++) x=read(),y=read(),add(x,y);
for(int i=;i<=n;i++) col[i]=read(),last[i]=col[i];
int cnt_change=,cnt_query=;
for(int i=,opt,x,y;i<=q;i++){
opt=read();x=read();y=read();
if(!opt){
cnt_change++;
change[cnt_change].pos=x;
change[cnt_change].new_col=y;
change[cnt_change].last=last[x];
last[x]=y;
}
else{
cnt_query++;
query[cnt_query].u=x;
query[cnt_query].v=y;
query[cnt_query].id=cnt_query;
query[cnt_query].t=cnt_change;
}
}
dep[root=]=;dfs_block(root);
for(block_num++;stack[];belong[stack[stack[]--]]=block_num);
top[root]=root;dfs_top(root);
sort(query+,query+cnt_query+,cmp);
for(int i=;i<=query[].t;i++)
modify(change[i].pos,change[i].new_col);
solve(query[].u,query[].v);
int ancestor=lca(query[].u,query[].v);
update(ancestor);
res[query[].id]=ans;
update(ancestor);
for(int i=;i<=cnt_query;i++){
for(int k=query[i-].t+;k<=query[i].t;k++)
modify(change[k].pos,change[k].new_col);
for(int k=query[i-].t;k>query[i].t;k--)
modify(change[k].pos,change[k].last);
solve(query[i-].u,query[i].u);//?
solve(query[i-].v,query[i].v);
ancestor=lca(query[i].u,query[i].v);
update(ancestor);
res[query[i].id]=ans;
update(ancestor);
}
for(int i=;i<=cnt_query;i++) printf("%lld\n",res[i]);
return ;
}

最新文章

  1. 微信官方开源UI库-WeUI
  2. 转: JAVA递归算法实例小结
  3. Educational Codeforces Round 16 A
  4. ITERATOR(迭代器)设计模式
  5. 安装MySQldb出错解决方法
  6. 第二百二十三天 how can I 坚持
  7. C++异常以及异常与析构函数
  8. BZOJ 1046 上升序列
  9. Swift—使用try?和try!区别-备
  10. hdu2059 龟兔赛跑
  11. oracle11g用户名密码不区分大小写
  12. 简单说下 Winform 的分页快速开发框架必须要实现的几个功能之一
  13. 《高性能Mysql》解读---Mysql的事务和多版本并发
  14. 口试C#概念
  15. angular-repeat
  16. Vue系列之 =&gt; 命名视图实现经典布局
  17. python之路---11 第一类对象 函数名 闭包 迭代器
  18. Excel VBA 使用笔记
  19. php 数据集转换树、递归重组节点信息多维数组(转)
  20. Net Core 使用外部登陆提供程序登陆的流程,以及身份认证的流程

热门文章

  1. 关于centos防火墙
  2. 无法启动Visual Studio的localhost web服务器,端口&quot;xxxx&quot;已被使用
  3. svn安装配置使用小总结
  4. Linux(二) 常用命令
  5. popcount 算法分析
  6. 【spring boot】12.spring boot对多种不同类型数据库,多数据源配置使用
  7. 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解【转】
  8. linux 系统时间硬件时间同步
  9. js 原生方法获取所有兄弟节点
  10. 自定义序列化4 (MFC调用C#的.dll)