BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序
BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序
Description
Input
Output
Sample Input
1 3
2 3
1 1 3
2 3 3
1 1 3
2 1 2
1 1 3
Sample Output
9
11
HINT
- #include <stdio.h>
- #include <string.h>
- #include <algorithm>
- #include <math.h>
- using namespace std;
- #define RR register
- inline char nc() {
- static char buf[100000],*p1,*p2;
- return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
- }
- inline int rd() {
- RR int x=0;RR char s=nc();
- while(s<'0'||s>'9') s=nc();
- while(s>='0'&&s<='9') x=(x<<3)+(x<<1)+s-'0',s=nc();
- return x;
- }
- #define N 100500
- int head[N],to[N],nxt[N],val[N],cnt,dfn[N],son[N],L[N],R[N],block,dis[N],n,m,pos[N],mx,t[N],more[N],size,fa[N],tot;
- struct A {
- int v,id;
- }a[N],b[N],c[N];
- inline bool cmp1(const A &x,const A &y) {
- return x.v<y.v;
- }
- inline void add(int u,int v,int w) {
- to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; val[cnt]=w;
- }
- void dfs(int x) {
- RR int i; dfn[x]=++tot; a[tot].v=dis[x]; a[tot].id=tot;
- for(i=head[x];i;i=nxt[i]) {
- dis[to[i]]=dis[x]+val[i];
- dfs(to[i]);
- }
- son[x]=tot;
- }
- inline void update(int l,int r,int v) {
- RR int p=pos[l],q=pos[r],i,j,k;
- RR int lb=0,lc=0;
- if(p==q) {
- for(i=L[p];i<=R[p];i++) {
- if(a[i].id<=r&&a[i].id>=l) b[++lb]=a[i],b[lb].v+=v;
- else c[++lc]=a[i];
- }
- i=1; j=1; k=L[p];
- while(i<=lb&&j<=lc) {
- if(b[i].v<=c[j].v) a[k++]=b[i++];
- else a[k++]=c[j++];
- }
- while(i<=lb) a[k++]=b[i++];
- while(j<=lc) a[k++]=c[j++];
- }else {
- for(i=p+1;i<q;i++) more[i]+=v;
- lb=0,lc=0;
- for(i=L[p];i<=R[p];i++) {
- if(a[i].id>=l) b[++lb]=a[i],b[lb].v+=v;
- else c[++lc]=a[i];
- }
- i=1; j=1; k=L[p];
- while(i<=lb&&j<=lc) {
- if(b[i].v<=c[j].v) a[k++]=b[i++];
- else a[k++]=c[j++];
- }
- while(i<=lb) a[k++]=b[i++];
- while(j<=lc) a[k++]=c[j++];
- lb=0; lc=0;
- for(i=L[q];i<=R[q];i++) {
- if(a[i].id<=r) b[++lb]=a[i],b[lb].v+=v;
- else c[++lc]=a[i];
- }
- i=1; j=1; k=L[q];
- while(i<=lb&&j<=lc) {
- if(b[i].v<=c[j].v) a[k++]=b[i++];
- else a[k++]=c[j++];
- }
- while(i<=lb) a[k++]=b[i++];
- while(j<=lc) a[k++]=c[j++];
- }
- }
- inline int search(int l,int r,int x) {
- RR int tmp=l,p=pos[l]; r++;
- x-=more[p];
- while(l<r) {
- int mid=(l+r)>>1;
- if(a[mid].v<=x) l=mid+1;
- else r=mid;
- }
- return l-tmp;
- }
- inline int findt(int x) {
- RR int l=1,r=t[0]+1,mid;
- while(l<r) {
- mid=(l+r)>>1;
- if(t[mid]<=x) l=mid+1;
- else r=mid;
- }
- return l-1;
- }
- inline int query(int x,int y,int k) {
- if(y-x+1<k) return -1;
- RR int p=pos[x],q=pos[y],i,j;
- if(p==q) {
- t[0]=0;
- for(i=L[p];i<=R[p];i++) {
- if(a[i].id>=x&&a[i].id<=y) t[++t[0]]=a[i].v+more[p];
- }
- }else {
- t[0]=0;
- RR int lb=0,lc=0;
- for(i=L[p];i<=R[p];i++) {
- if(a[i].id>=x) b[++lb]=a[i],b[lb].v+=more[p];
- }
- for(i=L[q];i<=R[q];i++) {
- if(a[i].id<=y) c[++lc]=a[i],c[lc].v+=more[q];
- }
- i=1; j=1;
- while(i<=lb&&j<=lc) {
- if(b[i].v<=c[j].v) t[++t[0]]=b[i++].v;
- else t[++t[0]]=c[j++].v;
- }
- while(i<=lb) t[++t[0]]=b[i++].v;
- while(j<=lc) t[++t[0]]=c[j++].v;
- }
- RR int l=0,r=mx+1,mid,re;
- while(l<r) {
- mid=(l+r)>>1; re=0;
- if(t[0]) re+=findt(mid);
- for(i=p+1;i<q;i++) re+=search(L[i],R[i],mid);
- if(re>=k) r=mid;
- else l=mid+1;
- }
- return l;
- }
- int main() {
- //freopen("a.in","r",stdin);
- //freopen("a.out","w",stdout);
- RR int kitty,opt;
- n=rd(); m=rd(); kitty=rd();
- RR int i,x,y,j;
- // int ln=n,lg=1;
- // while(ln) lg++,ln>>=1;
- for(i=2;i<=n;i++) {
- x=rd(); y=rd(); add(x,i,y);
- }
- // size=((int)ceil(sqrt(n)))*(lg);
- // printf("%d\n",size);return 0;
- size=3700;
- dfs(1);
- block=n/size;
- for(i=1;i<=block;i++) {
- L[i]=R[i-1]+1; R[i]=i*size;
- sort(a+L[i],a+R[i]+1,cmp1);
- mx=max(mx,a[R[i]].v);
- for(j=L[i];j<=R[i];j++) pos[j]=i;
- }
- if(R[block]!=n) {
- block++; L[block]=R[block-1]+1; R[block]=n;
- sort(a+L[block],a+n+1,cmp1);
- for(i=L[block];i<=n;i++) pos[i]=block;
- mx=max(mx,a[n].v);
- }
- while(m--) {
- opt=rd(); x=rd(); y=rd();
- if(opt==1) {
- printf("%d\n",query(dfn[x],son[x],y));
- }else {
- update(dfn[x],son[x],y); mx+=y;
- }
- }
- return 0;
- }
BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序的更多相关文章
- BZOJ_4765_普通计算姬_分块+dfs序+树状数组
BZOJ_4765_普通计算姬_分块 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能 ...
- BZOJ4867 : [Ynoi2017]舌尖上的由乃
首先通过DFS序将原问题转化为序列上区间加.询问区间kth的问题. 考虑分块,设块大小为$K$,每块维护排序过后的$pair(值,编号)$. 对于修改,整块的部分可以直接打标记,而零碎的两块因为本来有 ...
- BZOJ4867 Ynoi2017舌尖上的由乃(dfs序+分块)
容易想到用dfs序转化为序列上的问题.考虑分块,对每块排序,修改时对于整块打上标记,边界暴力重构排序数组,询问时二分答案,这样k=sqrt(nlogn)时取最优复杂度nsqrt(nlogn)logn, ...
- (原创)舌尖上的c++--相逢
引子 前些时候,我在群里出了一道题目:将变参的类型连接在一起作为字符串并返回出来,要求只用函数实现,不能借助于结构体实现.用结构体来实现比较简单: template<typename... Ar ...
- 舌尖上的硬件:CPU/GPU芯片制造解析(高清)(组图)
一沙一世界,一树一菩提,我们这个世界的深邃全部蕴藏于一个个普通的平凡当中.小小的厨房所容纳的不仅仅是人们对味道的情感,更有推动整个世界前进的动力.要想理解我们的世界,有的时候只需要细细品味一下我们所喜 ...
- BZOJ_3252_攻略_线段树+dfs序
BZOJ_3252_攻略_线段树+dfs序 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏.今天他得到了一款新游戏< ...
- BZOJ_3729_Gty的游戏_博弈论+splay+dfs序
BZOJ_3729_Gty的游戏_博弈论+splay+dfs序 Description 某一天gty在与他的妹子玩游戏. 妹子提出一个游戏,给定一棵有根树,每个节点有一些石子,每次可以将不多于L的石子 ...
- BZOJ_2821_作诗(Poetize)_分块
BZOJ_2821_作诗(Poetize)_分块 Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要 ...
- BZOJ_2434_[NOI2011]_阿狸的打字机_(AC自动机+dfs序+树状数组)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=2434 给出\(n\)个字符串,\(m\)个询问,对于第\(i\)个询问,求第\(x_i\)个字 ...
随机推荐
- dll附加依赖项查看——dumpbin 命令
VS自带工具 查看程序或动态链接库需要的动态链接库 dumpbin /dependents d:\test.exe 查看动态链接库的输出函数 dumpbin /exports d:\libmysq ...
- POSTGRESQL 并发控制
http://meidayhxp.blog.163.com/blog/static/117608156201210243837491/ 这个内容是官方Doc中的一章,具体是那一版的,还未确认. 第九章 ...
- python3学习笔记4---引用http://python3-cookbook.readthedocs.io/zh_CN/latest/
2018-03-01数据结构与算法(4) 1.16过滤序列元素 最简单的过滤序列元素的方法就是使用列表推导.比如: >>> mylist = [1, 4, -5, 10, -7, 2 ...
- eBay账号token授权
1.注册开发者账号(https://go.developer.ebay.com/) hufangyong hu6253859. 2.注册沙箱测试账号(http://sandbox.ebay.com ...
- Heap
#include using namespace std; int heap[100010],cnt=0; void put(int x) { cnt++; heap[cnt]=x; int now= ...
- Java开源生鲜电商平台-系统架构与技术选型(源码可下载)
Java开源生鲜电商平台-系统架构与技术选型(源码可下载) 1. 硬件环境 公司服务器 2. 软件环境 2.1 操作系统 Linux CentOS 6.8系列 2.2 反向代理/web服务器 ...
- ArrayList源码分析超详细
ArrayList源码分析超详解 想要分析下源码是件好事,但是如何去进行分析呢?以我的例子来说,我进行源码分析的过程如下几步: 找到类:利用 IDEA 找到所需要分析的类(ztrl+N查找ArraLi ...
- 基于DP的矩阵连乘问题
当多个连续可乘矩阵做乘法时,选择正确的做乘顺序可以有效减少做乘法的次数,而选择的方法可以很容易的通过DP实现. 原理就是对于每一个所求矩阵,搜索所有可以相乘得到它的方法,比较它们的消耗,选取最小值作为 ...
- js中几种实用的跨域方法原理详解【转】
源地址:http://www.cnblogs.com/2050/p/3191744.html 这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通 ...
- IDEA安装教程
1.下载安装程序A,链接:https://pan.baidu.com/s/1IAsGDbApfyNsHuS7_m0rdw 密码:fthp 2.下载一个配置程序B,下载安装之后,暂时不用管,之后会用到. ...