CSES - HIIT Open 2024 - Results
Submission details
Task:Key cutting
Sender:TyƤmiesklubi
Submission time:2024-11-16 12:54:57 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.05 sdetails
#10ACCEPTED0.05 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.05 sdetails
#13ACCEPTED0.04 sdetails
#14ACCEPTED0.04 sdetails

Code

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    const int inf = 1e9 + 7;
    vector<int> a(n);
    for(int &i : a) cin >> i;
    for(int &i : a) i = inf - i;

    int N = 1;
    while(N < n) N *= 2;

    vector<vector<int> > p(2*N);
    vector<int> mx(2*N);
    for(int i=0; i<n; i++){
        mx[N+i] = a[i];
        p[N+i].push_back(i);
    }

    for(int i=N-1; i>0; i--){
        int l = i*2;
        int r = i*2+1;
        if(mx[l] == mx[r]){
            mx[i] = mx[r];
            p[i] = p[l];
            for(int j : p[r]) p[i].push_back(j);
        } else if(mx[l] < mx[r]){
            mx[i] = mx[r];
            p[i] = p[r];
        } else {
            mx[i] = mx[l];
            p[i] = p[l];
        }
    }

    auto gmax = [&](int l, int r){
        l += N; r += N;
        int m = 0;
        while(l <= r){
            if(l&1) m = max(m, mx[l++]);
            if(~r&1) m = max(m, mx[r--]);
            l /= 2;
            r /= 2;
        }
        return m;
    };

    auto gpos = [&](int l, int r, int m){
        vector<int> pos;
        l += N; r += N;
        while(l <= r){
            if(l&1){
                if(mx[l] == m) for(int j : p[l]) pos.push_back(j);
                l++;
            }
            if(~r&1){
                if(mx[r] == m) for(int j : p[r]) pos.push_back(j);
                r--;
            }
            l /= 2;
            r /= 2;
        }
        return pos;
    };
    
    int ans = 0;
    auto dnq = [&](auto &&dnq, int l, int r) -> void {
        ans++;
        int m = gmax(l, r);
        if(m == inf){
            ans--;
        }
        auto pos = gpos(l, r, m);
        sort(pos.begin(), pos.end());
        int pr = l;
        for(int j : pos){
            if(j-pr > 0){
                dnq(dnq, pr, j-1);
            }
            pr = j+1;
        }
        if(r-pr >= 0){
            dnq(dnq, pr, r);
        }
    };

    dnq(dnq, 0, n-1);

    cout << ans << '\n';
}

Test details

Test 1

Verdict: ACCEPTED

input
3
1 2 1

correct output
2

user output
2

Test 2

Verdict: ACCEPTED

input
1
0

correct output
0

user output
0

Test 3

Verdict: ACCEPTED

input
1
9

correct output
1

user output
1

Test 4

Verdict: ACCEPTED

input
100
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

correct output
0

user output
0

Test 5

Verdict: ACCEPTED

input
100
0 0 0 1 1 0 0 0 0 1 1 1 0 1 1 ...

correct output
25

user output
25

Test 6

Verdict: ACCEPTED

input
100
2 1 2 1 2 0 0 0 1 1 2 2 1 2 2 ...

correct output
41

user output
41

Test 7

Verdict: ACCEPTED

input
100
36 5 10 37 94 59 20 31 64 2 58...

correct output
99

user output
99

Test 8

Verdict: ACCEPTED

input
100
228768416 32415139 952687252 6...

correct output
100

user output
100

Test 9

Verdict: ACCEPTED

input
100000
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...

correct output
0

user output
0

Test 10

Verdict: ACCEPTED

input
100000
1 1 1 0 0 1 0 0 0 0 1 0 1 0 0 ...

correct output
24965

user output
24965

Test 11

Verdict: ACCEPTED

input
100000
2 1 2 2 2 2 2 1 1 0 1 1 0 1 1 ...

correct output
38968

user output
38968

Test 12

Verdict: ACCEPTED

input
100000
4 4 5 4 4 5 0 2 2 1 4 4 1 0 5 ...

correct output
59156

user output
59156

Test 13

Verdict: ACCEPTED

input
100000
18 5 6 16 8 10 1 7 4 15 5 9 19...

correct output
82598

user output
82598

Test 14

Verdict: ACCEPTED

input
100000
33 37 37 86 42 38 18 10 77 57 ...

correct output
94897

user output
94897