Submission #161063

# Submission time Handle Problem Language Result Execution time Memory
161063 2019-10-31T12:03:56 Z stoyan_malinin Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
64 / 100
1601 ms 262144 KB
#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 node
{
    int maxCost = 0;
    vector <int> v;

    node *L, *R;

    node(){}
    int calcCost(node *A, node *B)
    {
        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 l, int r)
    {
        if(l==r)
        {
            this->v = vector <int>{a[l]};
            return;
        }

        this->L = new node();
        this->R = new node();

        this->L->build(l, (l+r)/2);
        this->R->build((l+r)/2+1, r);
        this->maxCost = this->calcCost(this->L, this->R);

        for(int x: this->L->v) this->v.push_back(x);
        for(int x: this->R->v) this->v.push_back(x);
        sort(this->v.begin(), this->v.end());
    }

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

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

node *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 node();
    T->build(0, n-1);

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

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

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

            maxElement = max(maxElement, 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
*/

Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:110:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<v.size();i++)
                       ~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 0 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 0 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 508 KB Output is correct
11 Correct 6 ms 760 KB Output is correct
12 Correct 10 ms 1784 KB Output is correct
13 Correct 10 ms 1784 KB Output is correct
14 Correct 13 ms 1784 KB Output is correct
15 Correct 13 ms 1784 KB Output is correct
16 Correct 10 ms 1784 KB Output is correct
17 Correct 8 ms 1400 KB Output is correct
18 Correct 9 ms 1656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1601 ms 262144 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 396 ms 28168 KB Output is correct
2 Correct 334 ms 28764 KB Output is correct
3 Correct 267 ms 28784 KB Output is correct
4 Correct 260 ms 28912 KB Output is correct
5 Correct 268 ms 28784 KB Output is correct
6 Correct 166 ms 28528 KB Output is correct
7 Correct 166 ms 28656 KB Output is correct
8 Correct 236 ms 28448 KB Output is correct
9 Correct 53 ms 2040 KB Output is correct
10 Correct 229 ms 28348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 0 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 508 KB Output is correct
11 Correct 6 ms 760 KB Output is correct
12 Correct 10 ms 1784 KB Output is correct
13 Correct 10 ms 1784 KB Output is correct
14 Correct 13 ms 1784 KB Output is correct
15 Correct 13 ms 1784 KB Output is correct
16 Correct 10 ms 1784 KB Output is correct
17 Correct 8 ms 1400 KB Output is correct
18 Correct 9 ms 1656 KB Output is correct
19 Correct 965 ms 60736 KB Output is correct
20 Correct 946 ms 60652 KB Output is correct
21 Correct 758 ms 60720 KB Output is correct
22 Correct 762 ms 60492 KB Output is correct
23 Correct 769 ms 60524 KB Output is correct
24 Correct 392 ms 60268 KB Output is correct
25 Correct 404 ms 60440 KB Output is correct
26 Correct 563 ms 60524 KB Output is correct
27 Correct 548 ms 60396 KB Output is correct
28 Correct 610 ms 60576 KB Output is correct
29 Correct 609 ms 60780 KB Output is correct
30 Correct 609 ms 60600 KB Output is correct
31 Correct 608 ms 60652 KB Output is correct
32 Correct 623 ms 60772 KB Output is correct
33 Correct 606 ms 60524 KB Output is correct
34 Correct 359 ms 60144 KB Output is correct
35 Correct 359 ms 60268 KB Output is correct
36 Correct 356 ms 60120 KB Output is correct
37 Correct 358 ms 60012 KB Output is correct
38 Correct 359 ms 60264 KB Output is correct
39 Correct 600 ms 59264 KB Output is correct
40 Correct 347 ms 37956 KB Output is correct
41 Correct 541 ms 58800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 0 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 504 KB Output is correct
8 Correct 3 ms 504 KB Output is correct
9 Correct 3 ms 376 KB Output is correct
10 Correct 3 ms 508 KB Output is correct
11 Correct 6 ms 760 KB Output is correct
12 Correct 10 ms 1784 KB Output is correct
13 Correct 10 ms 1784 KB Output is correct
14 Correct 13 ms 1784 KB Output is correct
15 Correct 13 ms 1784 KB Output is correct
16 Correct 10 ms 1784 KB Output is correct
17 Correct 8 ms 1400 KB Output is correct
18 Correct 9 ms 1656 KB Output is correct
19 Runtime error 1601 ms 262144 KB Execution killed with signal 9 (could be triggered by violating memory limits)
20 Halted 0 ms 0 KB -