当前位置:网站首页>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;
}
边栏推荐
- MySQL中的存储过程(详细篇)
- C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
- 将 APACHE 日志解析到 SQL 数据库中
- 知识蒸馏2:目标检测中的知识蒸馏
- 论文阅读之《Underwater scene prior inspired deep underwater image and video Enhancement (UWCNN)》
- 数据库系统原理与应用教程(064)—— MySQL 练习题:操作题 51-61(八):查询条件的构造、通配符
- Arranger software FL Studio Chinese version installation tutorial and switching language tutorial
- Graph Attention Mechanism
- 基于模糊PID的液压舵机伺服系统
- 关于内和调试无法查看ntdll内存的问题
猜你喜欢

JMeter笔记4 | JMeter界面介绍

PLSQL Developer安装和配置

Servo System of Hydraulic Steering Gear Based on Fuzzy PID

esp32系列(5):esp32 蓝牙架构学习
![有效的括号字符串[贪心练习]](/img/1c/5cefb53bc4aba54dd79b0cc9b09b0d.png)
有效的括号字符串[贪心练习]

【AAAI2020】阿里DMR:融合Matching思想的深度排序模型

JVM诊断命令jcmd介绍

想要写出好的测试用例,先要学会测试设计

论文阅读之《DeepIlluminance: Contextual IlluminanceEstimation via Deep Neural Networks》

高级语言垃圾回收思路和如何减少性能影响原理分析
随机推荐
Valid bracketed strings [greedy exercise]
图卷积神经网络的数学原理——谱图理论和傅里叶变换初探
分账系统二清解决方案如何助力电商平台合规经营?
Arranger software FL Studio Chinese version installation tutorial and switching language tutorial
Dive deep on Netflix‘s recommender system(Netflix推荐系统是如何实现的?)
MySQL中的存储过程(详细篇)
C陷阱与缺陷 第6章 预处理器 6.3 宏并不是语句
Excel导入和导出
全球架构师峰会
(17)[系统调用]追踪系统调用(0环)
Error EPERM operation not permitted, mkdir 'Dsoftwarenodejsnode_cache_cacach Two solutions
mysql刷脏的几种场景以及相关参数
Win11如何把d盘空间分给c盘?Win11d盘分盘出来给c盘的方法
想要写出好的测试用例,先要学会测试设计
esp32系列(5):esp32 蓝牙架构学习
[OC学习笔记]属性关键字
知识蒸馏2:目标检测中的知识蒸馏
Promise entry to proficient (1.5w word detailed explanation)
习题:变量、常量和基本数据类型
【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌
