답안 #114609

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
114609 2019-06-02T04:43:10 Z FutymyClone Money (IZhO17_money) C++14
0 / 100
26 ms 31616 KB
#include <bits/stdc++.h>

using namespace std;

const int N = 1e6 + 5;

int n, a[N], nxt[N];
stack <int> st;

struct SegmentTree {
    int node[N << 2], lazy[N << 2];
    void init(){
        memset(node, 0, sizeof(node));
        memset(lazy, 0, sizeof(lazy));
    }

    void dolazy (int i, int l, int r) {
        if (lazy[i]) {
            node[i] += lazy[i] * (r - l + 1);
            node[i] = min(node[i], r - l + 1);
            if (l != r) {
                lazy[i << 1] += lazy[i];
                lazy[i << 1 | 1] += lazy[i];
            }

            lazy[i] = 0;
        }
    }

    void update (int i, int l, int r, int a, int b) {
        dolazy(i, l, r);
        if (l > r || a > r || b < l) return;
        if (a <= l && r <= b) {
            node[i] += (r - l + 1); node[i] = min(node[i], r - l + 1);
            if (l != r) {
                lazy[i << 1]++;
                lazy[i << 1 | 1]++;
            }

            return;
        }

        int mid = (l + r) >> 1;
        update(i << 1, l, mid, a, b);
        update(i << 1 | 1, mid + 1, r, a, b);
        node[i] = node[i << 1] + node[i << 1 | 1];
    }

    int query (int i, int l, int r, int a, int b) {
        if (l > r || a > r || b < l) return 0;
        dolazy(i, l, r);
        if (a <= l && r <= b) return node[i];
        int mid = (l + r) >> 1;
        return query(i << 1, l, mid, a, b) + query(i << 1 | 1, mid + 1, r, a, b);
    }
} it;

int main(){
    scanf("%d", &n); it.init();
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= n; i++) {
        if (st.empty() || a[st.top()] <= a[i]) st.push(i);
        else {
            while (!st.empty()) nxt[st.top()] = i, st.pop();
            st.push(i);
        }
    }

    while (!st.empty()) nxt[st.top()] = n + 1, st.pop();

    int cur = 1, ans = 0;
    while (cur <= n) {
        int l = cur, r = nxt[cur] - 1;
        while (l <= r) {
            int mid = (l + r) / 2;
            int res = it.query(1, 1, N, a[cur], a[mid]);

            if (res == 0 || res == a[mid] - a[cur] + 1) l = mid + 1;
            else r = mid - 1;
        }

        ans++;
        it.update(1, 1, N, a[cur], a[r]);
        cur = l;
    }

    printf("%d", ans);
    return 0;
}

Compilation message

money.cpp: In function 'int main()':
money.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n); it.init();
     ~~~~~^~~~~~~~~~
money.cpp:60:39: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
                                  ~~~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 31616 KB Output is correct
2 Incorrect 26 ms 31616 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 31616 KB Output is correct
2 Incorrect 26 ms 31616 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 31616 KB Output is correct
2 Incorrect 26 ms 31616 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 31616 KB Output is correct
2 Incorrect 26 ms 31616 KB Output isn't correct
3 Halted 0 ms 0 KB -