Codeforces Round #381 (Div. 1)

来源:互联网 发布:国家数据保密协议 编辑:程序博客网 时间:2024/06/03 18:59
A. Alyona and mex
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alyona's mother wants to present an array of n non-negative integers to Alyona. The array should be special.

Alyona is a capricious girl so after she gets the array, she inspects m of its subarrays. Subarray is a set of some subsequent elements of the array. The i-th subarray is described with two integers li and ri, and its elements are a[li], a[li + 1], ..., a[ri].

Alyona is going to find mex for each of the chosen subarrays. Among these m mexes the girl is going to find the smallest. She wants this minimum mex to be as large as possible.

You are to find an array a of n elements so that the minimum mex among those chosen by Alyona subarrays is as large as possible.

The mex of a set S is a minimum possible non-negative integer that is not in S.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 105).

The next m lines contain information about the subarrays chosen by Alyona. The i-th of these lines contains two integers li and ri(1 ≤ li ≤ ri ≤ n), that describe the subarray a[li], a[li + 1], ..., a[ri].

Output

In the first line print single integer — the maximum possible minimum mex.

In the second line print n integers — the array a. All the elements in a should be between 0 and 109.

It is guaranteed that there is an optimal answer in which all the elements in a are between 0 and 109.

If there are multiple solutions, print any of them.

Examples
input
5 31 32 54 5
output
21 0 2 1 0
input
4 21 42 4
output
35 2 0 1
Note

The first example: the mex of the subarray (1, 3) is equal to 3, the mex of the subarray (2, 5) is equal to 3, the mex of the subarray (4, 5) is equal to 2 as well, thus the minumal mex among the subarrays chosen by Alyona is equal to 2.


#include <cstdio>#include <algorithm>using namespace std;int main(){int n, m, a, b;scanf("%d%d", &n, &m);int mex = 1000000;for (int i = 0; i < m; i++){scanf("%d%d", &a, &b);mex = min(b - a, mex);}printf("%d\n", mex + 1);for (int i = 1; i <= n; i++)printf("%d ", i % (mex + 1));printf("\n");return 0;}


B. Alyona and a tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wrote ai. Moreover, the girl wrote a positive integer to every edge of the tree (possibly, different integers on different edges).

Let's define dist(v, u) as the sum of the integers written on the edges of the simple path from v to u.

The vertex v controls the vertex u (v ≠ u) if and only if u is in the subtree of v and dist(v, u) ≤ au.

Alyona wants to settle in some vertex. In order to do this, she wants to know for each vertex v what is the number of vertices u such that v controls u.

Input

The first line contains single integer n (1 ≤ n ≤ 2·105).

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the integers written in the vertices.

The next (n - 1) lines contain two integers each. The i-th of these lines contains integers pi and wi (1 ≤ pi ≤ n1 ≤ wi ≤ 109) — the parent of the (i + 1)-th vertex in the tree and the number written on the edge between pi and (i + 1).

It is guaranteed that the given graph is a tree.

Output

Print n integers — the i-th of these numbers should be equal to the number of vertices that the i-th vertex controls.

Examples
input
52 5 1 4 61 71 13 53 6
output
1 0 1 0 0
input
59 7 8 6 51 12 13 14 1
output
4 3 2 1 0
Note

In the example test case the vertex 1 controls the vertex 3, the vertex 3 controls the vertex 5 (note that is doesn't mean the vertex 1controls the vertex 5).


#include <cstdio>#include <vector>#include <algorithm>using namespace std;const int N = 200005;typedef long long ll;int n, val[N], ans[N], add[N];vector <pair<int, int> > edge[N];vector <pair<ll, int> > DIS;void dfs(int x, ll dis){    DIS.push_back(make_pair(dis, x));    int pos = lower_bound(DIS.begin(), DIS.end(), make_pair(dis - val[x], -1)) - DIS.begin();    if (pos > 0) add[DIS[pos - 1].second] --;    if (DIS.size() > 1) add[DIS[DIS.size() - 2].second] ++;    for (int i = 0; i < edge[x].size(); i++)    {        int child = edge[x][i].first;        int w = edge[x][i].second;        dfs(child, dis + w);        add[x] += add[child];    }    ans[x] = add[x];    DIS.pop_back();}int main(){    scanf("%d", &n);    for (int i = 1; i <= n; i++)        scanf("%d", &val[i]);    for (int i = 2; i <= n; i++)    {        int a, b;        scanf("%d%d", &a, &b);        edge[a].push_back(make_pair(i, b));    }    dfs (1, 0);    for (int i = 1; i <= n; i++)        printf("%d ", ans[i]);    return 0;}

C. Alyona and towers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Alyona has built n towers by putting small cubes some on the top of others. Each cube has size 1 × 1 × 1. A tower is a non-zero amount of cubes standing on the top of each other. The towers are next to each other, forming a row.

Sometimes Alyona chooses some segment towers, and put on the top of each tower several cubes. Formally, Alyouna chooses some segment of towers from li to ri and adds di cubes on the top of them.

Let the sequence a1, a2, ..., an be the heights of the towers from left to right. Let's call as a segment of towers al, al + 1, ..., ar a hill if the following condition holds: there is integer k (l ≤ k ≤ r) such that al < al + 1 < al + 2 < ... < ak > ak + 1 > ak + 2 > ... > ar.

After each addition of di cubes on the top of the towers from li to ri, Alyona wants to know the maximum width among all hills. The width of a hill is the number of towers in it.

Input

The first line contain single integer n (1 ≤ n ≤ 3·105) — the number of towers.

The second line contain n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the number of cubes in each tower.

The third line contain single integer m (1 ≤ m ≤ 3·105) — the number of additions.

The next m lines contain 3 integers each. The i-th of these lines contains integers liri and di (1 ≤ l ≤ r ≤ n1 ≤ di ≤ 109), that mean that Alyona puts di cubes on the tio of each of the towers from li to ri.

Output

Print m lines. In i-th line print the maximum width of the hills after the i-th addition.

Example
input
55 5 5 5 531 3 22 2 14 4 1
output
245
Note

The first sample is as follows:

After addition of 2 cubes on the top of each towers from the first to the third, the number of cubes in the towers become equal to [7, 7, 7, 5, 5]. The hill with maximum width is [7, 5], thus the maximum width is 2.

After addition of 1 cube on the second tower, the number of cubes in the towers become equal to [7, 8, 7, 5, 5]. The hill with maximum width is now [7, 8, 7, 5], thus the maximum width is 4.

After addition of 1 cube on the fourth tower, the number of cubes in the towers become equal to [7, 8, 7, 6, 5]. The hill with maximum width is now [7, 8, 7, 6, 5], thus the maximum width is 5.


#include <cstdio>#include <algorithm>using namespace std;const int MAXN = 300010;typedef long long ll;int n, M, height[MAXN];ll minus[MAXN];struct TREENODE{    int l, m, r;}node[4 * MAXN];int sign(ll x){    if (x > 0) return 1;    if (x < 0) return -1;    return 0;}void push_up(int l, int r, int o){    int m = (l + r) / 2;    node[o].m = max(node[o * 2].m, node[o * 2 + 1].m);    node[o].l = node[o * 2].l;    node[o].r = node[o * 2 + 1].r;    if (!!minus[m] && !!minus[m + 1] && sign(minus[m]) >= sign(minus[m + 1]))    {        node[o].m = max(node[o].m, node[o * 2].r + node[o * 2 + 1].l);        if (node[o * 2].m == (m - l + 1)) node[o].l = node[o * 2].l + node[o * 2 + 1].l;         if (node[o * 2 + 1].m == (r - m)) node[o].r = node[o * 2 + 1].r + node[o * 2].r;    }}void build_tree(int l, int r, int o){    if (l == r)    {        int num = !!minus[l];        node[o] = {num, num, num};        return ;        }    int m = (l + r) / 2;    build_tree(l, m, o * 2);    build_tree(m + 1, r, o * 2 + 1);    push_up(l, r, o);}void update(int l, int r, int o, int pos, int d){    if (l == r)    {        minus[pos] += d;        int num = !!minus[pos];        node[o] = {num, num, num};        return ;    }    int m = (l + r) / 2;    if (pos <= m) update(l, m, o * 2, pos, d);    else update(m + 1, r, o * 2 + 1, pos, d);    push_up(l, r, o);}int main(){    scanf("%d", &n);    for (int i = 1; i <= n; i++)        scanf("%d", &height[i]);    for (int i = 1; i < n; i++)        minus[i] = height[i + 1] - height[i];    if (n != 1)        build_tree(1, n - 1 , 1);    scanf("%d", &M);    int l, r, delta;    for (int i = 1; i <= M; i++)    {        if (n == 1)        {            printf("1\n");            continue;        }        scanf("%d%d%d", &l, &r, &delta);        if (l != 1) update(1, n - 1, 1, l - 1, delta);        if (r != n) update(1, n - 1, 1, r, -delta);        printf("%d\n", node[1].m + 1);    }    return 0;}


0 0
原创粉丝点击