codeforces 1023 D Array Restoration (tree array)

Topic

Insert picture description here

Title

There is a sequence of length n. You can make q modifications. The i-th modification changes the number of the interval [l,r] to i. The q modifications involved must cover every number in the interval, and q modifications After that, change some numbers in this sequence to 0 to get a new sequence (that is, the input sequence), and ask if you can use this new sequence to find a legal sequence before it becomes 0

answer

  1. The required string must have q, because q is the last modification, and there must be q after modification, so the input sequence is required to contain either q or 0 (we can change 0 to q), If the input sequence has neither q nor 0, then it must output NO
  1. If you want to have a legal sequence, you must ensure that the input sequence is also legal. For the input sequence, because each modification is to modify an interval, and it must become larger after modification, it is impossible for two identical numeric intervals to appear Numbers smaller than it, such as 6 5 6 are definitely illegal. For the 6th modification, we can only modify the number of a range to 6 and only this one chance, so the modification can only be 5 also Modified together, the above situation is impossible
  1. Knowing this property, we can use the tree array to cleverly judge whether it is a legal sequence. For the number that appears for the first time, we can mark how many numbers are smaller than it. When we encounter this number for the second time, we As long as it is judged whether there are too many numbers smaller than it, if it is too much, then there must be a number smaller than this number in this interval. The design manipulation is not to add 1 to a position, and then query the prefix and (less than this number) Number), a proper tree array
  1. For the output, it is definitely more convenient to have the same number of consecutive segments, so for 0, we can directly make it equal to the previous number, but it should be noted that when the input sequence does not have q, we must first convert 0 to q (Because a q must appear in the sequence)

Code

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>

using namespace std;
const int N = 2e5 + 10;

int n, q;
int a[N], vis[N];
int tr[N];

int lowbit(int x) {
    
    
    return x & -x;
}

void add(int x, int c) {
    
    
    for (int i = x; i <= q; i += lowbit(i)) tr[i] += c;
}

int sum(int x) {
    
    
    int res = 0;
    for (int i = x; i; i -= lowbit(i)) res += tr[i];
    return res;
}

int main() {
    
    

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    cin >> n >> q;
    for (int i = 1; i <= n; i++) cin >> a[i];

    bool flag = true;
    for (int i = 1; i <= n; i++) {
    
    
        if (a[i] == 0) continue;
        if (vis[a[i]]) {
    
       //说明之前出现过
            if (vis[a[i]] != sum(a[i])) {
    
      //说明中间出现过小于a[i]的数
                flag = false;
                break;
            }
        }
        add(a[i], 1);
        vis[a[i]] = sum(a[i]);
    }

    if (flag) {
    
    
        bool zero = false, isq = false;
        for (int i = 1; i <= n; i++) {
    
    
            if (a[i] == 0) zero = true;
            if (a[i] == q) isq = true;
        }
        if (!(zero || isq)) {
    
    
            cout << "NO" << endl;
        } else {
    
    
            a[0] = 1;
            for (int i = 1; i <= n; i++) {
    
    
                if (a[i] == 0) {
    
    
                    if (!isq) {
    
    
                        a[i] = q;
                        isq = true;
                    } else {
    
    
                        a[i] = a[i - 1];
                    }
                }
            }
            cout << "YES" << endl;
            for (int i = 1; i <= n; i++) {
    
    
                cout << a[i] << " ";
            }
            cout << endl;
        }
    } else {
    
    
        cout << "NO" << endl;
    }

    return 0;
}

Guess you like

Origin blog.csdn.net/qq_44791484/article/details/114107011