23832023-01-12 09:41:01rennRendezéscpp11Accepted 40/4050ms7412 KiB
#include <bits/stdc++.h>
using namespace std;

#define InTheNameOfGod cin.tie(0); ios::sync_with_stdio(0);
#define inv pair<int, int>

void print(vector<int> &s)
{
    for(auto &x : s)
        cout << x << " ";
    cout << "\n";
}

void print(vector<inv> &s)
{
    for(auto &x : s)
        cout << x.first << ":" << x.second << " ";
    cout << "\n";
}

int main()
{
    InTheNameOfGod

    int n, k, c = 0;
    cin >> n >> k;

    vector<int> s(n+2);
    for(int i = 1; i <= n; i++)
    {
        cin >> s[i];
    }

    vector<inv> ss;
    ss.reserve(k);
    queue<inv> done;
    for(int i = 0, a, b; i < k; i++)
    {
        cin >> a >> b;
        ss.push_back({a, b});
    }

    sort(ss.begin(), ss.end());

    inv z(-1, -1);
    for(inv &x : ss)
    {
        if(x.first <= z.second)
        {
            z.second = max(z.second, x.second);
            continue;
        }

        done.push(z);
        z.first = x.first;
        z.second = x.second;
    }
    done.push(z);
    done.pop();

    while(!done.empty())
    {
        z = done.front();
        done.pop();
        sort(s.begin()+z.first, s.begin()+z.second+1);
    }

    for(int i = 1; i <= n; i++)
    {
        c = i == s[i] ? c+1 : c;
    }
    cout << c << "\n";

    return 0;
}
SubtaskSumTestVerdictTimeMemory
base40/40
1Accepted0/03ms1828 KiB
2Accepted0/08ms2312 KiB
3Accepted2/22ms2220 KiB
4Accepted2/22ms2424 KiB
5Accepted2/22ms2632 KiB
6Accepted2/22ms2856 KiB
7Accepted2/22ms2928 KiB
8Accepted2/22ms3052 KiB
9Accepted2/22ms3276 KiB
10Accepted2/23ms3608 KiB
11Accepted2/246ms6712 KiB
12Accepted2/246ms7192 KiB
13Accepted2/246ms7244 KiB
14Accepted1/146ms7312 KiB
15Accepted2/246ms7412 KiB
16Accepted2/248ms6700 KiB
17Accepted2/250ms6552 KiB
18Accepted2/250ms6608 KiB
19Accepted2/250ms6808 KiB
20Accepted1/150ms7016 KiB
21Accepted2/248ms6964 KiB
22Accepted2/248ms6960 KiB
23Accepted2/248ms7024 KiB