Submission #1172287

#TimeUsernameProblemLanguageResultExecution timeMemory
1172287AtabayRajabliHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
100 / 100
2452 ms238112 KiB
#include <bits/stdc++.h>
// #define int long long
#define all(v) v.begin(), v.end()
using namespace std;

const int sz = 1e6 + 1, inf = 1e18;
int n, m, a[sz], inv[sz << 2];
vector<int> v[sz << 2];

void join(int ind)
{
    int i = 0, j = 0;
    inv[ind] = max(inv[ind << 1], inv[ind << 1 | 1]);
    while(i < v[ind << 1].size() && j < v[ind << 1 | 1].size())
    {
        if(v[ind << 1][i] <= v[ind << 1 | 1][j]) 
        {
            v[ind].push_back(v[ind << 1][i++]);
        }
        else
        {
            v[ind].push_back(v[ind << 1 | 1][j++]);
        }
    }
    while(i < v[ind << 1].size()) 
    {
        v[ind].push_back(v[ind << 1][i++]);
    }
    while(j < v[ind << 1 | 1].size()) 
    {
        v[ind].push_back(v[ind << 1 | 1][j++]);
    }
    auto lb = lower_bound(all(v[ind << 1 | 1]), v[ind << 1].back());
    if(lb != v[ind << 1 | 1].begin()) 
    {
        inv[ind] = max(inv[ind], v[ind << 1].back() + *--lb);
    }
}

void build(int ind, int l, int r)
{
    if(l == r)
    {
        v[ind].push_back(a[l]);
        return;
    }
    int mid = (l + r) >> 1;
    build(ind << 1, l, mid), build(ind << 1 | 1, mid + 1, r);
    join(ind);
}

void get(int ind, int l, int r, int lx, int rx, vector<int> &inds, int &mx)
{
    if(l > rx || r < lx) return;
    if(lx <= l && r <= rx)
    {
        mx = max(mx, inv[ind]);
        inds.push_back(ind);
        return;
    }
    int mid = (l + r) >> 1;
    get(ind << 1, l, mid, lx, rx, inds, mx);
    get(ind << 1 | 1, mid + 1, r, lx, rx, inds, mx);
}

signed main()
{                                                                  
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    build(1, 1, n);
    while(m--)
    {
        int l, r, k, mx = -1, ans = -1;
        cin >> l >> r >> k;
        vector<int> inds;
        get(1, 1, n, l, r, inds, ans);
        mx = v[inds[0]].back();
        for(int i = 1; i < inds.size(); i++)
        {
            auto lb = lower_bound(all(v[inds[i]]), mx);
            if(lb != v[inds[i]].begin()) ans = max(ans, mx + *--lb);
            mx = max(mx, v[inds[i]].back());
        }
        cout << (ans <= k) << '\n';
    }
}

Compilation message (stderr)

sortbooks.cpp:6:31: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
    6 | const int sz = 1e6 + 1, inf = 1e18;
      |                               ^~~~
#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...