제출 #161064

#제출 시각아이디문제언어결과실행 시간메모리
161064stoyan_malininHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
64 / 100
1623 ms262144 KiB
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int MAXN = 1e6 + 5;

int n;
int a[MAXN];

int findIndex(vector <int> &v, int x)
{
    if(v[0]>=x) return -1;

    int l = 0, r = v.size() - 1, mid;
    while(l+1<r)
    {
        mid = (l+r)/2;

        if(v[mid]<x) l = mid;
        else r = mid - 1;
    }

    if(v[r]<x) return v[r];
    return v[l];
}

struct Tree
{
    struct node
    {
        int maxCost = 0;
        vector <int> v;

        node()
        {
            this->maxCost = 0;
            this->v = vector <int>{};
        }
    };

    node tree[MAXN*4];

    int calcCost(int ind1, int ind2)
    {
        node *A = &this->tree[ind1];
        node *B = &this->tree[ind2];
        int ans = max(A->maxCost, B->maxCost);

        int x = findIndex(B->v, A->v.back());
        if(x!=-1) ans = max(ans, x + A->v.back());

        return ans;
    }

    void build(int ind, int l, int r)
    {
        if(l==r)
        {
            this->tree[ind].v = {a[l]};
            return;
        }

        this->build(ind*2, l, (l+r)/2);
        this->build(ind*2+1, (l+r)/2+1, r);
        this->tree[ind].maxCost = this->calcCost(2*ind, 2*ind+1);

        for(int x: this->tree[2*ind].v) this->tree[ind].v.push_back(x);
        for(int x: this->tree[2*ind+1].v) this->tree[ind].v.push_back(x);
        sort(this->tree[ind].v.begin(), this->tree[ind].v.end());
    }

    void query(int ind, int l, int r, int ql, int qr, vector <int> &output)
    {
        if(l>=ql && r<=qr)
        {
            output.push_back(ind);
            return;
        }
        if(r<ql || l>qr) return;

        this->query(2*ind, l, (l+r)/2, ql, qr, output);
        this->query(2*ind+1, (l+r)/2+1, r, ql, qr, output);
    }
};

Tree *T;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);

    int Q;

    cin >> n >> Q;
    for(int i = 0;i<n;i++)
    {
        cin >> a[i];
    }

    T = new Tree();
    T->build(1, 0, n-1);

    vector <int> v;
    while(Q--)
    {
        int l, r, x;
        cin >> l >> r >> x;
        l--;r--;

        int cost = 0;
        v.clear();T->query(1, 0, n-1, l, r, v);

        int maxElement = -1;
        for(int i = 0;i<v.size();i++)
        {
            cost = max(cost, T->tree[ v[i] ].maxCost);
            if(maxElement!=-1)
            {
                int curr = findIndex(T->tree[ v[i] ].v, maxElement);
                if(curr!=-1) cost = max(cost, curr + maxElement);
            }

            maxElement = max(maxElement, T->tree[ v[i] ].v.back());
        }

        if(cost<=x) cout << "1" << '\n';
        else cout << "0" << '\n';
    }
}
/*
5 2
3 5 1 8 2
1 3 6
2 5 3
*/

컴파일 시 표준 에러 (stderr) 메시지

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:117:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<v.size();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...