【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组
【BZOJ4009】[HNOI2015]接水果
Description
Input
第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数。
Output
对于每个果子,输出一行表示选择的盘子的权值。
Sample Input
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
3 2 217394434
10 7 13022269
6 7 283254485
6 8 333042360
4 6 442139372
8 3 225045590
10 4 922205209
10 8 808296330
9 2 486331361
4 9 551176338
1 8 5
3 8 3
3 8 4
1 8 3
4 8 1
2 3 1
2 3 1
2 3 1
2 4 1
1 4 1
Sample Output
333042360
442139372
283254485
283254485
217394434
217394434
217394434
217394434
217394434
HINT
N,P,Q<=40000。
题解:觉得很熟悉?赶紧去复习精神污染吧!
设入栈序p,出栈序q,那么a-b是x-y的子路径有以下两种情况(p[a]<p[b],p[x]<p[y])。
a是b的祖先,那么设b在a的c儿子的子树中,则有1<=p[x]<p[c],q[c]<p[y]<=n。
a不是b的祖先,则有p[a]<=p[x]<=q[a],p[b]<=p[y]<=q[b]。
这就变成了平面上有一堆矩形,每个矩形都有一个权值,每次询问一个点,问在所有包含这个点的矩形中,权值第k小的权值是多少。可以用树套树做,当然,用整体二分+扫描线+树状数组区间修改也是极好的啦~
- #include <cstdio>
- #include <iostream>
- #include <cstring>
- #include <algorithm>
- using namespace std;
- const int maxn=40010;
- int n,tot,tp,m1,m2,cnt,now;
- int to[maxn<<1],next[maxn<<1],head[maxn],p1[maxn],p2[maxn],fa[16][maxn],dep[maxn],ans[maxn],s[maxn],vis[maxn];
- struct operation
- {
- int a1,b1,a2,b2,v;
- operation() {}
- operation(int _1,int _2,int _3,int _4,int _5) {a1=_1,b1=_2,a2=_3,b2=_4,v=_5;}
- }p[maxn<<1];
- struct ask
- {
- int a,b,v,sl,org;
- ask() {}
- ask(int _1,int _2,int _3,int _4) {a=_1,b=_2,v=_3,org=_4;}
- }q[maxn],qq[maxn];
- struct node
- {
- int x,a,b,v;
- node() {}
- node(int _1,int _2,int _3,int _4) {x=_1,a=_2,b=_3,v=_4;}
- }pp[maxn<<1];
- 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;
- }
- void add(int a,int b)
- {
- to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
- }
- void dfs(int x)
- {
- p1[x]=++p1[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]);
- p2[x]=p1[0];
- }
- void updata(int x,int v)
- {
- for(int i=x;i<=n;i+=i&-i)
- {
- if(vis[i]<now) vis[i]=now,s[i]=0;
- s[i]+=v;
- }
- }
- int query(int x)
- {
- int i,ret=0;
- for(i=x;i;i-=i&-i)
- {
- if(vis[i]<now) vis[i]=now,s[i]=0;
- ret+=s[i];
- }
- return ret;
- }
- bool cmpx(node a,node b)
- {
- return a.x<b.x;
- }
- bool cmpa(ask a,ask b)
- {
- return a.a<b.a;
- }
- bool cmpv(operation a,operation b)
- {
- return a.v<b.v;
- }
- void solve(int l,int r,int L,int R)
- {
- if(l==r||L>R)
- {
- for(int i=L;i<=R;i++) ans[q[i].org]=p[l].v;
- return ;
- }
- sort(p+l,p+r+1,cmpv);
- int mid=(l+r)>>1,MID=L-1,i,j;
- for(tp=0,now++,i=l;i<=mid;i++)
- pp[++tp]=node(p[i].a1,p[i].a2,p[i].b2,1),pp[++tp]=node(p[i].b1+1,p[i].a2,p[i].b2,-1);
- sort(pp+1,pp+tp+1,cmpx),sort(q+L,q+R+1,cmpa);
- for(i=L,j=1;i<=R;i++)
- {
- for(;j<=tp&&pp[j].x<=q[i].a;j++) updata(pp[j].a,pp[j].v),updata(pp[j].b+1,-pp[j].v);
- q[i].sl=query(q[i].b),MID+=(q[i].sl>=q[i].v);
- }
- int h1=L,h2=MID+1;
- for(i=L;i<=R;i++)
- {
- if(q[i].sl>=q[i].v) qq[h1++]=q[i];
- else q[i].v-=q[i].sl,qq[h2++]=q[i];
- }
- for(i=L;i<=R;i++) q[i]=qq[i];
- solve(l,mid,L,MID),solve(mid+1,r,MID+1,R);
- }
- int FA(int x,int y)
- {
- for(int j=15;j>=0;j--) if((1<<j)<=y) y-=(1<<j),x=fa[j][x];
- return x;
- }
- int main()
- {
- //freopen("bz4009.in","r",stdin);
- n=rd(),m1=rd(),m2=rd();
- int i,j,a,b,c,d;
- memset(head,-1,sizeof(head));
- for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a);
- dep[1]=1,dfs(1);
- for(j=1;(1<<j)<=n;j++) for(i=1;i<=n;i++) fa[j][i]=fa[j-1][fa[j-1][i]];
- for(i=1;i<=m1;i++)
- {
- a=rd(),b=rd(),c=rd();
- if(p1[a]>p1[b]) swap(a,b);
- if(p2[a]>=p2[b])
- {
- d=FA(b,dep[b]-dep[a]-1);
- p[++tot]=operation(1,p1[d]-1,p1[b],p2[b],c);
- if(p2[b]<n) p[++tot]=operation(p1[b],p2[b],p2[d]+1,n,c);
- }
- else p[++tot]=operation(p1[a],p2[a],p1[b],p2[b],c);
- }
- for(i=1;i<=m2;i++)
- {
- a=rd(),b=rd();
- if(p1[a]>p1[b]) swap(a,b);
- q[i]=ask(p1[a],p1[b],rd(),i);
- }
- solve(1,tot,1,m2);
- for(i=1;i<=m2;i++) printf("%d\n",ans[i]);
- return 0;
- }//4 4 1 1 2 2 3 1 4 1 2 2 2 3 3 1 4 4 4 1 1 2 4 1
【BZOJ4009】[HNOI2015]接水果 DFS序+整体二分+扫描线+树状数组的更多相关文章
- BZOJ 4009: [HNOI2015]接水果 (整体二分+扫描线 树状数组)
整体二分+扫描线 树状数组 具体做法看这里a CODE #include <cctype> #include <cstdio> #include <cstring> ...
- bzoj4009 [HNOI2015]接水果 整体二分+扫描线+树状数组+dfs序
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4009 题解 考虑怎样的情况就会有一个链覆盖另一个链. 设被覆盖的链为 \(a - b\),覆盖 ...
- Luogu3527 POI2011 Meteors 整体二分、树状数组、差分
传送门 比较板子的整体二分题目,时限有点紧注意常数 整体二分的过程中将时间在\([l,mid]\)之间的流星使用树状数组+差分进行维护,然后对所有国家查看一遍并分好类,递归下去,记得消除答案在\([m ...
- [bzoj2527][Poi2011]Meteors_整体二分_树状数组
Meteors bzoj-2527 Poi-2011 题目大意:题目链接. 注释:略. 想法: 首先答案可以离线,且具有单调性. 这里的单调性就是随着时间的推移,每个国家收集的陨石数增加. 不难想到整 ...
- [bzoj2738]矩阵乘法_整体二分_树状数组
矩阵乘法 bzoj-2738 题目大意:给定一个$n*n$的矩阵.每次给定一个矩阵求矩阵$k$小值. 注释:$1\le n\le 500$,$1\le q\le 6\cdot 10^4$. 想法: 新 ...
- [POI2011]MET-Meteors 整体二分_树状数组_卡常
线段树肯定会 TLE 的,必须要用树状数组. Code: // luogu-judger-enable-o2 #include <cstdio> #include <algorith ...
- [luogu4479][BJWC2018]第k大斜率【二维偏序+二分+离散化+树状数组】
传送门 https://www.luogu.org/problemnew/show/P4479 题目描述 在平面直角坐标系上,有 n 个不同的点.任意两个不同的点确定了一条直线.请求出所有斜率存在的直 ...
- 【bzoj4009】[HNOI2015]接水果 DFS序+树上倍增+整体二分+树状数组
题目描述 给出一棵n个点的树,给定m条路径,每条路径有一个权值.q次询问求一个路径包含的所有给定路径中权值第k小的. 输入 第一行三个数 n和P 和Q,表示树的大小和盘子的个数和水果的个数. 接下来n ...
- Codeforces 899 F. Letters Removing (二分、树状数组)
题目链接:Letters Removing 题意: 给你一个长度为n的字符串,给出m次操作.每次操作给出一个l,r和一个字符c,要求删除字符串l到r之间所有的c. 题解: 看样例可以看出,这题最大的难 ...
随机推荐
- centos7 mongodb3.2与3.4版本安装(转)
一.安装环境及配置yum vi /etc/yum.repos.d/mongodb-org-3.2.repo [mongodb-org-3.2] name=MongoDB Repository base ...
- opengl中VAO,VBO,IBO用法小结【转】
http://cowboy.1988.blog.163.com/blog/static/751057982014380251300/ opengl中VAO,VBO,IBO用法小结 这三个玩意全面取代旧 ...
- C#中out与ref区别
一.ref(参考)与out区别 1.out(只出不进) 将方法中的参数传递出去,在方法中将该参数传递出去之前需要在该方法起始赋初值:在方法外传递的该参数可以不用赋值: 简单理解就是:将一个东西抛出去之 ...
- JAX-WS编写webservice
1.新建一个Java工程 2.创建要发布的类 package com.linjian.webservice; import javax.jws.WebMethod; import javax.jws. ...
- Node.js 内存泄露 定位
之前我们在64位Linux服务器上使用Node.js时,当Node进程物理内存接近1.6G,由于谷歌V8引擎对内存的限制,会导致进程退出! 显然我们自身编码或npm加载的第3行模块存在内存泄露问题,那 ...
- PS 如何制作环绕文字效果
最终效果 地球素材 1.打开素材,使用椭圆选区工具按住shift绘制正圆选区 2.转到路径面板,将选区变为工作路径 3.选择文字工具,在路径上输入文字 4.ctrl+T,按住ctrl+alt,鼠标拖动 ...
- Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
1.Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'. 原因解析: gradle ...
- 使用 react-native-tab-navigator 创建 TabBar 组件
1.首先安装好ReactNative的运行环境,安装组件依赖库 使用npm install react-native-tab-navigator --save安装所依赖的第三方库 2.导入 impor ...
- ddmrp
DDMRP 特点 在供应链加入 mts 缓冲,解耦 lead time, 缩小 bullwhip 效应,最小化库存 buffer动态调整 buffer 分3个颜色共 4个区域[zone],为 gree ...
- rabbitmq 用户和授权
官方文档 https://my.oschina.net/hncscwc/blog/262246?p=