Description

Byteasar has designed a supercomputer of novel architecture. It may comprise of many (identical) processing units. Each processing unit can execute a single instruction per time unit.
The programs for this computer are not sequential but rather have a tree structure. Each instruction may have zero, one, or multiple subsequent instructions, for which it is the parent instruction.
The instructions of the program can be executed in parallel on all available processing units. Moreover, they can be executed in many orders: the only restriction is that an instruction cannot be executed unless its parent instruction has been executed before. For example, as many subsequent instructions of an instruction that has been executed already can be executed in parallel as there are processing units.
Byteasar has a certain program to run. Since he likes utilizing his resources optimally, he is wondering how the number of processing units would affect the running time. He asks you to determine, for a given program and number of processing units, the minimum execution time of the program on a supercomputer with this many processing units.
 
给定一棵N个节点的有根树,根节点为1。
Q次询问,每次给定一个K,用最少的操作次数遍历完整棵树,输出最少操作次数。
每次操作可以选择访问不超过K个未访问的点,且这些点的父亲必须在之前被访问过。

Input

In the first line of standard input, there are two integers, N and Q (1<=N,Q<=1 000 000), separated by a single space, that specify the number of instructions in Byteasar's program and the number of running time queries (for different numbers of processing units).
In the second line of input, there is a sequence of Q integers, K1,k2,…Kq (1<=Ki<=1 000 000), separated by single spaces: Ki is the number of processing units in Byteasar's i-th query.
In the third and last input line, there is a sequence of N-1 integers, A2,A2…An (1<=Ai<i), separated by single spaces: Ai specifies the number of the parent instruction of the instruction number i. The instructions are numbered with successive integers from 1 to N, where the instruction no. 1 is the first instruction of the program.

Output

Your program should print one line consisting of Q integers, separated by single spaces, to the standard output: the i-th of these numbers should specify the minimum execution time of the program on a supercomputer with Ki processing units.

Sample Input

20 1
3
1 1 1 3 4 3 2 8 6 9 10 12 12 13 14 11 11 11 11

Sample Output

8

HINT

1
2
3
4
5
6
7
8
1    
2 3 4
5 6 7
8 10  
9 12  
11 13 14
15 16 17
18 19 20
感觉还是可以意会结论的,然后斜率优化一下。
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int maxn=1000010;
int n,m,d,A[maxn],h[maxn],s[maxn],st[maxn];
int Q[maxn],ans[maxn];
int main() {
n=read();m=read();h[1]=1;
rep(i,1,m) A[i]=read();
rep(i,2,n) d=max(d,h[i]=h[read()]+1);
rep(i,1,n) s[h[i]]++;
dwn(i,d,1) s[i]+=s[i+1];
rep(i,1,d) s[i]=s[i+1];
int l=1,r=0;
dwn(i,d,0) {
while(l<r&&(ll)(Q[r-1]-Q[r])*(s[i]-s[Q[r]])>=(ll)(Q[r]-i)*(s[Q[r]]-s[Q[r-1]])) r--;
Q[++r]=i;
}
dwn(i,n,1) {
while(l<r&&(ll)i*(Q[l]-Q[l+1])<=s[Q[l+1]]-s[Q[l]]) l++;
ans[i]=Q[l]+(s[Q[l]]?((s[Q[l]]-1)/i+1):0);
}
rep(i,1,m) printf("%d%c",ans[min(n,A[i])],i==m?'\n':' ');
return 0;
}

  

 

BZOJ3835: [Poi2014]Supercomputer的更多相关文章

  1. BZOJ3835[Poi2014]Supercomputer——斜率优化

    题目描述 Byteasar has designed a supercomputer of novel architecture. It may comprise of many (identical ...

  2. BZOJ3835 [Poi2014]Supercomputer 【斜率优化】

    题目链接 BZOJ3835 题解 对于\(k\),设\(s[i]\)为深度大于\(i\)的点数 \[ans = max\{i + \lceil \frac{s[i]}{k}\} \rceil\] 最优 ...

  3. 【BZOJ】3835: [Poi2014]Supercomputer

    题意 \(n(1 \le 1000000)\)个点的有根树,\(1\)号点为根,\(q(1 \le 1000000)\)次询问,每次给一个\(k\),每一次可以选择\(k\)个未访问的点,且父亲是访问 ...

  4. 题解-POI2014 Supercomputer

    Problem 辣鸡bzoj权限题,洛谷链接 题意概要:一棵 \(n\) 个点有根树.\(Q\) 次询问给出一个 \(K\),回答遍历完整棵树所需最少操作次数.每次操作可以选择访问不超过 \(K\) ...

  5. [POI2014]Supercomputer

    题目大意: 给定一个$n(n\le10^6)$个结点的有根树,从根结点开始染色.每次可以染和已染色结点相邻的任意$k$个结点.$q(q\le10^6)$组询问,每次给定$k$,问至少需要染几次? 思路 ...

  6. POI2014题解

    POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...

  7. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  8. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  9. BZOJ 3524: [Poi2014]Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][St ...

随机推荐

  1. 数据结构之图 Part3 – 2 遍历

    BFS using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...

  2. Oracle【IT实验室】数据库备份与恢复之五:Flashback

    Flashback在开发环境(有时生产环境的特殊情况下)是很有用的一个工具.     5.1 9i Flashback 简介     5.1.1  原理 当数据  update  或  delete  ...

  3. 获得H.264视频分辨率的方法

    转自:http://www.cnblogs.com/likwo/p/3531241.html 在使用ffmpeg解码播放TS流的时候(例如之前写过的UDP组播流),在连接时往往需要耗费大量时间.经过d ...

  4. error C2039: “bind2nd”: 不是“std”的成员

    VS2012 出现如下错误: error C2039: "bind2nd": 不是"std"的成员     头文件中加上 #include <functi ...

  5. Apache与Tomcat联系及区别(转)

    Apache与Tomcat都是Apache开源组织开发的用于处理HTTP服务的项目,两者都是免费的,都可以做为独立的Web服务器运行.Apache是Web服务器而Tomcat是Java应用服务器. A ...

  6. 一个简单的Object Hook的例子(win7 32bit)

    Object Hook简单的来说就是Hook对象,这里拿看雪上的一个例子,因为是在win7 32位上的,有些地方做了些修改. _OBJECT_HEADER: kd> dt _OBJECT_HEA ...

  7. Arduino101学习笔记(十)—— 串口通信

    //打开串口 Serial.begin(); //获取串口上可读取的数据的字节数.该数据是指已经到达并存储在接收缓存(共有64字节)中 Serial.available(); //读串口数据,串口上第 ...

  8. SurfaceView

    我们先来看下官方API对SurfaceView的介绍 SurfaceView的API介绍 Provides a dedicated drawing surface embedded inside of ...

  9. Android的Touch事件处理机制

    Android的Touch事件处理机制比较复杂,特别是在考虑了多点触摸以及事件拦截之后. Android的Touch事件处理分3个层面:Activity层,ViewGroup层,View层. 首先说一 ...

  10. SpringRMI解析3-RmiServiceExporter逻辑细节

    在发布RMI服务的流程中,有几个步骤可能是我们比较关心的. 获取registry 由于底层的封装,获取Registry实例是非常简单的,只需要使用一个函数LocateRegistry.createRe ...