답안 #174695

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
174695 2020-01-06T17:59:39 Z _Enkognit Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
77 / 100
2459 ms 262148 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

#pragma GCC optimize("-Og")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#define ll long long
#define mp make_pair
#define pb push_back
#define ld long double
#define fi first
#define se second
#define pld pair<ld,ld>
#define pll pair<ll,ll>
#define sqr(a) ((a)*(a))
#define pii pair<int,int>
#define y1 Enkognit
#define sqr(a) ((a)*(a))

using namespace std;

mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());

const ll md1=1e9+7, md2=998244357, md3=rnd()%(ll)(1e8), p1=31, p2=37, p3=41;

//template <typename T>
//using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

int n, m, k, l, r, i, j, b[1000001], ttt=1;
pii a[1000001];

struct node
{
    node *l, *r;
    int mx;
    node():l(nullptr),r(nullptr),mx(0) {};
};

struct bin_persistent_tree
{
    vector<pair<node*, ll> > root;

    void build_new_tree(int n)
    {
        root.pb(mp(new node(),-1e9));
        build(root.back().fi,1,n);
    }

    void build(node *h,int l,int r)
    {
        if (l==r) {h->mx=-1e9;return;}
        int w=(l+r)/2;
        h->l=new node();
        h->r=new node();
        build(h->l,l,w);
        build(h->r,w+1,r);
        h->mx=-1e9;
    }

    node* update(node *h,int l,int r,int x,int k)
    {
        if (l==r)
        {
            node* u=new node();
            u->mx=k;
            return u;
        }
        int w=(l+r)/2;
        node *u=new node();
        if (x<=w)
        {
            u->r=h->r;
            u->l=update(h->l,l,w,x,k);
        }else
        {
            u->l=h->l;
            u->r=update(h->r,w+1,r,x,k);
        }
        u->mx=max(u->l->mx,u->r->mx);
        return u;
    }

    ll gt(node *h,int l,int r,int x,int y)
    {
        if (x>y) return -1e9;
        if (l==x && y==r) return h->mx;
        int w=(l+r)/2;
        return max(gt(h->l,l,w,x,min(y,w)), gt(h->r,w+1,r,max(x,w+1),y));
    }
};

bin_persistent_tree g;

pll d[5000001];
ll di[5000001];

void build(int h,int l,int r)
{
    if (l==r) {d[h]=mp(b[l],0);di[h]=b[l];return;}
    int w=(l+r)/2;
    build(h*2,l,w);
    build(h*2+1,w+1,r);
    d[h].fi=max(d[h*2].fi,d[h*2+1].fi);
    di[h]=min(di[h*2],di[h*2+1]);
    ll p=max(d[h*2].se,d[h*2+1].se);
    if (d[h*2].fi>d[h*2+1].fi) d[h].se=max(p,d[h*2].fi+d[h*2+1].fi); else
    {
        ll lr=0, rr=g.root.size()-1;
        while (lr<rr)
        {
            int wr=(lr+rr+1)/2;
            if (g.root[wr].se<d[h*2].fi) lr=wr; else rr=wr-1;
        }
        ll o=g.gt(g.root[lr].fi,1,n,w+1,r);
        //cout << d[h*2].fi << " " << o << " " << w+1 << " " << r << "\n";
        d[h].se=max(p, d[h*2].fi+o);
    }
    //cout << l << " " << r << " : " << d[h].se << "\n";
}

pii get(int h,int l,int r,int x,int y)
{
    if (x>y) return mp(0,-1e9);
    if (l==x && y==r) return d[h];
    int w=(l+r)/2;
    pll o1=get(h*2,l,w,x,min(y,w));
    pll o2=get(h*2+1,w+1,r,max(x,w+1),y);
    pll o3=mp(max(o1.fi,o2.fi), max(o1.se,o2.se));
    if (!(x<=w && y>=w+1) || o1.se<0 || o2.se<0) return o3;
    if (o1.fi>o2.fi) o3.se=max(o3.se, o1.fi+o2.fi);else
    {
        ll lr=0, rr=g.root.size()-1;
        while (lr<rr)
        {
            int wr=(lr+rr+1)/2;
            if (g.root[wr].se<o1.fi) lr=wr; else rr=wr-1;
        }
        ll o=g.gt(g.root[lr].fi,1,n,w+1,y);
        o3.se=max(o3.se, o1.fi+o);
        //if (o>=o1.fi) cout << x << "-" << y << " " << o1.fi << " " << g.root[lr].se << "\n";
    }
    return o3;
}

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    int p=1e18;
    for (int i = 1; i <= n; i++) {cin >> b[i];a[i].fi=b[i];a[i].se=i;p=min(p,b[i]);}
    vector<pair<pll,ll> > v;
    while (m--)
    {
        l, r, k;
        cin >> l >> r >> k;
        if (k>=p) ttt=0;
        v.pb(mp(mp(l,r),k));
    }
    if (ttt)
    {
        vector <ll> p;
        p.clear();
        p.pb(0);
        for (int i = 1; i < n; i++)
            if (b[i]>b[i+1]) p.pb(i);
        p.pb(n);
        for (int i = 0; i < v.size(); i++)
        {
            ll l=0, r=p.size()-1;
            while (l<r)
            {
                ll w=(l+r)/2;
                if (v[i].fi.fi<=p[w]) r=w; else l=w+1;
            }
            cout << (p[l]>=v[i].fi.se) << "\n";
        }
        exit(0);
    }
    sort(a+1,a+n+1);
    g.build_new_tree(n);
    for (int i = 1; i <= n; i++)
    {
        g.root.pb(mp(g.update(g.root.back().fi,1,n,a[i].se,a[i].fi), a[i].fi));
    }
    //cout << g.gt(g.root[2].fi,1,n,1,n);
    build(1,1,n);
    for (int i = 0; i < v.size(); i++)
    {
        cout << (v[i].se>=get(1,1,n,v[i].fi.fi,v[i].fi.se).se) << "\n";
    }
}
/*
5 2
3 5 1 8 2
1 3 6
2 5 3

6 4
4 6 8 5 7 9
1 3 3
2 5 2
1 6 1
4 6 2
*/

Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:153:11: warning: overflow in implicit constant conversion [-Woverflow]
     int p=1e18;
           ^~~~
sortbooks.cpp:158:12: warning: left operand of comma operator has no effect [-Wunused-value]
         l, r, k;
            ^
sortbooks.cpp:158:15: warning: right operand of comma operator has no effect [-Wunused-value]
         l, r, k;
               ^
sortbooks.cpp:158:16: warning: right operand of comma operator has no effect [-Wunused-value]
         l, r, k;
                ^
sortbooks.cpp:171:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < v.size(); i++)
                         ~~^~~~~~~~~~
sortbooks.cpp:191:23: 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 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 632 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 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 8 ms 1272 KB Output is correct
12 Correct 15 ms 3360 KB Output is correct
13 Correct 19 ms 3448 KB Output is correct
14 Correct 22 ms 3512 KB Output is correct
15 Correct 28 ms 3616 KB Output is correct
16 Correct 22 ms 3512 KB Output is correct
17 Correct 15 ms 2872 KB Output is correct
18 Correct 17 ms 3640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 861 ms 47996 KB Output is correct
2 Correct 921 ms 78716 KB Output is correct
3 Correct 880 ms 78512 KB Output is correct
4 Correct 878 ms 78636 KB Output is correct
5 Correct 635 ms 70572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 893 ms 73692 KB Output is correct
2 Correct 866 ms 73624 KB Output is correct
3 Correct 822 ms 73584 KB Output is correct
4 Correct 852 ms 73712 KB Output is correct
5 Correct 795 ms 73772 KB Output is correct
6 Correct 622 ms 73636 KB Output is correct
7 Correct 626 ms 73700 KB Output is correct
8 Correct 490 ms 73744 KB Output is correct
9 Correct 91 ms 3684 KB Output is correct
10 Correct 503 ms 73568 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 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 8 ms 1272 KB Output is correct
12 Correct 15 ms 3360 KB Output is correct
13 Correct 19 ms 3448 KB Output is correct
14 Correct 22 ms 3512 KB Output is correct
15 Correct 28 ms 3616 KB Output is correct
16 Correct 22 ms 3512 KB Output is correct
17 Correct 15 ms 2872 KB Output is correct
18 Correct 17 ms 3640 KB Output is correct
19 Correct 2292 ms 153212 KB Output is correct
20 Correct 2320 ms 153216 KB Output is correct
21 Correct 2074 ms 153176 KB Output is correct
22 Correct 2041 ms 153204 KB Output is correct
23 Correct 2019 ms 153148 KB Output is correct
24 Correct 1808 ms 153180 KB Output is correct
25 Correct 1834 ms 153280 KB Output is correct
26 Correct 2306 ms 153108 KB Output is correct
27 Correct 2265 ms 153296 KB Output is correct
28 Correct 2421 ms 153176 KB Output is correct
29 Correct 2341 ms 153172 KB Output is correct
30 Correct 2396 ms 153192 KB Output is correct
31 Correct 2378 ms 153120 KB Output is correct
32 Correct 2412 ms 153104 KB Output is correct
33 Correct 2459 ms 153028 KB Output is correct
34 Correct 1511 ms 153152 KB Output is correct
35 Correct 1507 ms 153176 KB Output is correct
36 Correct 1524 ms 153068 KB Output is correct
37 Correct 1501 ms 153168 KB Output is correct
38 Correct 1518 ms 153248 KB Output is correct
39 Correct 1963 ms 153140 KB Output is correct
40 Correct 1160 ms 99664 KB Output is correct
41 Correct 1144 ms 153304 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 2 ms 504 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 3 ms 632 KB Output is correct
7 Correct 3 ms 632 KB Output is correct
8 Correct 3 ms 632 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 632 KB Output is correct
11 Correct 8 ms 1272 KB Output is correct
12 Correct 15 ms 3360 KB Output is correct
13 Correct 19 ms 3448 KB Output is correct
14 Correct 22 ms 3512 KB Output is correct
15 Correct 28 ms 3616 KB Output is correct
16 Correct 22 ms 3512 KB Output is correct
17 Correct 15 ms 2872 KB Output is correct
18 Correct 17 ms 3640 KB Output is correct
19 Correct 861 ms 47996 KB Output is correct
20 Correct 921 ms 78716 KB Output is correct
21 Correct 880 ms 78512 KB Output is correct
22 Correct 878 ms 78636 KB Output is correct
23 Correct 635 ms 70572 KB Output is correct
24 Correct 893 ms 73692 KB Output is correct
25 Correct 866 ms 73624 KB Output is correct
26 Correct 822 ms 73584 KB Output is correct
27 Correct 852 ms 73712 KB Output is correct
28 Correct 795 ms 73772 KB Output is correct
29 Correct 622 ms 73636 KB Output is correct
30 Correct 626 ms 73700 KB Output is correct
31 Correct 490 ms 73744 KB Output is correct
32 Correct 91 ms 3684 KB Output is correct
33 Correct 503 ms 73568 KB Output is correct
34 Correct 2292 ms 153212 KB Output is correct
35 Correct 2320 ms 153216 KB Output is correct
36 Correct 2074 ms 153176 KB Output is correct
37 Correct 2041 ms 153204 KB Output is correct
38 Correct 2019 ms 153148 KB Output is correct
39 Correct 1808 ms 153180 KB Output is correct
40 Correct 1834 ms 153280 KB Output is correct
41 Correct 2306 ms 153108 KB Output is correct
42 Correct 2265 ms 153296 KB Output is correct
43 Correct 2421 ms 153176 KB Output is correct
44 Correct 2341 ms 153172 KB Output is correct
45 Correct 2396 ms 153192 KB Output is correct
46 Correct 2378 ms 153120 KB Output is correct
47 Correct 2412 ms 153104 KB Output is correct
48 Correct 2459 ms 153028 KB Output is correct
49 Correct 1511 ms 153152 KB Output is correct
50 Correct 1507 ms 153176 KB Output is correct
51 Correct 1524 ms 153068 KB Output is correct
52 Correct 1501 ms 153168 KB Output is correct
53 Correct 1518 ms 153248 KB Output is correct
54 Correct 1963 ms 153140 KB Output is correct
55 Correct 1160 ms 99664 KB Output is correct
56 Correct 1144 ms 153304 KB Output is correct
57 Runtime error 1459 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
58 Halted 0 ms 0 KB -