当前位置:网站首页>2022年杭电多校第2场 1001 Static Query on Tree(树链剖分+哈希表差分
2022年杭电多校第2场 1001 Static Query on Tree(树链剖分+哈希表差分
2022-07-30 17:32:00 【NONE-C】
题目大意:
给定一棵根为1的树,给定点集A,B,C,能被A任意一个到达,B任意一个到达,且能达到C的任意一个的点的数量。
这是第二场多校,写完签到题,就开了1001。其实看懂题目之后,思路还是挺快就出现了,当时就给队友口胡了一种算法,如果能把C的子树染色,在把A,B每一个点到1的路径上的全部打上个标记,最后统计一下答案救过了。但是不知道该怎么操作,赛后看题解,树链剖分!
果然还是吃了没文化(数据结构)的亏,花了两天在恶补树剖,终于把这题给A了。
本来打算写线段树的,但是想到就只有三种颜色,用点小技巧就可以过了,少写线段树,代码立马缩减90行
#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;
}
边栏推荐
- 优酷视频元素内容召回系统:多级多模态引擎探索
- 线程同步 控制执行顺序
- 论文阅读之《Color Constancy Using CNNs》
- [MRCTF2020]Ezaudit
- Error occurred while trying to proxy request项目突然起不来了
- Redis缓存穿透-热点缓存并发重建-缓存与数据库双写不一致-缓存雪崩
- 论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
- Tensorflow模型量化(Quantization)原理及其实现方法
- 强烈推荐APP破解常用工具集合!
- fast shell porting
猜你喜欢
随机推荐
(17)[系统调用]追踪系统调用(0环)
DLCM - 基于列表上下文信息的重排序模型
公司部门来了个00后测试卷王之王,老油条表示真干不过,已经...
FP6600QSO SOP-8 USB专用充电端口控制器 用于快充电协议和QC2.0/3.0
游戏化产品搭建思路的拆解与探究
链表Oj练习题 纯C语言
SQLServer下载与安装
Web 3.0入门教程
Mathematical Principles of Graph Convolutional Neural Networks——A Preliminary Study on Spectral Graph Theory and Fourier Transform
【Cloud Store Announcement】Notice of Help Center Update on July 30
高级语言垃圾回收思路和如何减少性能影响原理分析
Daily practice------Generate 13-digit bar, Ean-13 code rule: The thirteenth digit is the check code obtained by the calculation of the first twelve digits.
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
Tensorflow中实现正则化
shell快速移植
JMeter笔记3 | JMeter安装和环境说明
C陷阱与缺陷 第7章 可移植性缺陷 7.3 整数的大小
Valid bracketed strings [greedy exercise]
un7.30:linux——如何在docker容器中安装MySQL?
JMeter Notes 4 | JMeter Interface Introduction