Submission #161064

# Submission time Handle Problem Language Result Execution time Memory
161064 2019-10-31T12:22:56 Z stoyan_malinin Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
64 / 100
1623 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 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
*/

Compilation message

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 time Memory Grader output
1 Correct 127 ms 125688 KB Output is correct
2 Correct 127 ms 125660 KB Output is correct
3 Correct 127 ms 125628 KB Output is correct
4 Correct 129 ms 125660 KB Output is correct
5 Correct 126 ms 125728 KB Output is correct
6 Correct 128 ms 125628 KB Output is correct
7 Correct 130 ms 125692 KB Output is correct
8 Correct 140 ms 125688 KB Output is correct
9 Correct 126 ms 125684 KB Output is correct
10 Correct 126 ms 125584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 125688 KB Output is correct
2 Correct 127 ms 125660 KB Output is correct
3 Correct 127 ms 125628 KB Output is correct
4 Correct 129 ms 125660 KB Output is correct
5 Correct 126 ms 125728 KB Output is correct
6 Correct 128 ms 125628 KB Output is correct
7 Correct 130 ms 125692 KB Output is correct
8 Correct 140 ms 125688 KB Output is correct
9 Correct 126 ms 125684 KB Output is correct
10 Correct 126 ms 125584 KB Output is correct
11 Correct 130 ms 125816 KB Output is correct
12 Correct 140 ms 126300 KB Output is correct
13 Correct 149 ms 126428 KB Output is correct
14 Correct 152 ms 126480 KB Output is correct
15 Correct 143 ms 126448 KB Output is correct
16 Correct 134 ms 126448 KB Output is correct
17 Correct 146 ms 126072 KB Output is correct
18 Correct 147 ms 126328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 1623 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 479 ms 141404 KB Output is correct
2 Correct 421 ms 141068 KB Output is correct
3 Correct 343 ms 141424 KB Output is correct
4 Correct 338 ms 141128 KB Output is correct
5 Correct 339 ms 141228 KB Output is correct
6 Correct 288 ms 141156 KB Output is correct
7 Correct 288 ms 141172 KB Output is correct
8 Correct 310 ms 141224 KB Output is correct
9 Correct 179 ms 127392 KB Output is correct
10 Correct 304 ms 141168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 125688 KB Output is correct
2 Correct 127 ms 125660 KB Output is correct
3 Correct 127 ms 125628 KB Output is correct
4 Correct 129 ms 125660 KB Output is correct
5 Correct 126 ms 125728 KB Output is correct
6 Correct 128 ms 125628 KB Output is correct
7 Correct 130 ms 125692 KB Output is correct
8 Correct 140 ms 125688 KB Output is correct
9 Correct 126 ms 125684 KB Output is correct
10 Correct 126 ms 125584 KB Output is correct
11 Correct 130 ms 125816 KB Output is correct
12 Correct 140 ms 126300 KB Output is correct
13 Correct 149 ms 126428 KB Output is correct
14 Correct 152 ms 126480 KB Output is correct
15 Correct 143 ms 126448 KB Output is correct
16 Correct 134 ms 126448 KB Output is correct
17 Correct 146 ms 126072 KB Output is correct
18 Correct 147 ms 126328 KB Output is correct
19 Correct 899 ms 155796 KB Output is correct
20 Correct 946 ms 155788 KB Output is correct
21 Correct 816 ms 155856 KB Output is correct
22 Correct 863 ms 155828 KB Output is correct
23 Correct 821 ms 155796 KB Output is correct
24 Correct 494 ms 155768 KB Output is correct
25 Correct 491 ms 155884 KB Output is correct
26 Correct 562 ms 155696 KB Output is correct
27 Correct 588 ms 155864 KB Output is correct
28 Correct 599 ms 155884 KB Output is correct
29 Correct 586 ms 155756 KB Output is correct
30 Correct 582 ms 155760 KB Output is correct
31 Correct 607 ms 155756 KB Output is correct
32 Correct 612 ms 155884 KB Output is correct
33 Correct 615 ms 155768 KB Output is correct
34 Correct 496 ms 155884 KB Output is correct
35 Correct 497 ms 155752 KB Output is correct
36 Correct 481 ms 155840 KB Output is correct
37 Correct 481 ms 155708 KB Output is correct
38 Correct 479 ms 155808 KB Output is correct
39 Correct 587 ms 155756 KB Output is correct
40 Correct 401 ms 143888 KB Output is correct
41 Correct 554 ms 155912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 127 ms 125688 KB Output is correct
2 Correct 127 ms 125660 KB Output is correct
3 Correct 127 ms 125628 KB Output is correct
4 Correct 129 ms 125660 KB Output is correct
5 Correct 126 ms 125728 KB Output is correct
6 Correct 128 ms 125628 KB Output is correct
7 Correct 130 ms 125692 KB Output is correct
8 Correct 140 ms 125688 KB Output is correct
9 Correct 126 ms 125684 KB Output is correct
10 Correct 126 ms 125584 KB Output is correct
11 Correct 130 ms 125816 KB Output is correct
12 Correct 140 ms 126300 KB Output is correct
13 Correct 149 ms 126428 KB Output is correct
14 Correct 152 ms 126480 KB Output is correct
15 Correct 143 ms 126448 KB Output is correct
16 Correct 134 ms 126448 KB Output is correct
17 Correct 146 ms 126072 KB Output is correct
18 Correct 147 ms 126328 KB Output is correct
19 Runtime error 1623 ms 262144 KB Execution killed with signal 9 (could be triggered by violating memory limits)
20 Halted 0 ms 0 KB -