[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 ...
随机推荐
- 洛谷luogu3957跳房子(单调队列优化)
QwQ被普及组的题折磨的死去活来. 硬是卡线段树,没卡过QwQ oi生涯,第一道正经的单调队列dp题 进入正题 题目大意: 其中\(n \le 500000\) 看到这个题的第一感觉就是二分金币数 很 ...
- 霍尔效应实验 execl数据处理
execl 函数 =POWER() /*幂次*/ =ROUND() /*保留小数点后几位*/ =SQRT() /*开平方根*/ =POWER( x, 1/3 ) /*开立方根*/ =COUNTA() ...
- python中单引号、双引号和三引号
在python中字符串可以用双引号表示,也可以用单引号表示: str1 = 'hello world'str2 = "hello world" 这两种字符串的表示方法没有区别. p ...
- ubuntu20.04 使用root用户登录
1.设置root用户密码 执行 sudo passwd root 然后输入设置的密码,输入两次,这样就完成了设置root用户密码了 2.修改配置文件 执行 sudo vim /usr/share/li ...
- Beta阶段性总结
1.题士开发总结 2.反思 2.1 Issue管理 从0522敲定各个功能的API后,团队成员及时沟通,积极开发,但由于开发过程没能有效体现在issue上(如未能及时在issue上形成记录,功能开发完 ...
- UltraSoft - Beta - Scrum Meeting 3
20200519会议纪要 Date: May 19th, 2020. Scrum 情况汇报 进度情况 组员 负责 今日进度 q2l PM.后端 暂无 Liuzh 前端 暂无 Kkkk 前端 完成了前端 ...
- wpa_supplicant启动出错rfkill: Cannot open RFKILL control device
在板子是调试网络,千辛万苦把wpa_supplicant及其依赖都移植编译进来了,在板子上调试启动的时候启动报错了 D/wpa_supplicant( 1152): wpa_supplicant v2 ...
- Spoj 2878 KNIGHTS - Knights of the Round Table | 双联通分量 二分图判定
题目链接 考虑建立原图的补图,即如果两个骑士不互相憎恨,就在他们之间连一条无向边. 显而易见的是,如果若干个骑士在同一个点数为奇数的环上时,他们就可以在一起开会.换句话说,如果一个骑士被一个奇环包含, ...
- 2万字|30张图带你领略glibc内存管理精髓(因为OOM导致了上千万损失)
前言 大家好,我是雨乐. 5年前,在上家公司的时候,因为进程OOM造成了上千万的损失,当时用了一个月的时间来分析glibc源码,最终将问题彻底解决. 最近在逛知乎的时候,发现不少人有对malloc/f ...
- 羽夏看Win系统内核——保护模式篇
写在前面 此系列是本人一个字一个字码出来的,包括示例和实验截图.由于系统内核的复杂性,故可能有错误或者不全面的地方,如有错误,欢迎批评指正,本教程将会长期更新. 如有好的建议,欢迎反馈.码字不易, ...