20182019-acmicpc-asia-dhaka-regional F .Path Intersection 树链剖分
直接进行树链剖分,每次对路径区间内的所有点值+1,线段树进行维护,然后查询线段树的最大值的个数!!!
查询线段树区间最大值个数,可以先维护区间和,在维护区间最值,如果区间和等于区间最值乘以区间长度,那么直接返回长度!!!
清空的时候,直接减去,不要直接重新建树
#include<bits/stdc++.h>
#define LL long long
#define lson rt<<1
#define rson rt<<1|1
using namespace std;
const int MAXN = 2e5+;
struct node{
int l,r;
int sum,laze;
int maxx;
}tree[MAXN<<];
struct edge{
int Next,to;
}e[MAXN<<];
int head[MAXN];
int siz[MAXN];
int top[MAXN];
int son[MAXN];
int d[MAXN];
int fa[MAXN];
int id[MAXN];
int sz[MAXN];
int uu[],vv[];
int cnt,tot;
void add(int x,int y){
e[++tot].Next=head[x];
e[tot].to=y;
head[x]=tot;
}
void dfs1(int u,int f,int depth){
d[u]=depth;
fa[u]=f;
siz[u]=;
for (int i=head[u];i;i=e[i].Next){
int v=e[i].to;
if (v==f)
continue;
dfs1(v,u,depth+);
siz[u]+=siz[v];
if (siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int t){
top[u]=t;
id[u]=++cnt;
if (!son[u])
return;
dfs2(son[u],t);
for (int i=head[u];i;i=e[i].Next)
{
int v=e[i].to;
if (v!=son[u] && v!=fa[u])
dfs2(v,v);
}
}
void push_down(int rt){
if (tree[rt].laze){
tree[lson].sum+=(tree[lson].r-tree[lson].l+)*tree[rt].laze;
tree[rson].sum+=(tree[rson].r-tree[rson].l+)*tree[rt].laze;
tree[lson].maxx+=tree[rt].laze;
tree[rson].maxx+=tree[rt].laze;
tree[lson].laze+=tree[rt].laze;
tree[rson].laze+=tree[rt].laze;
tree[rt].laze=;
}
}
void buildtree(int rt,int l,int r){
tree[rt].l=l;
tree[rt].r=r;
tree[rt].sum=;
tree[rt].laze=;
tree[rt].maxx=;
int mid=(l+r)>>;
if (l==r){
return;
}
buildtree(lson,l,mid);
buildtree(rson,mid+,r);
}
void update(int rt,int ul,int ur,int w){
if (ul>ur)return;
int l=tree[rt].l;
int r=tree[rt].r;
if (ul<=l && r<=ur){
tree[rt].laze+=w;
tree[rt].maxx+=w;
tree[rt].sum+=(r-l+)*w;
return;
}
push_down(rt);
int mid=(l+r)>>;
if (ur<=mid){
update(lson,ul,ur,w);
}else if (ul>mid){
update(rson,ul,ur,w);
}else {
update(lson,ul,ur,w);
update(rson,ul,ur,w);
}
tree[rt].maxx=max(tree[lson].maxx,tree[rson].maxx);
tree[rt].sum=tree[lson].sum+tree[rson].sum;
}
void qRange(int x,int y,int k){
while(top[x]!=top[y]){
if (d[top[x]]<d[top[y]])swap(x,y);
update(,id[top[x]],id[x],k);
x=fa[top[x]];
}
if (d[x]>d[y])swap(x,y);
update(,id[x],id[y],k);
}
int query(int rt,int ql,int qr,int w){
int l=tree[rt].l;
int r=tree[rt].r;
if (tree[rt].sum==w*(r-l+)){
return r-l+;
}
if (l==r){
return ;
}
push_down(rt);
int mid=(l+r)>>;
int ans=;
if (tree[lson].maxx==w){
ans+=query(lson,ql,qr,w);
}
if (tree[rson].maxx==w){
ans+=query(rson,ql,qr,w);
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
int n;
int ca=;
while(t--){
memset(head,,sizeof(head));
memset(id,,sizeof(id));
memset(top,,sizeof(top));
memset(siz,,sizeof(siz));
memset(fa,,sizeof(fa));
memset(son,,sizeof(son));
memset(d,,sizeof(d));
scanf("%d",&n);
cnt=;
tot=;
int u,v;
for (int i=;i<n;i++){
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);
}
int r,op,k;
dfs1(,,);
dfs2(,);
scanf("%d",&op);
printf("Case %d:\n",++ca);
buildtree(,,n);
for (int i=;i<=op;i++){
scanf("%d",&k);
for (int i=;i<=k;i++){
scanf("%d%d",&uu[i],&vv[i]);
qRange(uu[i],vv[i],);
}
int ans=query(,,n,k);
for (int i=;i<=k;i++){
qRange(uu[i],vv[i],-);
}
printf("%d\n",ans);
}
}
return ;
}
20182019-acmicpc-asia-dhaka-regional F .Path Intersection 树链剖分的更多相关文章
- 2018-2019 ACM-ICPC, Asia Dhaka Regional Contest
目录 Contest Info Solutions B. Counting Inversion C. Divisors of the Divisors of An Integer E. Helping ...
- 2018-2019 ACM-ICPC, Asia Dhaka Regional Contest C.Divisors of the Divisors of An Integer (数论)
题意:求\(n!\)的每个因子的因子数. 题解:我们可以对\(n!\)进行质因数分解,这里可以直接用推论快速求出:https://5ab-juruo.blog.luogu.org/solution-p ...
- Gym - 102040F Path Intersection (树链剖分+线段树)
题意:给出棵树上的k条路径,求这些路径的公共点数量. 将每条路径上的点都打上标记,被标记过k次的点就是公共点了.由于公共点形成的区间是连续的,因此直接在线段树上暴搜即可在$O(logn)$求出一条链上 ...
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心
2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest- H. Rikka with A Long Colour Palette -思维+贪心 [Proble ...
- 2018-2019 ACM-ICPC, Asia Nanjing Regional Contest
https://codeforces.com/gym/101981 Problem A. Adrien and Austin 贪心,注意细节 f[x]=1:先手必赢. f[x]: 分成两部分(或一部分 ...
- Gym 102028C - Supreme Command - [思维题][2018-2019 ACM-ICPC Asia Jiaozuo Regional Contest Problem C]
题目链接:https://codeforces.com/gym/102028/problem/C Lewis likes playing chess. Now he has n rooks on th ...
- Gym 101981I - Magic Potion - [最大流][2018-2019 ACM-ICPC Asia Nanjing Regional Contest Problem I]
题目链接:http://codeforces.com/gym/101981/attachments There are n heroes and m monsters living in an isl ...
- 2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest Solution
A. Rikka with Minimum Spanning Trees 题意: 给出一个图,求最小生成树的个数和权值 思路: 因为数据随机,只有一个MST #include <bits/std ...
- 2018-2019 ACM-ICPC, Asia Jiaozuo Regional Contest
目录 Contest Info Solutions A. Xu Xiake in Henan Province D. Keiichi Tsuchiya the Drift King E. Resist ...
随机推荐
- javax.el.PropertyNotFoundException: Property 'studentAge' not found on type com.hs.model.StudentModel
mi明明已经把这个字段重构了,为啥还这样提示呢?整个工程全部 查找也找不到这个字段了啊
- 解决安装编译工具gcc后无法连接mysql
在安装编译工具gcc后: yum -y install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2 ...
- js面向对象开发基础
js的面向对象开发能力较弱,基本是以prototype为核心的面向对象,虽然现在出了个class这玩意,但本文还是先不做探讨. 面向对象基础——构造函数方法 var Fly = function (s ...
- 【笔记】http1.1支持的7种请求方法
本文是本人复习http协议整理笔记,以备后续查阅. http1.1支持的7种请求方法:get.post.head.options.put.delete.trace 在internet应用中,最常用的请 ...
- 【linux配置】虚拟机配置静态IP地址
使用VMware配置虚拟机静态IP地址 一.安装好虚拟后在菜单栏选择编辑→ 虚拟网络编辑器,打开虚拟网络编辑器对话框,选择Vmnet8 Net网络连接方式,随意设置子网IP,点击NAT设置页面,查看子 ...
- 斯坦福CS课程列表
http://exploredegrees.stanford.edu/coursedescriptions/cs/ CS 101. Introduction to Computing Principl ...
- vue中element-ui添加按钮
<div v-for="(v,i) in list"> <el-form label-width="120px" size="sma ...
- python中的输入和输出
输入和输出 输出: 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字.比如输出'hello, world',用代码实现如下: >>> print('hello, ...
- C#字符串与 byte数据的互相转换
string和byte[]的转换 (C#) string类型转成byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( s ...
- [已转移]js事件流之事件冒泡的应用----事件委托
该文章已转移到博客:https://cynthia0329.github.io/ 什么是事件委托? 它还有一个名字叫事件代理. JavaScript高级程序设计上讲: 事件委托就是利用事件冒泡,只指定 ...