답안 #174696

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

#pragma GCC optimize("-g")
#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*, int> > 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;

pii d[4000001];
int o, lr, wr, rr;
int di[4000001];

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]);
    int 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
    {
        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;
        }
        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;
    pii o1=get(h*2,l,w,x,min(y,w));
    pii o2=get(h*2+1,w+1,r,max(x,w+1),y);
    pii 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
    {
        lr=0, rr=g.root.size()-1;
        while (lr<rr)
        {
            wr=(lr+rr+1)/2;
            if (g.root[wr].se<o1.fi) lr=wr; else rr=wr-1;
        }
        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:4:26: warning: bad option '-g' to pragma 'optimize' [-Wpragmas]
 #pragma GCC optimize("-g")
                          ^
sortbooks.cpp:37:10: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
     node():l(nullptr),r(nullptr),mx(0) {};
          ^
sortbooks.cpp:44:30: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
     void build_new_tree(int n)
                              ^
sortbooks.cpp:50:35: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
     void build(node *h,int l,int r)
                                   ^
sortbooks.cpp:61:49: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
     node* update(node *h,int l,int r,int x,int k)
                                                 ^
sortbooks.cpp:84:42: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
     ll gt(node *h,int l,int r,int x,int y)
                                          ^
sortbooks.cpp:99:29: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
 void build(int h,int l,int r)
                             ^
sortbooks.cpp:123:38: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
 pii get(int h,int l,int r,int x,int y)
                                      ^
sortbooks.cpp:147:10: warning: bad option '-g' to attribute 'optimize' [-Wattributes]
 int main()
          ^
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:154:11: warning: overflow in implicit constant conversion [-Woverflow]
     int p=1e18;
           ^~~~
sortbooks.cpp:159:12: warning: left operand of comma operator has no effect [-Wunused-value]
         l, r, k;
            ^
sortbooks.cpp:159:15: warning: right operand of comma operator has no effect [-Wunused-value]
         l, r, k;
               ^
sortbooks.cpp:159:16: warning: right operand of comma operator has no effect [-Wunused-value]
         l, r, k;
                ^
sortbooks.cpp:172:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for (int i = 0; i < v.size(); i++)
                         ~~^~~~~~~~~~
sortbooks.cpp:192: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 504 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 504 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 14 ms 3192 KB Output is correct
13 Correct 16 ms 3192 KB Output is correct
14 Correct 20 ms 3408 KB Output is correct
15 Correct 20 ms 3408 KB Output is correct
16 Correct 20 ms 3384 KB Output is correct
17 Correct 15 ms 2744 KB Output is correct
18 Correct 17 ms 3384 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 874 ms 50088 KB Output is correct
2 Correct 875 ms 45920 KB Output is correct
3 Correct 871 ms 46116 KB Output is correct
4 Correct 870 ms 46096 KB Output is correct
5 Correct 643 ms 38080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 925 ms 70564 KB Output is correct
2 Correct 1002 ms 70500 KB Output is correct
3 Correct 803 ms 70520 KB Output is correct
4 Correct 802 ms 70692 KB Output is correct
5 Correct 778 ms 70580 KB Output is correct
6 Correct 611 ms 70628 KB Output is correct
7 Correct 618 ms 70568 KB Output is correct
8 Correct 476 ms 70524 KB Output is correct
9 Correct 91 ms 3692 KB Output is correct
10 Correct 476 ms 70700 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 504 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 14 ms 3192 KB Output is correct
13 Correct 16 ms 3192 KB Output is correct
14 Correct 20 ms 3408 KB Output is correct
15 Correct 20 ms 3408 KB Output is correct
16 Correct 20 ms 3384 KB Output is correct
17 Correct 15 ms 2744 KB Output is correct
18 Correct 17 ms 3384 KB Output is correct
19 Correct 2210 ms 147004 KB Output is correct
20 Correct 2229 ms 146960 KB Output is correct
21 Correct 1927 ms 146904 KB Output is correct
22 Correct 1997 ms 146848 KB Output is correct
23 Correct 1896 ms 146964 KB Output is correct
24 Correct 1827 ms 146864 KB Output is correct
25 Correct 1793 ms 146996 KB Output is correct
26 Correct 2327 ms 147056 KB Output is correct
27 Correct 2220 ms 146960 KB Output is correct
28 Correct 2372 ms 147096 KB Output is correct
29 Correct 2282 ms 146988 KB Output is correct
30 Correct 2289 ms 147160 KB Output is correct
31 Correct 2316 ms 146964 KB Output is correct
32 Correct 2367 ms 146940 KB Output is correct
33 Correct 2352 ms 146988 KB Output is correct
34 Correct 1575 ms 147124 KB Output is correct
35 Correct 1538 ms 146924 KB Output is correct
36 Correct 1498 ms 146904 KB Output is correct
37 Correct 1499 ms 147032 KB Output is correct
38 Correct 1508 ms 147068 KB Output is correct
39 Correct 1911 ms 146904 KB Output is correct
40 Correct 1138 ms 96504 KB Output is correct
41 Correct 1080 ms 147008 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 504 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 14 ms 3192 KB Output is correct
13 Correct 16 ms 3192 KB Output is correct
14 Correct 20 ms 3408 KB Output is correct
15 Correct 20 ms 3408 KB Output is correct
16 Correct 20 ms 3384 KB Output is correct
17 Correct 15 ms 2744 KB Output is correct
18 Correct 17 ms 3384 KB Output is correct
19 Correct 874 ms 50088 KB Output is correct
20 Correct 875 ms 45920 KB Output is correct
21 Correct 871 ms 46116 KB Output is correct
22 Correct 870 ms 46096 KB Output is correct
23 Correct 643 ms 38080 KB Output is correct
24 Correct 925 ms 70564 KB Output is correct
25 Correct 1002 ms 70500 KB Output is correct
26 Correct 803 ms 70520 KB Output is correct
27 Correct 802 ms 70692 KB Output is correct
28 Correct 778 ms 70580 KB Output is correct
29 Correct 611 ms 70628 KB Output is correct
30 Correct 618 ms 70568 KB Output is correct
31 Correct 476 ms 70524 KB Output is correct
32 Correct 91 ms 3692 KB Output is correct
33 Correct 476 ms 70700 KB Output is correct
34 Correct 2210 ms 147004 KB Output is correct
35 Correct 2229 ms 146960 KB Output is correct
36 Correct 1927 ms 146904 KB Output is correct
37 Correct 1997 ms 146848 KB Output is correct
38 Correct 1896 ms 146964 KB Output is correct
39 Correct 1827 ms 146864 KB Output is correct
40 Correct 1793 ms 146996 KB Output is correct
41 Correct 2327 ms 147056 KB Output is correct
42 Correct 2220 ms 146960 KB Output is correct
43 Correct 2372 ms 147096 KB Output is correct
44 Correct 2282 ms 146988 KB Output is correct
45 Correct 2289 ms 147160 KB Output is correct
46 Correct 2316 ms 146964 KB Output is correct
47 Correct 2367 ms 146940 KB Output is correct
48 Correct 2352 ms 146988 KB Output is correct
49 Correct 1575 ms 147124 KB Output is correct
50 Correct 1538 ms 146924 KB Output is correct
51 Correct 1498 ms 146904 KB Output is correct
52 Correct 1499 ms 147032 KB Output is correct
53 Correct 1508 ms 147068 KB Output is correct
54 Correct 1911 ms 146904 KB Output is correct
55 Correct 1138 ms 96504 KB Output is correct
56 Correct 1080 ms 147008 KB Output is correct
57 Runtime error 1324 ms 262148 KB Execution killed with signal 9 (could be triggered by violating memory limits)
58 Halted 0 ms 0 KB -