361C - Levko and Array Recovery - CodeForces Solution


brute force constructive algorithms greedy *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define int long long
#define ld long double

using namespace std;

struct Data {
    int t;
    int l;
    int r;
    int x;
};

int32_t main() {
    int n, m;
    cin >> n >> m;
    vector<Data> a(m);
    vector<int> res(n, -1e9);
    for (int i = 0; i < m; i++) {
        int t, l, r, x;
        cin >> t >> l >> r >> x;
        l--;
        r--;
        a[i].t = t;
        a[i].l = l;
        a[i].r = r;
        a[i].x = x;
        if (t == 2) {
            bool go = false;
            for (int j = l; j <= r; j++) {
                if (res[j] == -1e9 || res[j] >= x) {
                    res[j] = x;
                    go = true;
                }
            }
            if (!go) {
                cout << "NO";
                return 0;
            }
        } else {
            for (int j = l; j <= r; j++) {
                if (res[j] != -1e9) {
                    res[j] += x;
                }
            }
        }
    }
    for (int i = m - 1; i >= 0; i--) {
        if (a[i].t == 1) {
            for (int j = a[i].l; j <= a[i].r; j++) {
                res[j] -= a[i].x;
            }
        } else {
            int max1 = -1e9;
            for (int j = a[i].l; j <= a[i].r; j++) {
                max1 = max(max1, res[j]);
            }
            if (max1 != a[i].x) {
                cout << "NO";
                return 0;
            }
        }
    }
    cout << "YES" << '\n';
    for (int i : res) {
        if (i <= -1e9) {
            cout << 1000000000 << ' ';
        } else {
            cout << i << ' ';
        }
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good
1C - Ancient Berland Circus
721A - One-dimensional Japanese Crossword
1715B - Beautiful Array
60B - Serial Time
453A - Little Pony and Expected Maximum
1715A - Crossmarket
1715C - Monoblock
1512C - A-B Palindrome
1679B - Stone Age Problem
402A - Nuts
792A - New Bus Route
221A - Little Elephant and Function
492C - Vanya and Exams