Submission #1280141

#TimeUsernameProblemLanguageResultExecution timeMemory
1280141quangminh412Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1135 ms82900 KiB
/*
  Ben Watson

  Quang Minh MasterDDDDD
*/

#include <bits/stdc++.h>
using namespace std;

#define ll long long

const string name = "test";

void solve();
signed main()
{
    if (fopen((name + ".inp").c_str(), "r"))
    {
        freopen((name + ".inp").c_str(), "r", stdin);
        freopen((name + ".out").c_str(), "w", stdout);
    }
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    solve();

    return 0;
}

// main program
const int maxn = 1e6 + 1;

struct SegmentTree
{
    vector<int> st;
    int n;

    SegmentTree(int n) : n(n) { st.resize(4 * n + 1, 0); }

    void update(int head, int l, int r, int pos, int val)
    {
        if (pos < l || r < pos) return;
        if (l == r)
        {
            st[head] = max(st[head], val);
            return;
        }
        int mid = l + r >> 1;
        if (pos <= mid)
            update(head << 1, l, mid, pos, val);
        else
            update(head << 1 | 1, mid + 1, r, pos, val);
        st[head] = max(st[head << 1], st[head << 1 | 1]);
    }
    void update(int pos, int val) { update(1, 1, n, pos, val); }

    int query(int head, int l, int r, int u, int v)
    {
        if (l > v || r < u) return 0;
        if (u <= l && r <= v) return st[head];
        int mid = l + r >> 1;
        return max(query(head << 1, l, mid, u, v), query(head << 1 | 1, mid + 1, r, u, v));
    }
    int query(int u, int v) { return query(1, 1, n, u, v); }
};

vector<int> queries[maxn];
int L[maxn], R[maxn], K[maxn];
int w[maxn], pos[maxn];
int n, m;

void solve()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> w[i];
    for (int i = 1; i <= m; i++)
    {
        cin >> L[i] >> R[i] >> K[i];
        queries[R[i]].push_back(i);
    }

    stack<int> stk;
    for (int i = 1; i <= n; i++)
    {
        while (!stk.empty() && w[stk.top()] <= w[i])
            stk.pop();
        if (!stk.empty())
            pos[i] = stk.top();
        else
            pos[i] = -1;
        stk.push(i);
    }

    SegmentTree st(n);
    vector<int> ans(m + 1, 0);
    for (int r = 1; r <= n; r++)
    {
        if (pos[r] != -1)
            st.update(pos[r], w[pos[r]] + w[r]);
        for (int idx : queries[r])
        {
            int l = L[idx], k = K[idx];
            if (st.query(l, r) <= k)
                ans[idx] = 1;
            else
                ans[idx] = 0;
        }
    }

    for (int i = 1; i <= m; i++)
        cout << ans[i] << '\n';
}

Compilation message (stderr)

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   19 |         freopen((name + ".inp").c_str(), "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen((name + ".out").c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#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...