问题相当于统计$且\sum_{l\le x<y\le r且lca(x,y)=x}1=c(sz[x],2)-\sum_{son}c(sz[son],2)$,考虑用莫队来维护区间,那么相当于要支持:1.某个点到根的链修改;2.询问某个点的上述式子
树链剖分维护:对于轻儿子,将这个权值加入父亲,复杂度$o(n\sqrt{n}\log n)$;对于重儿子,由于只有单点,用可持久化线段树来维护,复杂度$o(n\log n)$
由于复杂度较高,可能需要卡一下常数

  1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define K 450
5 #define ll long long
6 #define L (k<<1)
7 #define R (L+1)
8 #define mid (l+r>>1)
9 struct ji{
10 int nex,to;
11 }edge[N<<1];
12 struct qu{
13 int x,y,z,id;
14 }q[N];
15 int E,V,n,m,r,x,y,head[N],fa[N],sz[N],mx[N],id[N],top[N],ro[N],f[N*20],ls[N*20],rs[N*20];
16 ll sum[N],ans[N];
17 bool cmp(qu x,qu y){
18 return (x.x/K<y.x/K)||(x.x/K==y.x/K)&&(x.y<y.y);
19 }
20 ll c(int k){
21 return (k-1LL)*k/2;
22 }
23 void add(int x,int y){
24 edge[E].nex=head[x];
25 edge[E].to=y;
26 head[x]=E++;
27 }
28 void dfs1(int k,int f){
29 fa[k]=f;
30 sz[k]=1;
31 for(int i=head[k];i!=-1;i=edge[i].nex)
32 if (edge[i].to!=f){
33 dfs1(edge[i].to,k);
34 sz[k]+=sz[edge[i].to];
35 if (sz[mx[k]]<sz[edge[i].to])mx[k]=edge[i].to;
36 }
37 }
38 void dfs2(int k,int t){
39 id[k]=++x;
40 top[k]=t;
41 if (mx[k])dfs2(mx[k],t);
42 for(int i=head[k];i!=-1;i=edge[i].nex)
43 if ((edge[i].to!=fa[k])&&(edge[i].to!=mx[k]))dfs2(edge[i].to,edge[i].to);
44 }
45 void update(int &k,int l,int r,int x){
46 f[++V]=f[k]+1;
47 ls[V]=ls[k];
48 rs[V]=rs[k];
49 k=V;
50 if (l==r)return;
51 if (x<=mid)update(ls[k],l,mid,x);
52 else update(rs[k],mid+1,r,x);
53 }
54 int query(int k,int l,int r,int x,int y){
55 if ((!k)||(l>y)||(x>r))return 0;
56 if ((x<=l)&&(r<=y))return f[k];
57 return query(ls[k],l,mid,x,y)+query(rs[k],mid+1,r,x,y);
58 }
59 ll query(int k,int l,int r){
60 return c(query(ro[r],1,n,id[k],id[k]+sz[k]-1)-query(ro[l-1],1,n,id[k],id[k]+sz[k]-1));
61 }
62 void update(int k,int x){
63 while (k){
64 k=top[k];
65 if (k!=r){
66 f[k]+=x;
67 sum[fa[k]]+=c(f[k])-c(f[k]-x);
68 }
69 k=fa[k];
70 }
71 }
72 int main(){
73 scanf("%d%d%d",&n,&m,&r);
74 memset(head,-1,sizeof(head));
75 for(int i=1;i<n;i++){
76 scanf("%d%d",&x,&y);
77 add(x,y);
78 add(y,x);
79 }
80 x=0;
81 dfs1(r,0);
82 dfs2(r,r);
83 for(int i=1;i<=m;i++){
84 scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].z);
85 q[i].id=i;
86 }
87 sort(q+1,q+m+1,cmp);
88 for(int i=1;i<=n;i++){
89 ro[i]=ro[i-1];
90 update(ro[i],1,n,id[i]);
91 }
92 for(int i=1;i<=m;i++)
93 if (q[i].x<=q[i].y)ans[q[i].id]=query(q[i].z,q[i].x,q[i].y)-query(mx[q[i].z],q[i].x,q[i].y);
94 memset(f,0,sizeof(f));
95 x=1,y=0;
96 for(int i=1;i<=m;i++){
97 if (q[i].x>q[i].y)continue;
98 while (q[i].x<x)update(--x,1);
99 while (y<q[i].y)update(++y,1);
100 while (x<q[i].x)update(x++,-1);
101 while (q[i].y<y)update(y--,-1);
102 ans[q[i].id]-=sum[q[i].z];
103 }
104 for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
105 }

[nowcoder5671D]Data structure的更多相关文章

  1. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  2. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  3. [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计

    Design and implement a TwoSum class. It should support the following operations:add and find. add - ...

  4. Finger Trees: A Simple General-purpose Data Structure

    http://staff.city.ac.uk/~ross/papers/FingerTree.html Summary We present 2-3 finger trees, a function ...

  5. Mesh Data Structure in OpenCascade

    Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...

  6. ✡ leetcode 170. Two Sum III - Data structure design 设计two sum模式 --------- java

    Design and implement a TwoSum class. It should support the following operations: add and find. add - ...

  7. leetcode Add and Search Word - Data structure design

    我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...

  8. Java for LeetCode 211 Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...

  9. HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...

随机推荐

  1. 使用ffmpeg进行视频图片提取

    环境:windows10-x64 ffmpeg的功能很强大,可以进行音频和视频的处理,这里记录下需要从视频文件提取图片的情况. ffmpeg官方地址:https://www.ffmpeg.org/使用 ...

  2. java实现责任链模式的小demo

    //一个请假请求 public class LeaveRequest { private int leaveDays; private String name; public void leave() ...

  3. C++ 与 Visual Studio 2019 和 WSL(四)——库组件

    C++ 与 Visual Studio 2019 和 WSL (库组件) Reference 在 C++ 项目中使用库和组件 | Microsoft Docs 演练:创建和使用自己的动态链接库 (C+ ...

  4. Golang通脉之基础入门

    为什么要学 Go 性能优越感:Go 极其地快,其性能与 Java 或 C++相似.在使用中,Go 一般比 Python 要快 30 倍: 序列化/去序列化.排序和聚合中表现优异: 开发者效率较高:多种 ...

  5. [no code][scrum meeting] Beta 9

    $( "#cnblogs_post_body" ).catalog() 例会时间:5月23日15:30,主持者:肖思炀 下次例会时间:5月25日11:30,主持者:伦泽标 一.工作 ...

  6. (六)、Docker 之 Dockerfile

    1.什么是Dockerfile Dockerfile是用来构建Docker镜像的构建文件,是由一系列命令和参数构成的脚本. 2.Dockerfile解析过程 前提认知: 每条保留字指令都必须为大写字母 ...

  7. 搬运2:早期写的探究printf

    目录: 1. 关于printf格式化输出 2. printf的一般形式 3. 转换说明 4. 格式化输出的意义 5. 转换说明修饰符 6. 修饰符中的标记 7. printf的返回值 ps:共3250 ...

  8. 大牛针对零基础入门c语言详解指针(超详细)

    C语言指针说难不难但是说容易又是最容易出错的地方,因此不管是你要做什么只要用到C指针你就跳不过,今天咱们就以 十九个例子来给大家简单的分析一下指针的应用,最后会有C语言视频资料提供给大家更加深入的参考 ...

  9. Python:Ubuntu上使用pip安装opencv-python出现错误

    Ubuntu 18.04 上 使用 pip 安装 opencv-python,出现的错误如下: 1 ~$: pip install opencv-python -i https://pypi.tuna ...

  10. 字符串与模式匹配算法(四):BM算法

    一.BM算法介绍 BM算法(Boyer-Moore算法)是罗伯特·波义尔(Robert Boyer)和杰·摩尔(J·Moore)在1977年共同提出的.与KMP算法不同的是,BM算法是模式串P由左向右 ...