[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 ...
随机推荐
- react之组件生命周期
四个阶段 初始化 运行中 销毁 错误处理(16.3以后) 初始化 constructor static getDerivedStateFromProps() componentWillMount() ...
- Java基础之(十三):类与对象
初识面向对象 面向对象 & 面向过程 面向过程思想 步骤清晰简单,第一步做什么,第二步做什么..... 面向过程适合处理一些较为简单的问题 面向对象思想 物以类聚,分类的思维模式,思考问题 ...
- 2021MySQL 8.0.26安装教程,目前最新版(详细全面)
MySQL 8 要比 MySQL 5.7 快 2 倍,还带来了大量的改进和更快的性能!所以为什么不用MySQL8呢!下面是MySQL 8.0.26的安装教程. 安装网址: https://dev.my ...
- AT3950 [AGC022E] Median Replace
题目传送门 Description 有一个长度为 \(n\) 的 \(01\) 串,里面有一些还没有确定,我们标记为 ? .可以进行若干次操作,每次操作可以把三个相邻的数替换成它们的中位数.问有多少种 ...
- 初识Linux shell
目录 初识Linux shell Linux 深入探究Linux内核 系统内存管理 交换空间 页面 换出 软件程序管理 Linux中的进程 Linux系统的运行级 硬件设备管理 插入设备驱动代码的方法 ...
- Oracle中常用的to_char用法详解
Oracle函数to_char转化数字型指定小数点位数的用法 to_char,函数功能,就是将数值型或者日期型转化为字符型. 比如最简单的应用: -- 1.0123=>1.0123 SELECT ...
- 激活NX窗口的按钮
原理:取得按钮名称以后,通过运行宏文件激活按钮 Imports System.IO Imports System.Runtime.InteropServices Imports NXOpen.Menu ...
- javascript-jquery对象的事件处理
一.页面加载 1.页面加载顺序:先加载<head></head>之间的内容,然后加载<body></body>之间的内容 直接在head之间书写jque ...
- 【二食堂】Beta - Scrum Meeting 6
Scrum Meeting 6 例会时间:5.19 18:30~18:50 进度情况 组员 当前进度 今日任务 李健 1. 实体标注的优化基本已经实现,后端有bug,还没有进行接口调用 issue 2 ...
- 使用json-path解析json
在我们的日常开发中,有时候需要从一个json字符串中获取一个值,或者从一段json字符串中获取到某些值,如果先使用Gson或Jackson转换成java对象在获取值,有些时候是很麻烦的,那么有没有一种 ...