当前位置:网站首页>Hangzhou electric school game 2 1001 2022 Static Query on Tree (Tree + hash table difference chain subdivision
Hangzhou electric school game 2 1001 2022 Static Query on Tree (Tree + hash table difference chain subdivision
2022-07-30 17:47:00 【NONE-C】
题目大意:
给定一棵根为1的树,给定点集A,B,C,能被AEither one arrives,BEither one arrives,且能达到Cthe number of points for any one of .
This is the second multi-school,Complete the sign-in question,就开了1001.In fact, after understanding the subject,The idea came quickly,At that time, I gave my teammates an algorithm,如果能把Csubtree coloring,在把A,BEvery point arrives1All on the path are marked with a mark,The last statistic saved the answer.但是不知道该怎么操作,赛后看题解,树链剖分!
Sure enough, there is still no culture to eat(数据结构)的亏,Spent two days in oxbushu dissection,Finally got this questionA了.
I was going to write a line segment tree,But there are only three colors that come to mind,Just a little trick,Write less segment trees,The code shrinks immediately90行
#include<bits\stdc++.h>
using namespace std;
const int maxn=2e5+5;
#define ll long long
int n,m,na,nb,nc;
int sz[maxn],son[maxn],fa[maxn],top[maxn],cnt=0,nid[maxn],dep[maxn];
int a[maxn],b[maxn],c[maxn];
vector<int>e[maxn];
ll tr[maxn];
map<int,ll>add;
void dfs1(int s,int father)
{
fa[s]=father;
sz[s]=1,dep[s]=dep[father]+1;
for(auto to:e[s])
{
if(to==father)continue;
dfs1(to,s);
sz[s]+=sz[to];
if(sz[son[s]]<sz[to])son[s]=to;
}
}
void dfs2(int s,int t)
{
nid[s]=++cnt;top[s]=t;
if(!son[s])return ;
dfs2(son[s],t);
for(auto to:e[s])
{
if(to==fa[s]||to==son[s])continue;
dfs2(to,to);
}
}
void add_path(int u,int v,ll y)
{
while(top[u]!=top[v])
{
if(dep[top[u]]<dep[top[v]])swap(u,v);
add[nid[top[u]]]+=y;add[nid[u]+1]-=y;
u=fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
add[nid[u]]+=y;add[nid[v]+1]-=y;
}
void add_tree(int u,ll y)
{
add[nid[u]]+=y;
add[nid[u]+sz[u]-1+1]-=y;
}
const ll p1=1;
const ll p2=1e6;
const ll p3=1e12;
void solve()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;i++)nid[i]=0,e[i].clear(),top[i]=0,fa[i]=0,son[i]=0,sz[i]=0,tr[i]=0;
for(int i=2;i<=n;i++)
{
int u;
scanf("%d",&u);
e[u].push_back(i);
e[i].push_back(u);
}
dfs1(1,0);
dfs2(1,1);
while(m--){
scanf("%d%d%d",&na,&nb,&nc);
for(int i=1;i<=na;i++)scanf("%d",&a[i]);
for(int i=1;i<=nb;i++)scanf("%d",&b[i]);
for(int i=1;i<=nc;i++)scanf("%d",&c[i]);
for(int i=1;i<=na;i++)add_path(1,a[i],(ll)p1);
for(int i=1;i<=nb;i++)add_path(1,b[i],(ll)p2);
for(int i=1;i<=nc;i++)add_tree(c[i],(ll)p3);
ll ans=0;int cnt=0,las=-1;
for(auto it:add){
ans+=it.second;
if(ans%p2>0&&(ans%p3)/p2>0&&ans/p3>0){
if(las==-1)las=it.first;}
else if(las!=-1)cnt+=(it.first-las),las=-1;
}
printf("%d\n",cnt);
add.clear();
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
边栏推荐
- 什么是无损检测设备?
- Promise入门到精通(1.5w字详解)
- 2022年杭电多校第2场 1001 Static Query on Tree(树链剖分+哈希表差分
- 592. Fraction Addition and Subtraction
- fast shell porting
- Google earth engine如何实现我们时间列表的排列和选取
- Shell implementation based on stm32
- 数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
- Insert data into MySQL in C language
- 今年这情况。。真心推荐专科的工程师升个本!
猜你喜欢

测试行业干了5年,从只会点点点到了现在的测试开发,总算是证明了自己

FastJson反序列化漏洞(复现)

JMeter Notes 3 | JMeter Installation and Environment Instructions

宽带射频放大器OA4SMM4(1)

公司部门来了个00后测试卷王之王,老油条表示真干不过,已经...

图卷积神经网络的数学原理——谱图理论和傅里叶变换初探

测试.net文字转语音模块System.Speech

Analysis and Simulation of Short Circuit Fault in Power System Based on MATLAB

华为无线设备配置Mesh业务
![[HarekazeCTF2019] Avatar Uploader 1](/img/2c/6dde7b8d34ba0deb334b4283e1e30e.png)
[HarekazeCTF2019] Avatar Uploader 1
随机推荐
[OC学习笔记]属性关键字
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
图卷积神经网络的数学原理——谱图理论和傅里叶变换初探
JMeter笔记3 | JMeter安装和环境说明
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
【解决】关于 Unity Hub 获取许可证失败 或 无响应导致无法开发的问题
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
18.支持向量机(SVM)的介绍
知识蒸馏2:目标检测中的知识蒸馏
中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
宝塔搭建PHP自适应懒人网址导航源码实测
论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
C陷阱与缺陷 第6章 预处理器 6.2 宏并不是函数
记者卧底
JMeter Notes 3 | JMeter Installation and Environment Instructions
Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
习题:变量、常量和基本数据类型
链表Oj练习题 纯C语言
超声波探伤仪是做什么用的?
Summary of String Copy, Concatenation, Comparison and Split Functions (1)
