题目大意:给定一棵n个点的树,每个点上有一个汉子或妹子,每人有一个权值,每次询问一条链上选出一对权值相等的男女有多少种选法。(n,q<=10^5)

做法:比较显然的树上莫队,熟悉序列莫队那套理论再推广到树上即可,我的树上莫队好像有点假,我是先树分块,对每块的根跑一遍dfs处理到各个点的链的信息并顺便处理一个端点在这个块内的询问,常数好像有点大,卡了半天常才过……学习了一下dalao的树上莫队,求一个进和出都算一遍的dfs序,链询问转化为区间询问,在区间内但不在链上的会被计算到两次所以被抵消了,感觉很奇妙。

丑陋代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
char BB[<<],*S=BB;
inline int read()
{
int x;char c;
while((c=*S++)<''||c>'');
for(x=c-'';(c=*S++)>=''&&c<='';)x=x*+c-'';
return x;
}
#define MN 100000
#define K 700
struct edge{int nx,t;}e[MN*+];
struct query{int x,y,id;}Q[MN+];
int a[MN+],b[MN+],cnt,h[MN+],en,fa[MN+],d[MN+],f[MN+],dep[MN+];
int rt,nw,u[MN+],A[MN+],B[MN+];
long long ans,C[MN+];
bool cmp(const query&a,const query&b){return f[a.x]<f[b.x];}
map<int,int> mp;
inline void ins(int x,int y)
{
e[++en]=(edge){h[x],y};h[x]=en;
e[++en]=(edge){h[y],x};h[y]=en;
}
void dfs(int x)
{
for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa[x])
{
fa[e[i].t]=x;dep[e[i].t]=dep[x]+;dfs(e[i].t);
d[x]=max(d[x],d[e[i].t]+);
}
if(d[x]==K||x==rt)f[x]=x,d[x]=-;
}
void build(int x)
{
for(int i=h[x];i;i=e[i].nx)if(e[i].t!=fa[x])
{
if(!f[e[i].t])f[e[i].t]=f[x];
build(e[i].t);
}
}
void DFS(int x,int fa)
{
d[x]=++cnt;
for(int i=h[x];i;i=e[i].nx)
if(e[i].t!=fa)DFS(e[i].t,x);
}
bool CMP(const query&a,const query&b){return d[a.y]<d[b.y];}
void cal(int x)
{
if(u[x]^=)ans+=a[x]?A[b[x]]:B[b[x]],a[x]?++B[b[x]]:++A[b[x]];
else ans-=a[x]?A[b[x]]:B[b[x]],a[x]?--B[b[x]]:--A[b[x]];
}
void solve(int x,int fr)
{
d[x]=MN+;cal(x);
for(;rt==f[Q[nw].x]&&Q[nw].y==x;++nw)
{
for(int i=Q[nw].x,u=;i!=rt;i=fa[i])
if(d[i]==MN+&&u)cal(rt),u=;else cal(i);
C[Q[nw].id]=ans;
for(int i=Q[nw].x,u=;i!=rt;i=fa[i])
if(d[i]==MN+&&u)cal(rt),u=;else cal(i);
}
for(int i=h[x];i;i=e[i].nx)
if(e[i].t!=fr)solve(e[i].t,x);
d[x]=;cal(x);
}
int main()
{
BB[fread(BB,,<<,stdin)]=;
int n=read(),i,j,q;
for(i=;i<=n;++i)a[i]=read();
for(i=;i<=n;++i)b[i]=mp[b[i]=read()]?mp[b[i]]:mp[b[i]]=++cnt;
for(i=;i<n;++i)ins(read(),read());
rt=%n+;dfs(rt);build(rt);
for(q=read(),i=;i<=q;++i)
{
Q[i].x=read();Q[i].y=read();Q[i].id=i;
if(dep[Q[i].x]-dep[f[Q[i].x]]>dep[Q[i].y]-dep[f[Q[i].y]])swap(Q[i].x,Q[i].y);
}
sort(Q+,Q+q+,cmp);
for(nw=;nw<=q;)
{
DFS(f[Q[nw].x],cnt=);
for(i=nw;f[Q[i].x]==f[Q[nw].x];++i);
sort(Q+nw,Q+i,CMP);
solve(rt=f[Q[nw].x],ans=);
}
for(i=;i<=q;++i)printf("%I64d\n",C[i]);
}

[Codeforces]852I - Dating的更多相关文章

  1. Codeforces 852I Dating 树上莫队

    Dating 随便树上莫队搞一搞就好啦. #include<bits/stdc++.h> #define LL long long #define LD long double #defi ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. github上传时出现error: src refspec master does not match any解决办法

    github上传时出现error: src refspec master does not match any解决办法 这个问题,我之前也遇到过,这次又遇到了只是时间间隔比较长了,为了防止以后再遇到类 ...

  2. java实现同步的两种方式

    同步是多线程中的重要概念.同步的使用可以保证在多线程运行的环境中,程序不会产生设计之外的错误结果.同步的实现方式有两种,同步方法和同步块,这两种方式都要用到synchronized关键字. 给一个方法 ...

  3. 源端控制的OpenFlow数据面

    OpenFlow 交换机一般采用 TCAM 存储和查找流表,从而带来了扩展性.成本和能耗的问题.TCAM 成本和能耗过高,存储容量有限,一般交换机中的 TCAM 仅能存储几千条流表项,对 OpenFl ...

  4. scrapy 模拟登陆

    import scrapy import urllib.request from scrapy.http import Request,FormRequest class LoginspdSpider ...

  5. AWS中,如果使用了ELB,出现outofservice

    平台:亚马逊AWS EC2 出现状况: 我创建了弹性平衡负载,也注册了实例,但是实例的状态一直是outofservice.为什么? 为什么会出现这个问题呢? 1:实例有问题: 2:负载平衡器创建的有问 ...

  6. 构建微服务开发环境8————Hello 微服务

    [内容指引] 1.用IDEA打开微服务项目; 2.更新Maven依赖: 3.IntelliJ IDEA JDK配置; 4.修改代码: 5.运行微服务: 6.将代码变更提交到Github. 经过前面的努 ...

  7. php的set_time_limit()函数

    set_time_limit(0); 括号里边的数字是执行时间,如果为零说明永久执行直到程序结束,如果为大于零的数字,则不管程序是否执行完成,到了设定的秒数,程序结束. 一个简单的例子,在网页里显示1 ...

  8. LeetCode & Q13-Roman to Integer-Easy

    Math String Description: Given a roman numeral, convert it to an integer. Input is guaranteed to be ...

  9. LeetCode & Q119-Pascal's Triangle II-Easy

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...

  10. Mego(2) - NET主流ORM框架分析

    接上文我们测试了各个ORM框架的性能,大家可以很直观的看到各个ORM框架与原生的ADO.NET在境删改查的性能差异.这里和大家分享下我对ORM框架的理解及一些使用经验. ORM框架工作原理 典型ORM ...