【BZOJ4285】使者 cdq分治+扫描线+树状数组
【BZOJ4285】使者
Description
Input
Output
Sample Input
1 2
1 3
1 4
2 5
5 9
5 10
5 11
10 13
3 6
4 7
4 8
7 12
6
2 4
10 12
9 8
6 7
3 11
7 10
5
1 1 5
3 5 4
2 7 10
2 10 12
3 5 4
Sample Output
1
HINT
题解:又是精神污染。
我们将每个跳跃点看成在DFS序中二维平面上的一个点,将询问看成若干个矩形,那么就是问你矩形中点的个数,用树状数组+扫描线即可。但是有加点删点怎么办呢?套上cdq分治即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=100010;
int n,m,Q,cnt,np,nq,now;
int to[maxn<<1],next[maxn<<1],head[maxn],fa[20][maxn],Log[maxn],dep[maxn],siz[maxn],p1[maxn],p2[maxn];
int s[maxn],tim[maxn],op[maxn],ans[maxn];
struct point
{
int x,y,k,org;
point() {}
point(int a,int b,int c,int d) {x=a,y=b,k=c,org=d;}
}p[maxn];
struct QUERY
{
int l,r,x,k,org;
QUERY() {}
QUERY(int a,int b,int c,int d,int e) {l=a,r=b,x=c,k=d,org=e;}
}q[maxn<<1];
inline void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
void dfs(int x)
{
siz[x]=1,p1[x]=++p2[0];
for(int i=head[x];i!=-1;i=next[i]) if(to[i]!=fa[0][x])
fa[0][to[i]]=x,dep[to[i]]=dep[x]+1,dfs(to[i]),siz[x]+=siz[to[i]];
p2[x]=p2[0];
}
inline int FA(int x,int y)
{
for(int i=Log[y];i>=0;i--) if(y>=(1<<i)) y-=(1<<i),x=fa[i][x];
return x;
}
bool cmpx(const point &a,const point &b)
{
return a.x<b.x;
}
bool cmqx(const QUERY &a,const QUERY &b)
{
return a.x<b.x;
}
inline void updata(int x,int v)
{
for(int i=x;i<=n;i+=i&-i)
{
if(tim[i]<now) tim[i]=now,s[i]=0;
s[i]+=v;
}
}
inline int query(int x)
{
int ret=0,i;
for(i=x;i;i-=i&-i)
{
if(tim[i]<now) tim[i]=now,s[i]=0;
ret+=s[i];
}
return ret;
}
void solve(int l,int r,int L,int R)
{
if(l==r)
{
for(int i=L;i<=R;i++) if(p[l].org<q[i].org&&p[l].x<=q[i].x&&p[l].y>=q[i].l&&p[l].y<=q[i].r)
ans[q[i].org]+=p[l].k*q[i].k;
return ;
}
if(L>R) return ;
int mid=(l+r)>>1,MID,i,j;
for(i=L;i<=R;i++) if(q[i].org>p[mid].org) break;
MID=i-1;
solve(l,mid,L,MID),solve(mid+1,r,MID+1,R);
sort(p+l,p+mid+1,cmpx),sort(q+MID+1,q+R+1,cmqx);
now++;
for(i=MID+1,j=l;i<=R;i++)
{
for(;j<=mid&&p[j].x<=q[i].x;j++) updata(p[j].y,p[j].k);
ans[q[i].org]+=(query(q[i].r)-query(q[i].l-1))*q[i].k;
}
}
inline int rd()
{
int ret=0,f=1; char gc=getchar();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();}
while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar();
return ret*f;
}
int main()
{
n=rd();
int i,j,a,b,c;
memset(head,-1,sizeof(head));
for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
dfs(1);
for(i=2;i<=n;i++) Log[i]=Log[i>>1]+1;
for(j=1;(1<<j)<=n;j++) for(i=1;i<=n;i++) fa[j][i]=fa[j-1][fa[j-1][i]];
m=rd();
for(i=1;i<=m;i++)
{
a=rd(),b=rd();
if(p1[a]>p1[b]) swap(a,b);
p[++np]=point(p1[a],p1[b],1,0);
}
Q=rd();
for(i=1;i<=Q;i++)
{
op[i]=rd(),a=rd(),b=rd();
if(p1[a]>p1[b]) swap(a,b);
if(op[i]==1) p[++np]=point(p1[a],p1[b],1,i);
if(op[i]==2) p[++np]=point(p1[a],p1[b],-1,i);
if(op[i]==3)
{
if(p1[b]<=p2[a])
{
c=FA(b,dep[b]-dep[a]-1);
q[++nq]=QUERY(p1[b],p2[b],p1[c]-1,1,i);
q[++nq]=QUERY(p2[c]+1,n,p1[b]-1,-1,i),q[++nq]=QUERY(p2[c]+1,n,p2[b],1,i);
}
else q[++nq]=QUERY(p1[b],p2[b],p1[a]-1,-1,i),q[++nq]=QUERY(p1[b],p2[b],p2[a],1,i);
}
}
solve(1,np,1,nq);
for(i=1;i<=Q;i++) if(op[i]==3) printf("%d\n",ans[i]);
return 0;
}
【BZOJ4285】使者 cdq分治+扫描线+树状数组的更多相关文章
- [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)
[APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...
- bzoj 1176 cdq分治套树状数组
题面: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2716 怎么KD树跑得都那么快啊..我写的CDQ分治被暴虐 做四遍CDQ分治,每次求一个 ...
- UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)
传送门 CJOJ Solution 考虑这是一个四维偏序对吧. 直接cdq套在一起,然后这题有两种实现方法(树状数组的更快!) 代码实现1(cdq+cdq+cdq) /* mail: mleautom ...
- bzoj2253纸箱堆叠(动态规划+cdq分治套树状数组)
Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , 之后,即可自动化生产三边边长为 (a mod P,a^2 mod p,a^3 mod P) (a^4 ...
- 【模板】cdq分治代替树状数组(单点修改,区间查询)
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...
随机推荐
- 轻量级ORM框架Dapper应用三:使用Dapper实现In操作
IN 操作符允许我们在 WHERE 子句中规定多个值. 本篇文章中,还是使用和上篇文章中同样的实体类和数据库,Dapper使用in操作符的代码如下: using System; using Syste ...
- 关于winform窗体关闭时弹出提示框,选择否时窗体也关闭的问题
在窗体中有FormClosing这个事件,这个事件是在窗体关闭时候运行的.如果要取消某个事件的操作,那么就在该事件中写上e.Cancel=true就能取消该事件,也就是不执行该事件.所以,你要在窗体关 ...
- Java-jdbc连接简化类jdbcUtil
在src文件夹下创建配置文件 db.properties db.properties drivername=com.mysql.jdbc.Driver url=jdbc:mysql://localho ...
- each,collect map collect! map!
arr = [1,2,3] 1) arr2 = arr.each{|element| element = element * 2} #arr与arr2仍然都等于[1,2,3] each返回原数组 ...
- windows 搭建 subversion+TortoiseSVN
1.版本 (a)Apache 2.2.25:httpd-2.2.25-win32-x86-no_ssl.msi (b)Subversion:Setup-Subversion-1.8.5.msi (c) ...
- 微信分享SDK
网址:http://www.8ru.org/weixin-js-sdk.html 下载demo:http://demo.open.weixin.qq.com/jssdk
- 深度可分离卷积结构(depthwise separable convolution)计算复杂度分析
https://zhuanlan.zhihu.com/p/28186857 这个例子说明了什么叫做空间可分离卷积,这种方法并不应用在深度学习中,只是用来帮你理解这种结构. 在神经网络中,我们通常会使用 ...
- map用法小例子
一. Map< Key , Value > m_Eg; 一般赋值表示成: TypeElem value; m_Eg[key] = value; 或 m_Eg.insert(make_ ...
- e595. Drawing an Image
See also e575 The Quintessential Drawing Program and e594 Reading an Image or Icon from a File. publ ...
- 在jsp中,page指令的()属性用来引入需要的包或类。
在jsp中,page指令的()属性用来引入需要的包或类. A.extends B.import C.language D.contentType 解答:B