[nowcoder5671D]Data structure


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的更多相关文章
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- [LeetCode] Two Sum III - Data structure design 两数之和之三 - 数据结构设计
Design and implement a TwoSum class. It should support the following operations:add and find. add - ...
- 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 ...
- Mesh Data Structure in OpenCascade
Mesh Data Structure in OpenCascade eryar@163.com 摘要Abstract:本文对网格数据结构作简要介绍,并结合使用OpenCascade中的数据结构,将网 ...
- ✡ 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 - ...
- leetcode Add and Search Word - Data structure design
我要在这里装个逼啦 class WordDictionary(object): def __init__(self): """ initialize your data ...
- 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 ...
- HDU5739 Fantasia(点双连通分量 + Block Forest Data Structure)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5739 Description Professor Zhang has an undirect ...
随机推荐
- Spring Bean装配笔记
Spring Bean装配笔记 Spring中的Bean是一个很重要的概念.Spring作为一个Bean容器,它可以管理对象和对象之间的依赖关系,我们不需要自己建立对象,把这部分工作全部转交给容器完成 ...
- 重修 Tarjan
Tarjan是谁 Tarjan's SCCs(有向图强连通分量)algorithm 给定⼀个有向图 \(G\),若存在 \(rt\in V\),满⾜从 \(rt\) 出发能到达 \(V\) 中的所有的 ...
- linux启动redis命令
首先进入到/usr/local/bin目录下(因为你redis安装的目录绝大多数都在这里) root@xxxx:/usr/local/bin#:redis-server wangconfig/redi ...
- FastAPI 学习之路(三十八)Static Files
如果使用前后台不分离的开发方式,那么模板文件中使用的静态文件,比如css/js等文件的目录需要在后台进行配置,以便模板渲染是能正确读到这些静态文件.那么我们应该如何处理呢. 首先安装依赖 pip in ...
- SharkCTF2021 bbpop题记
一道挺好的web. 做完这一题,感觉php序列化(甚至魔术方法)之类的有点开始玩明白了. 题面很长: 预备知识: PHP类的方法中,有一部分以下划线开头的"魔术方法".不同于普通方 ...
- [技术博客]OKhttp3使用get,post,delete,patch四种请求
OKhttp3使用get,post,delete,patch四种请求 1.okhttp简介 okhttp封装了大量http操作,大大简化了安卓网络请求操作,是现在最火的安卓端轻量级网络框架.如今okh ...
- 升级MySQL8.0的历险记
最近忙于Fighting的项目,所以笔耕有些松懈,实为不该. 刚好遇到需要从MySQL5.7.33升级到MySQL8.0.x的需求,于是记录一下整个升级过程,踩坑而过. 背景梗概:本地docker容器 ...
- Asp.Net 熟悉 Spring
注:(为加强记忆,所以记录下来,对于有些地方为什么那样写,我也不太理解) 一.我们先创建个窗体应用程序Demos,事先熟悉它是这么实现的 第一步,先在项目的根目录下建一个library文件夹,目的是放 ...
- 从零开始的DIY智能家居 - 基于 ESP32 的智能浇水器
前言 上次 土壤湿度传感器 完成之后,就立下一个 flag 要搭建一个智慧浇水的智能场景,现在终于有时间填坑了!(o゚▽゚)o 智慧浇水场景的核心设备有三个: 检测土壤状态的:土壤湿度传感器 通过这个 ...
- Django(73)django-debug-toolbar调试工具
介绍 Django框架的调试工具栏使用django-debug-toolbar库,是一组可配置的面板,显示有关当前请求/响应的各种调试信息,点击时,显示有关面板内容的更多详细信息. 应用 1. 安装 ...