답안 #161065

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
161065 2019-10-31T12:39:17 Z stoyan_malinin Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
64 / 100
2252 ms 262148 KB
#include<iostream>
#include<random>
#include<vector>
#include<algorithm>

using namespace std;

const int MAXN = 1e6 + 5;

mt19937 rnd(69420);

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;
    vector <int> v;

    int l, r;
    node *L, *R;

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

        this->L = nullptr;
        this->R = nullptr;
    }

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

        this->L = new node(this->l, (this->l+this->r)/2);
        this->R = new node((this->l+this->r)/2+1, this->r);

        this->L->build();
        this->R->build();
        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 ql, int qr, vector <node*> &output)
    {
        if(this->l>=ql && this->r<=qr)
        {
            output.push_back(this);
            return;
        }
        if(this->r<ql || this->l>qr) return;

        this->L->query(ql, qr, output);
        this->R->query(ql, qr, output);
    }
};

node *T;

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

    int Q;

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

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

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

        int cost = 0;
        v.clear();T->query(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:128:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<v.size();i++)
                       ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 4 ms 380 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 4 ms 380 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 19 ms 804 KB Output is correct
12 Correct 23 ms 1656 KB Output is correct
13 Correct 25 ms 1656 KB Output is correct
14 Correct 37 ms 1788 KB Output is correct
15 Correct 37 ms 1784 KB Output is correct
16 Correct 36 ms 1668 KB Output is correct
17 Correct 28 ms 1452 KB Output is correct
18 Correct 29 ms 1696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 2252 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 849 ms 27544 KB Output is correct
2 Correct 798 ms 27664 KB Output is correct
3 Correct 763 ms 27572 KB Output is correct
4 Correct 726 ms 27504 KB Output is correct
5 Correct 750 ms 27636 KB Output is correct
6 Correct 717 ms 27480 KB Output is correct
7 Correct 589 ms 27528 KB Output is correct
8 Correct 696 ms 27608 KB Output is correct
9 Correct 432 ms 1144 KB Output is correct
10 Correct 665 ms 27356 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 4 ms 380 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 19 ms 804 KB Output is correct
12 Correct 23 ms 1656 KB Output is correct
13 Correct 25 ms 1656 KB Output is correct
14 Correct 37 ms 1788 KB Output is correct
15 Correct 37 ms 1784 KB Output is correct
16 Correct 36 ms 1668 KB Output is correct
17 Correct 28 ms 1452 KB Output is correct
18 Correct 29 ms 1696 KB Output is correct
19 Correct 2059 ms 54884 KB Output is correct
20 Correct 2025 ms 54816 KB Output is correct
21 Correct 1798 ms 54904 KB Output is correct
22 Correct 1833 ms 55020 KB Output is correct
23 Correct 1795 ms 54796 KB Output is correct
24 Correct 1428 ms 54864 KB Output is correct
25 Correct 1455 ms 54892 KB Output is correct
26 Correct 1638 ms 55020 KB Output is correct
27 Correct 1673 ms 54996 KB Output is correct
28 Correct 1748 ms 54904 KB Output is correct
29 Correct 1698 ms 54992 KB Output is correct
30 Correct 1753 ms 54872 KB Output is correct
31 Correct 1688 ms 54820 KB Output is correct
32 Correct 1715 ms 54728 KB Output is correct
33 Correct 1698 ms 54824 KB Output is correct
34 Correct 1388 ms 54892 KB Output is correct
35 Correct 1325 ms 54964 KB Output is correct
36 Correct 1352 ms 55088 KB Output is correct
37 Correct 1340 ms 54828 KB Output is correct
38 Correct 1371 ms 54892 KB Output is correct
39 Correct 1571 ms 54784 KB Output is correct
40 Correct 1255 ms 34364 KB Output is correct
41 Correct 1492 ms 56384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 4 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 3 ms 376 KB Output is correct
6 Correct 5 ms 504 KB Output is correct
7 Correct 5 ms 504 KB Output is correct
8 Correct 5 ms 504 KB Output is correct
9 Correct 4 ms 380 KB Output is correct
10 Correct 4 ms 376 KB Output is correct
11 Correct 19 ms 804 KB Output is correct
12 Correct 23 ms 1656 KB Output is correct
13 Correct 25 ms 1656 KB Output is correct
14 Correct 37 ms 1788 KB Output is correct
15 Correct 37 ms 1784 KB Output is correct
16 Correct 36 ms 1668 KB Output is correct
17 Correct 28 ms 1452 KB Output is correct
18 Correct 29 ms 1696 KB Output is correct
19 Runtime error 2252 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
20 Halted 0 ms 0 KB -