问题相当于统计$且\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. ShardingSphere 知识库更新 | 官方样例集助你快速上手

    Apache ShardingSphere 作为 Apache 顶级项目,是数据库领域最受欢迎的开源项目之一.经过 5 年多的发展,ShardingSphere 已获得超 14K Stars 的关注, ...

  2. BIBD&SBIBD的矩阵题

    证明不存在 \(01\) 方阵 \(A\) 使得: \(A^TA=\begin{pmatrix}7&2&\dots &2\\2&7&\dots&2\\ ...

  3. 一个简单的单例模式Demo

    /** * @author :nx014924 * @date :Created in 5/30/2021 1:09 PM * @description: * @modified By: * @ver ...

  4. 【高热FAQ】关于智慧康养物联网加速器 ,你想知道的都在这

    摘要:从软硬件解决方案.设备接入到资源扶持,一文梳理智慧康养物联网加速器中ISV最关心的问题. 本文分享自华为云社区<[高热FAQ]关于智慧康养物联网加速器 ,你想知道的都在这>,作者:技 ...

  5. 浅尝装饰器和AOP

    [写在前面] 参考文章:https://www.cnblogs.com/huxi/archive/2011/03/01/1967600.html[从简单的例子入手进行讲解,由浅入深,很到位] 装饰器部 ...

  6. Java中的函数式编程(四)方法引用method reference

    写在前面 我们已经知道,lambda表达式是一个匿名函数,可以用lambda表达式来实现一个函数式接口.   很自然的,我们会想到类的方法也是函数,本质上和lambda表达式是一样的,那是否也可以用类 ...

  7. Scrum Meeting 5.26

    Scrum Meeting Beta 1 日期:2021年5月23日&2021年5月24日 会议主要内容概述:汇报工作 一.进度情况 组员 负责 两日内已完成的工作 后两日计划完成的工作 工作 ...

  8. [技术博客]OKhttp3使用get,post,delete,patch四种请求

    OKhttp3使用get,post,delete,patch四种请求 1.okhttp简介 okhttp封装了大量http操作,大大简化了安卓网络请求操作,是现在最火的安卓端轻量级网络框架.如今okh ...

  9. seata整合nacos完成分布式的部署

    seata整合nacos完成分布式的部署 一.背景 二.部署机器 三.部署步骤 1.在seata上创建命名空间 2.下载对应版本的seata 3.单机启动 1.修改seata配置文件 1.修改注册中心 ...

  10. [CPP] 类的内存布局

    本文可以解决下面 3 个问题: 以不同方式继承之后,类的成员变量是如何分布的? 虚函数表及虚函数表指针,在可执行文件中的位置? 单一继承.多继承.虚拟继承之后,类的虚函数表的内容是如何变化的? 在这里 ...