Submission #173205

#TimeUsernameProblemLanguageResultExecution timeMemory
173205srvltHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
100 / 100
1068 ms61616 KiB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;

const int N = 1e6 + 7;
int n, m, a[N], last[N], bad[N];

struct query {
    int l, r, k, ind;
    bool operator < (const query & q) const { return r < q.r; }
} v[N];

set <pair <int, int> > cont;

inline void update(int x, int y) {
    auto p = cont.insert({x, y}).first;
    if (p != (--cont.end()) && (*next(p)).second > y) {
        cont.erase(p);
        return;
    }
    while (p != cont.begin() && (*prev(p)).second < y) cont.erase(prev(p));
}

inline int query(int x) {
    if (cont.empty()) return 0;
    auto p = cont.lower_bound({x, 0});
    if (p == cont.end()) return 0;
    return (*p).second;
}

int main() {
    scanf("%d %d", & n, & m);
    stack <int> s;
    for (int i = 1; i <= n; i++) {
        scanf("%d", & a[i]);
        while (!s.empty() && a[s.top()] <= a[i]) s.pop();
        if (!s.empty()) last[i] = s.top();
        s.push(i);
    }
    for (int i = 0; i < m; i++) scanf("%d %d %d", & v[i].l, & v[i].r, & v[i].k), v[i].ind = i;
    sort(v, v + m);
    int j = 1;
    for (int i = 0; i < m; i++) {
        while (j <= v[i].r) {
            if (j) update(last[j], a[j] + a[last[j]]);
            j++;
        }
        bad[v[i].ind] = (query(v[i].l) > v[i].k);
    }
    for (int i = 0; i < m; i++) cout << (bad[i] ? "0" : "1") << "\n";
}

Compilation message (stderr)

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", & n, & m);
     ~~~~~^~~~~~~~~~~~~~~~~~~
sortbooks.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", & a[i]);
         ~~~~~^~~~~~~~~~~~~~
sortbooks.cpp:40:80: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for (int i = 0; i < m; i++) scanf("%d %d %d", & v[i].l, & v[i].r, & v[i].k), v[i].ind = i;
                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...