当前位置:网站首页>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;
}
边栏推荐
- Arranger software FL Studio Chinese version installation tutorial and switching language tutorial
- C陷阱与缺陷 第6章 预处理器 6.2 宏并不是函数
- 查询表中开始日期与结束日期
- C陷阱与缺陷 第7章 可移植性缺陷 7.5 移位运算符
- 优酷视频元素内容召回系统:多级多模态引擎探索
- 17.机器学习系统的设计
- Excel导入和导出
- How Google earth engine realizes the arrangement and selection of our time list
- JMeter Notes 3 | JMeter Installation and Environment Instructions
- KDD 2020 | 深入浅出优势特征蒸馏在淘宝推荐中的应用
猜你喜欢
随机推荐
Arranger software FL Studio Chinese version installation tutorial and switching language tutorial
Promise入门到精通(1.5w字详解)
阿里巴巴CAN:Embedding前置的特征交互新思路
基于MATLAB的电力系统短路故障分析与仿真
【网络工程】A、B、C、D、E类IP地址划分依据和特殊的IP地址
华为无线设备配置Mesh业务
中文字符集编码Unicode ,gb2312 , cp936 ,GBK,GB18030
一个 15 年 SAP ABAP 开发人员分享的 SAPGUI 一些个性化设置和实用小技巧试读版
有效的括号字符串[贪心练习]
Ecplise执行C语言报错:cannot open output file xxx.exe: Permission denied
Promise entry to proficient (1.5w word detailed explanation)
ERROR 2003 (HY000) Can't connect to MySQL server on 'localhost3306' (10061)Solution
bert-base调试心得
什么是工业射线照相设备?
从零开始的Multi-armed Bandit
C陷阱与缺陷 第6章 预处理器 6.4 宏并不是类型定义
升级Win11后不喜欢怎么退回Win10系统?
Mongoose module
数据库系统原理与应用教程(065)—— MySQL 练习题:操作题 62-70(九):分组查询与子查询
bean的生命周期










