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. 【数据结构】建立和平衡AVL二叉树

    一步一步写平衡二叉树(AVL树) 原文地址:http://www.cppblog.com/cxiaojia/archive/2012/08/20/187776.html 我添加了一些内容,以充实整个算 ...

  2. [LeetCode] Word Break II (TLE)

    Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...

  3. Solr入门之(8)中文分词器配置

    Solr中虽然提供了一个中文分词器,但是效果很差,可以使用IKAnalyzer或Mmseg4j 或其他中文分词器. 一.IKAnalyzer分词器配置: 1.下载IKAnalyzer(IKAnalyz ...

  4. go sample - mongodb

    简单的mongodb 操作 package mainimport ( "fmt" "gopkg.in/mgo.v2" "gopkg.in/mgo.v2 ...

  5. html5 svg

    html5 svg <html > <body> <p>canvas 用js 绘画,是整幅画布,适合游戏 svg可放大,支持dom 操作,js事件 线性渐变.高斯模 ...

  6. Reading and Writing CSV Files in C#

    Introduction A common requirement is to have applications share data with other programs. Although t ...

  7. Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

    D. Directed Roads   ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...

  8. 【SQL Sever】实现SQL Sever的发布。订阅。 双机热备

    实现SQL Sever的发布和订阅  最大的好处就是: 可以实现读写分离,增删改操作在主数据库服务器上进行,查询在备份数据库服务器上进行.一方面提高软件执行效率,另一方面也减轻主库压力. 本次实现发布 ...

  9. 当 NSDictionary 遇见 nil

    Demo project: NSDictionary-NilSafe 问题 相信用 Objective-C 开发 iOS 应用的人对下面的 crash 不会陌生: *** -[__NSPlacehol ...

  10. iOS UIImageView设置为圆形

    UIImageView设置为圆形的方法(效率比较低下,当需要显示很多圆形view的时候,非常不推荐这种方式): imageView.layer.masksToBounds = YES; imageVi ...