A. Levko and Array Recovery----思维题

来源:互联网 发布:黑马java基础班测试题 编辑:程序博客网 时间:2024/05/16 08:55

A. Levko and Array Recovery
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Levko loves array a1, a2, ... , an, consisting of integers, very much. That is why Levko is playing with array a, performing all sorts of operations with it. Each operation Levko performs is of one of two types:

  1. Increase all elements from li to ri by di. In other words, perform assignments aj = aj + di for all j that meet the inequation li ≤ j ≤ ri.
  2. Find the maximum of elements from li to ri. That is, calculate the value .

Sadly, Levko has recently lost his array. Fortunately, Levko has records of all operations he has performed on array a. Help Levko, given the operation records, find at least one suitable array. The results of all operations for the given array must coincide with the record results. Levko clearly remembers that all numbers in his array didn't exceed 109 in their absolute value, so he asks you to find such an array.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 5000) — the size of the array and the number of operations in Levko's records, correspondingly.

Next m lines describe the operations, the i-th line describes the i-th operation. The first integer in the i-th line is integer ti (1 ≤ ti ≤ 2) that describes the operation type. If ti = 1, then it is followed by three integers liri and di (1 ≤ li ≤ ri ≤ n - 104 ≤ di ≤ 104) — the description of the operation of the first type. If ti = 2, then it is followed by three integers liri and mi (1 ≤ li ≤ ri ≤ n, - 5·107 ≤ mi ≤ 5·107) — the description of the operation of the second type.

The operations are given in the order Levko performed them on his array.

Output

In the first line print "YES" (without the quotes), if the solution exists and "NO" (without the quotes) otherwise.

If the solution exists, then on the second line print n integers a1, a2, ... , an (|ai| ≤ 109) — the recovered array.

Examples
input
4 51 2 3 12 1 2 82 3 4 71 1 3 32 3 4 8
output
YES4 7 4 7
input
4 51 2 3 12 1 2 82 3 4 71 1 3 32 3 4 13
output
NO

题目链接:http://codeforces.com/contest/360/problem/A


题目的意思是说对于一个序列有两个操作:

1.(1,l,r,p)..将区间[l,r]所有数都加上p

2.(2,l,r,m).求出区间[l,r]的最大值为m

现在给你这个操作,问你时候能够找到原序列,任意输出一个正确答案即可。


这个题我没有想出来,看了大牛的题解。

这个题就是求出每个位置初始时可能的最小值,然后判断是否合法。

代码:

#include <cstdio>#include <iostream>#include <cstring>#define inf 1000000007using namespace std;struct node{    int t,l,r,m;} op[5010];int a[5010],b[5010],n,m;bool solve(){    int p,i,k;    for (i=1;i<=n;i++){        a[i]=1000000000;    }    memset(b,0,sizeof(b));    for (p=1;p<=m;p++){        if (op[p].t==1)            for (i=op[p].l;i<=op[p].r;i++) b[i]+=op[p].m;        else            for (i=op[p].l;i<=op[p].r;i++)                a[i]=min(a[i],op[p].m-b[i]);    }    memset(b,0,sizeof(b));    for (p=1;p<=m;p++){        if (op[p].t==1)            for (i=op[p].l; i<=op[p].r; i++) b[i]+=op[p].m;        else{            k=-inf;            for(i=op[p].l;i<=op[p].r;i++)                k=max(k,a[i]+b[i]);            if(k!=op[p].m)                return false;        }    }    return true;}int main(){    int i;    scanf("%d%d",&n,&m);    for (i=1; i<=m; i++){        scanf("%d%d%d%d",&op[i].t,&op[i].l,&op[i].r,&op[i].m);    }    if (!solve()){        printf("NO\n");    }    else{        printf("YES\n");        for (i=1;i<=n;i++)            printf("%d ",a[i]);        cout<<endl;    }    return 0;}



原创粉丝点击