3052: [wc2013]糖果公园

Time Limit: 250 Sec  Memory Limit: 512 MB
Submit: 506  Solved: 189
[Submit][Status]

Description

Input

Output

Sample Input

Sample Input

 

Sample Output

84
131
27
84

HINT

  本来这道题想到了莫队算法,但是看到带修改就直接放弃了。结果看题解才发现带修改居然也能用莫队!!!之所以可以这样用,是因为修改的时间复杂度非常非常低,以至于可以在修改的时间轴上任意跳跃。具体细节自己去看其他题解啦。

  写的有点长,但是跑的还算比较快。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define MAXN 100100
#define MAXB 2200
#define MAXQ 100100
#define MAXV MAXN
#define MAXE MAXV*2
typedef long long qword;
inline int nextInt()
{
register int x=;
register int ch;
while (ch=getchar(),ch<'' || ch>'');
while (x=x*+ch-'',ch=getchar(),ch<='' && ch>='');
return x;
}
int n,m,q,sb;
struct Edge
{
int np;
Edge *next;
}E[MAXE],*V[MAXV];
int tope=-;
int que[MAXN];
void addedge(int x,int y)
{
E[++tope].np=y;
E[tope].next=V[x];
V[x]=&E[tope];
}
int depth[MAXN];
int pnt[MAXN];
int top[MAXN],bsiz[MAXN];
int belong[MAXN];
int topb=;
void bfs1()
{
Edge *ne;
int now;
int head=-,tail=;
que[]=;
pnt[]=;
belong[]=++topb;
top[]=;
bsiz[]=;
depth[]=;
while (head<tail)
{
now=que[++head];
for (ne=V[now];ne;ne=ne->next)
{
if (ne->np==pnt[now])continue;
que[++tail]=ne->np;
pnt[ne->np]=now;
depth[ne->np]=depth[now]+;
if (bsiz[belong[now]]<sb)
{
bsiz[belong[now]]++;
belong[ne->np]=belong[now];
top[ne->np]=top[now];
}else
{
belong[ne->np]=++topb;
bsiz[topb]=;
top[ne->np]=ne->np;
}
}
}
}
int jump[][MAXN];
void init_lca()
{
for (int i=;i<=n;i++)
jump[][i]=pnt[i];
for (int j=;j<;j++)
for (int i=;i<=n;i++)
jump[j][i]=jump[j-][jump[j-][i]];
}
int lca(int x,int y)
{
if (depth[x]<depth[y])swap(x,y);
int dep=depth[x]-depth[y];
for (int i=;i<;i++)
if (dep & (<<i))
x=jump[i][x];
if (x==y)return x;
for (int i=;i>=;i--)
if (jump[i][x]!=jump[i][y])
x=jump[i][x],y=jump[i][y];
return pnt[x];
}
int get_dis(int x,int y)
{
return depth[x]+depth[y]-*depth[lca(x,y)];
}
struct qur_t
{
int s,t,id;
int upper_pos;
qword ans;
}qur[MAXQ];
int topq=-;
bool cmp_qur_block(qur_t a1,qur_t a2)
{
if (belong[a1.s]!=belong[a2.s])
return belong[a1.s]<belong[a2.s];
if (belong[a1.t]!=belong[a2.t])
return belong[a1.t]<belong[a2.t];
return a1.id<a2.id;
}
bool cmp_qur_id(qur_t a1,qur_t a2)
{
return a1.id<a2.id;
}
int sweet[MAXN];
qword suprise[MAXN];
int candy[MAXN];
struct mdfy_t
{
int pos,cnew,cold,id;
}mdfy[MAXN];
int topm=-;
struct path
{
int s,t,a,ds;
path(int s,int t):s(s),t(t)
{
a=lca(s,t);
ds=depth[s]+depth[t]-*depth[a];
}
path(){}
bool inside(int x)
{
return get_dis(x,s)+get_dis(x,t)==ds;
}
void get_path(int *step,int &tops)
{
tops=-;
for (int x=s;x!=a;x=pnt[x])
step[++tops]=x;
tops=ds;
for (int x=t;x!=a;x=pnt[x])
step[tops--]=x;
step[tops--]=a;
tops=ds;
}
};
int tot[MAXN];
bool inside[MAXN];
int step[MAXN],tops;
int main()
{
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
int x,y,z;
n=nextInt(),m=nextInt(),q=nextInt();
sb=pow(n,2.0/);
for (int i=;i<=m;i++)
sweet[i]=nextInt();
for (int i=;i<=n;i++)
suprise[i]=nextInt();
for (int i=;i<=n;i++)suprise[i]+=suprise[i-];
for (int i=;i<n;i++)
{
x=nextInt(),y=nextInt();
addedge(x,y);
addedge(y,x);
}
for (int i=;i<=n;i++)
candy[i]=nextInt();
int opt;
for (int i=;i<=q;i++)
{
opt=nextInt();
if (opt==)
{
x=nextInt(),y=nextInt();
++topm;
mdfy[topm].pos=x;
mdfy[topm].cnew=y;
mdfy[topm].cold=candy[x];
candy[x]=y;
mdfy[topm].id=i;
}else
{
x=nextInt(),y=nextInt();
++topq;
qur[topq].s=x;
qur[topq].t=y;
qur[topq].upper_pos=topm+;
qur[topq].id=i;
}
}
for (int i=topm;i>=;i--)
candy[mdfy[i].pos]=mdfy[i].cold;
bfs1();
init_lca();
sort(qur,qur+topq+,cmp_qur_block);
for (int i=;i<qur[].upper_pos;i++)
candy[mdfy[i].pos]=mdfy[i].cnew;
int a;
a=lca(qur[].s,qur[].t);
qword nsum=;
for (x=qur[].s;x!=a;x=pnt[x])
{
inside[x]=true;
tot[candy[x]]++;
nsum+=(suprise[tot[candy[x]]]-suprise[tot[candy[x]]-])*sweet[candy[x]];
}
for (y=qur[].t;;y=pnt[y])
{
inside[y]=true;
tot[candy[y]]++;
nsum+=(suprise[tot[candy[y]]]-suprise[tot[candy[y]]-])*sweet[candy[y]];
if (y==a)break;
}
qur[].ans=nsum;
path pnow(qur[].s,qur[].t);
path pwalk;
for (int i=;i<=topq;i++)
{
for (int kk=;kk<;kk++)
{
if (kk==)
pwalk=path(qur[i-].s,qur[i].s);
else
pwalk=path(qur[i-].t,qur[i].t);
pwalk.get_path(step,tops);
bool flag=true;
for (int j=;j<=tops+;j++)
{
if (j!=tops+ && inside[step[j]])
{
inside[step[j]]=false;
tot[candy[step[j]]]--;
nsum+=(suprise[tot[candy[step[j]]]]-suprise[tot[candy[step[j]]]+])*sweet[candy[step[j]]];
}else
{
if (flag)
{
inside[step[j-]]=true;
tot[candy[step[j-]]]++;
nsum+=(suprise[tot[candy[step[j-]]]]-suprise[tot[candy[step[j-]]]-])*sweet[candy[step[j-]]];
flag=false;
}
if (j==tops+)break;
inside[step[j]]=true;
tot[candy[step[j]]]++;
nsum+=(suprise[tot[candy[step[j]]]]-suprise[tot[candy[step[j]]]-])*sweet[candy[step[j]]];
}
}
}
if (qur[i-].upper_pos<qur[i].upper_pos)
{
for (int j=qur[i-].upper_pos;j<qur[i].upper_pos;j++)
{
if (candy[mdfy[j].pos]!=mdfy[j].cold)throw;
candy[mdfy[j].pos]=mdfy[j].cnew;
if (inside[mdfy[j].pos])
{
tot[mdfy[j].cold]--;
nsum+=(suprise[tot[mdfy[j].cold]]-suprise[tot[mdfy[j].cold]+])*sweet[mdfy[j].cold];
tot[mdfy[j].cnew]++;
nsum+=(suprise[tot[mdfy[j].cnew]]-suprise[tot[mdfy[j].cnew]-])*sweet[mdfy[j].cnew];
}
}
}else
{
for (int j=qur[i-].upper_pos-;j>=qur[i].upper_pos;j--)
{
if (candy[mdfy[j].pos]!=mdfy[j].cnew)throw;
candy[mdfy[j].pos]=mdfy[j].cold;
if (inside[mdfy[j].pos])
{
tot[mdfy[j].cnew]--;
nsum+=(suprise[tot[mdfy[j].cnew]]-suprise[tot[mdfy[j].cnew]+])*sweet[mdfy[j].cnew];
tot[mdfy[j].cold]++;
nsum+=(suprise[tot[mdfy[j].cold]]-suprise[tot[mdfy[j].cold]-])*sweet[mdfy[j].cold];
}
}
}
qur[i].ans=nsum;
}
sort(qur,qur+topq+,cmp_qur_id);
for (int i=;i<=topq;i++)
printf("%lld\n",qur[i].ans);
}

bzoj 3052: [wc2013]糖果公园 带修改莫队的更多相关文章

  1. [BZOJ 3052] [wc2013] 糖果公园 【树上莫队】

    题目链接:BZOJ - 3052 题目分析 这道题就是非常经典的树上莫队了,并且是带修改的莫队. 带修改的莫队:将询问按照 左端点所在的块编号为第一关键字,右端点所在的块为第二关键字,位于第几次修改之 ...

  2. BZOJ.3052.[WC2013]糖果公园(树上莫队 带修改莫队)

    题目链接 BZOJ 当然哪都能交(都比在BZOJ交好),比如UOJ #58 //67376kb 27280ms //树上莫队+带修改莫队 模板题 #include <cmath> #inc ...

  3. 【bzoj3052】[wc2013]糖果公园 带修改树上莫队

    题目描述 给出一棵n个点的树,每个点有一个点权,点权范围为1~m.支持两种操作:(1)修改一个点的点权 (2)对于一条路径,求$\sum\limits_{i=1}^m\sum\limits_{j=1} ...

  4. BZOJ.2453.维护队列([模板]带修改莫队)

    题目链接 带修改莫队: 普通莫队的扩展,依旧从[l,r,t]怎么转移到[l+1,r,t],[l,r+1,t],[l,r,t+1]去考虑 对于当前所在的区间维护一个vis[l~r]=1,在修改值时根据是 ...

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

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

  6. bzoj 2120: 数颜色【带修改莫队】

    比较裸的带修莫队,对每个修改操作记一下它修改的位置修改前的颜色 然后正常莫队,每次对修改操作时间倒流一下即可 #include<iostream> #include<cstdio&g ...

  7. BZOJ 2120 数颜色 【带修改莫队】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2120 2120: 数颜色 Time Limit: 6 Sec  Memory Limit: ...

  8. BZOJ 2120 数颜色(带修改莫队)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2120 [题目大意] 给出一颜色序列,每次可以修改一个位置的颜色或者询问一个区间不同颜色 ...

  9. BZOJ3052 [wc2013] 糖果公园 【树上莫队】

    树上莫队和普通的序列莫队很像,我们把树进行dfs,然后存一个长度为2n的括号序列,就是一个点进去当作左括号,出来当作右括号,然后如果访问从u到v路径,我们可以转化成括号序列的区间,记录x进去的时候编号 ...

随机推荐

  1. jdk安装 java运行编译(不含语法)

    一.开发的准备 1.jdk的安装(window) (1)根据自己的电脑下载对应的jdk,并安装 (推荐安装在没有中文的目录中). 网站 http://www.oracle.com/technetwor ...

  2. cglib源码分析(三):Class生成策略

    cglib中生成类的工作是由AbstractClassGenerator的create方法使用相应的生成策略完成,具体代码如下: private GeneratorStrategy strategy ...

  3. mysql远程连接错误提醒:2013-Lost connection to MySQL server at ‘reading initial communication packet', system error: 0

    因为没有匹配/etc/hosts.allow. 解决方法: 1.在hosts.allow 文件中添加 mysqld:ALL [root@ucDB204 ~]# cat /etc/hosts.allow ...

  4. Linux关闭防火墙、SELinux

    使用root权限: Linux关闭防火墙: 1. chkconfig –list|grep iptables 2. chkconfig iptables off 永久关闭防火墙 3. chkconfi ...

  5. Python 基础【第三篇】输入和输出

    这里我们创建一个python(pytest)脚本用于学习测试(以后都为这个文件,不多做解释喽),这个文件必须要有执行权限的哈 1.创建pytest并赋予执行权限 [root@fengyuba_serv ...

  6. verilog语法:进程名

    always @ (posedge clk, negedge rstn)begin:State_flipflops if(!rstn) y<=S1; else y<=Y;end请问这段程序 ...

  7. poj 1007 DNA Sorting

    DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 95437   Accepted: 38399 Des ...

  8. HTML解析引擎:Jumony 开源项目

    Jumony Core首先提供了一个近乎完美的HTML解析引擎,其解析结果无限逼近浏览器的解析结果.不论是无结束标签的元素,可选结束标签的元素,或是标记属性,或是CSS选择器和样式,一切合法的,不合法 ...

  9. WKWebView详解

    WKWebView是在Apple的WWDC 2014随iOS 8和OS X 10.10出来的,是为了解决UIWebView加载速度慢.占用内存大的问题.使用UIWebView加载网页的时候,我们会发现 ...

  10. iOS imagePicker使用方法,方便使用!三步轻松搞定!

    自己总结的修改头像的方法,只为方便自己查询使用!转发 步骤:1.遵守代理协议 <UIImagePickerControllerDelegate,UINavigationControllerDel ...