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

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 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 3 4 8
output
YES
4 7 4 7
input
4 5
1 2 3 1
2 1 2 8
2 3 4 7
1 1 3 3
2 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 1000000007
using 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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值