답안 #1030329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1030329 2024-07-22T02:43:46 Z hotboy2703 식물 비교 (IOI20_plants) C++17
100 / 100
1078 ms 161956 KB
#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define pll pair <ll,ll>
#define fi first
#define se second
#define MP make_pair
#define sz(a) (ll((a).size()))
#define BIT(mask,i) (((mask) >> (i))&1)
#define MASK(i) (1LL<<(i))
namespace trung{
    const ll MAXN = 2e5+100;
    const ll INF = 1e9+7;
    ll n,k;
    vector <int> val;
    namespace seg{
        pll tree[MAXN*4];
        ll lazy[MAXN*4];
        pll combine(pll x,pll y){
            if (x.fi > y.fi)return y;
            else return x;
        }
        void build(ll id=1,ll l=0,ll r=n-1){
            if (l==r){tree[id] = MP(val[l],l);}
            else{
                ll mid = (l + r) >> 1;
                build(id<<1,l,mid);
                build(id<<1|1,mid+1,r);
                tree[id] = combine(tree[id<<1],tree[id<<1|1]);
            }
        }
        void push(ll id,ll x){
            tree[id].fi+=x;
            lazy[id]+=x;
        }
        void lz(ll id){
            push(id<<1,lazy[id]);
            push(id<<1|1,lazy[id]);
            lazy[id] = 0;
        }
        void update(ll l1,ll r1,ll id=1,ll l=0,ll r=n-1){
//            if (id==1)cout<<"UPD "<<l1<<' '<<r1<<endl;
            if (l1 > r || l > r1 || l1 > r1)return;
            if (l1 <= l && r <= r1){
//                cout<<"UPD2 "<<l<<' '<<r<<endl;
                push(id,-1);
                return;
            }
            lz(id);
            ll mid = (l + r) >> 1;
            update(l1,r1,id<<1,l,mid);
            update(l1,r1,id<<1|1,mid+1,r);
            tree[id] = combine(tree[id<<1],tree[id<<1|1]);
        }
        void del(ll i,ll id=1,ll l=0,ll r=n-1){
            if (l > i || i > r)return;
            if (l==r){
//                cout<<"BRUH "<<i<<endl;
                tree[id].fi = INF;
                return;
            }
            lz(id);
            ll mid = (l + r) >> 1;
            del(i,id<<1,l,mid);
            del(i,id<<1|1,mid+1,r);
            tree[id] = combine(tree[id<<1],tree[id<<1|1]);
        }
        ll get(ll l1,ll r1,ll id=1,ll l=0,ll r=n-1){
            if (l1 > r || l > r1 || l1 > r1)return n+1;
            if (tree[id].fi!=0)return n+1;
            if (l1 <= l && r <= r1)return tree[id].se;
            lz(id);
            ll mid = (l + r) >> 1;
            ll res = get(l1,r1,id<<1,l,mid);
            if (res==n+1)res = get(l1,r1,id<<1|1,mid+1,r);
            return res;
        }
        ll sus(ll i,ll id=1,ll l=0,ll r=n-1){
            if (i > r || l > i)return INF+1;
            if (l == r)return tree[id].fi;
            lz(id);
            ll mid = (l + r) >> 1;
            return min(sus(i,id<<1,l,mid),sus(i,id<<1|1,mid+1,r));
        }
    }
    vector <ll> h;
    ll label;
    bool in[MAXN];
    void solve(ll x){
//        assert(in[x] == 0);
//        in[x] = 1;
        ll l = x-k+1,r=x-1;
//        cout<<x<<' '<<l<<' '<<r<<endl;
        ll tmp;
        while ((tmp=seg::get(l,r)) != n+1){
            solve(tmp);
        }
        while ((tmp=seg::get(l+n,r+n)) != n+1){
//            cout<<x<<' '<<tmp<<endl;
            solve(tmp);
        }

        seg::del(x);
        h[x] = --label;
        seg::update(l,r);
        seg::update(l+n,r+n);
    }
    const ll MAXK = 20;
    ll sp_nxt[MAXK][MAXN*2];
    ll sp_pre[MAXK][MAXN*2];
    bool solve_nxt(ll x,ll y){
        for (ll j = MAXK-1;j>=0;j--){
            if (sp_nxt[j][x] <= y)x = sp_nxt[j][x];
        }
        return (y-x<k&&h[y]>=h[x]);
    }
    bool solve_pre(ll x,ll y){
        for (ll j = MAXK-1;j>=0;j--){
            if (sp_pre[j][x] >= y)x = sp_pre[j][x];
        }
        return (x-y<k&&h[y]>=h[x]);
    }
}

void init(int K, std::vector<int> r){
    using namespace trung;
    val = r;
    n=sz(r);
    h.resize(n);
    k=K;
    seg::build();
//    cout<<"OK"<<endl;
    ll cur;
    label = n;
    while ((cur=seg::get(0,n-1)) != n+1){
//        cout<<"CUR "<<cur<<endl;
        solve(cur);
    }
    for (ll i = 0;i < n;i ++){
        h.push_back(h[i]);
    }
    h.insert(h.begin(),INF);
    h.push_back(INF);
    {
        set <pll> s;
        for (ll i = 1;i <= k - 1;i ++){
            s.insert(MP(h[i],i));
        }
        for (ll i = 1;i <= 2 * n;i ++){
            s.erase(MP(h[i],i));
            if (i + k - 1 <= 2 * n)s.insert(MP(h[i+k-1],i+k-1));
            auto tmp = s.lower_bound(MP(h[i],i)); ;
            if (tmp != s.end())sp_nxt[0][i] = (*tmp).se;
            else sp_nxt[0][i] = i;
        }
        s.clear();
        for (ll i = 2*n;i>=2*n-k+2;i--)s.insert(MP(h[i],i));
        for (ll i = 2*n;i >= 1;i--){
            s.erase(MP(h[i],i));
            if (i - k + 1 >= 1)s.insert(MP(h[i - k + 1],i - k + 1));
            auto tmp = s.lower_bound(MP(h[i],i)); ;
            if (tmp != s.end())sp_pre[0][i] = (*tmp).se;
            else sp_pre[0][i] = i;
        }
//        for (ll i = 1;i <= 2 * n;i ++){
//            cout<<h[i]<<' ';
//        }
//        cout<<endl;
//        for (ll i = 1;i <= 2 * n;i ++){
//            cout<<sp_nxt[0][i]<<' ';
//        }
//        cout<<endl;
//        for (ll i = 1;i <= 2 * n;i ++){
//            cout<<sp_pre[0][i]<<' ';
//        }
//        cout<<endl;
    }
    for (ll j = 1;j<MAXK;j ++){
        for (ll i = 1;i <= 2 * n;i ++){
            sp_nxt[j][i] = sp_nxt[j-1][sp_nxt[j-1][i]];
            sp_pre[j][i] = sp_pre[j-1][sp_pre[j-1][i]];
        }
    }
}
int compare_plants(int x, int y){
    using namespace trung;
    x++,y++;
    if (h[x] > h[y]){
        if (solve_nxt(y,x+n)||solve_pre(y,x))return 1;
        else return 0;
    }
    else{
        if (solve_nxt(x,y)||solve_pre(x+n,y))return -1;
        else return 0;
    }
//    return (h[x] > h[y] ? 1 : -1);
}

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 42 ms 4556 KB Output is correct
7 Correct 96 ms 20164 KB Output is correct
8 Correct 365 ms 148728 KB Output is correct
9 Correct 353 ms 148772 KB Output is correct
10 Correct 326 ms 148728 KB Output is correct
11 Correct 366 ms 148780 KB Output is correct
12 Correct 410 ms 148752 KB Output is correct
13 Correct 462 ms 148732 KB Output is correct
14 Correct 329 ms 148736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 4 ms 1628 KB Output is correct
7 Correct 62 ms 9340 KB Output is correct
8 Correct 3 ms 860 KB Output is correct
9 Correct 3 ms 1628 KB Output is correct
10 Correct 64 ms 9156 KB Output is correct
11 Correct 84 ms 9060 KB Output is correct
12 Correct 75 ms 9216 KB Output is correct
13 Correct 61 ms 9268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 1 ms 860 KB Output is correct
6 Correct 4 ms 1628 KB Output is correct
7 Correct 62 ms 9340 KB Output is correct
8 Correct 3 ms 860 KB Output is correct
9 Correct 3 ms 1628 KB Output is correct
10 Correct 64 ms 9156 KB Output is correct
11 Correct 84 ms 9060 KB Output is correct
12 Correct 75 ms 9216 KB Output is correct
13 Correct 61 ms 9268 KB Output is correct
14 Correct 130 ms 20856 KB Output is correct
15 Correct 1006 ms 157204 KB Output is correct
16 Correct 130 ms 21096 KB Output is correct
17 Correct 1078 ms 157296 KB Output is correct
18 Correct 724 ms 155436 KB Output is correct
19 Correct 576 ms 155892 KB Output is correct
20 Correct 1035 ms 161956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 61 ms 6648 KB Output is correct
4 Correct 516 ms 149416 KB Output is correct
5 Correct 630 ms 148976 KB Output is correct
6 Correct 694 ms 149020 KB Output is correct
7 Correct 804 ms 150012 KB Output is correct
8 Correct 958 ms 155020 KB Output is correct
9 Correct 546 ms 148776 KB Output is correct
10 Correct 453 ms 148728 KB Output is correct
11 Correct 389 ms 148732 KB Output is correct
12 Correct 325 ms 148808 KB Output is correct
13 Correct 546 ms 152568 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 600 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 2 ms 860 KB Output is correct
7 Correct 11 ms 1884 KB Output is correct
8 Correct 10 ms 1884 KB Output is correct
9 Correct 12 ms 1884 KB Output is correct
10 Correct 10 ms 1780 KB Output is correct
11 Correct 10 ms 1884 KB Output is correct
12 Correct 11 ms 1856 KB Output is correct
13 Correct 10 ms 1880 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 0 ms 604 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 2 ms 1372 KB Output is correct
6 Correct 413 ms 148008 KB Output is correct
7 Correct 482 ms 148376 KB Output is correct
8 Correct 676 ms 149104 KB Output is correct
9 Correct 804 ms 154520 KB Output is correct
10 Correct 379 ms 147972 KB Output is correct
11 Correct 529 ms 153596 KB Output is correct
12 Correct 326 ms 148732 KB Output is correct
13 Correct 397 ms 147964 KB Output is correct
14 Correct 527 ms 148164 KB Output is correct
15 Correct 619 ms 148940 KB Output is correct
16 Correct 298 ms 147752 KB Output is correct
17 Correct 385 ms 147964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 604 KB Output is correct
2 Correct 0 ms 604 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 42 ms 4556 KB Output is correct
7 Correct 96 ms 20164 KB Output is correct
8 Correct 365 ms 148728 KB Output is correct
9 Correct 353 ms 148772 KB Output is correct
10 Correct 326 ms 148728 KB Output is correct
11 Correct 366 ms 148780 KB Output is correct
12 Correct 410 ms 148752 KB Output is correct
13 Correct 462 ms 148732 KB Output is correct
14 Correct 329 ms 148736 KB Output is correct
15 Correct 0 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 1 ms 604 KB Output is correct
18 Correct 0 ms 604 KB Output is correct
19 Correct 1 ms 860 KB Output is correct
20 Correct 4 ms 1628 KB Output is correct
21 Correct 62 ms 9340 KB Output is correct
22 Correct 3 ms 860 KB Output is correct
23 Correct 3 ms 1628 KB Output is correct
24 Correct 64 ms 9156 KB Output is correct
25 Correct 84 ms 9060 KB Output is correct
26 Correct 75 ms 9216 KB Output is correct
27 Correct 61 ms 9268 KB Output is correct
28 Correct 130 ms 20856 KB Output is correct
29 Correct 1006 ms 157204 KB Output is correct
30 Correct 130 ms 21096 KB Output is correct
31 Correct 1078 ms 157296 KB Output is correct
32 Correct 724 ms 155436 KB Output is correct
33 Correct 576 ms 155892 KB Output is correct
34 Correct 1035 ms 161956 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 0 ms 604 KB Output is correct
37 Correct 61 ms 6648 KB Output is correct
38 Correct 516 ms 149416 KB Output is correct
39 Correct 630 ms 148976 KB Output is correct
40 Correct 694 ms 149020 KB Output is correct
41 Correct 804 ms 150012 KB Output is correct
42 Correct 958 ms 155020 KB Output is correct
43 Correct 546 ms 148776 KB Output is correct
44 Correct 453 ms 148728 KB Output is correct
45 Correct 389 ms 148732 KB Output is correct
46 Correct 325 ms 148808 KB Output is correct
47 Correct 546 ms 152568 KB Output is correct
48 Correct 1 ms 600 KB Output is correct
49 Correct 0 ms 604 KB Output is correct
50 Correct 0 ms 604 KB Output is correct
51 Correct 1 ms 604 KB Output is correct
52 Correct 1 ms 604 KB Output is correct
53 Correct 2 ms 860 KB Output is correct
54 Correct 11 ms 1884 KB Output is correct
55 Correct 10 ms 1884 KB Output is correct
56 Correct 12 ms 1884 KB Output is correct
57 Correct 10 ms 1780 KB Output is correct
58 Correct 10 ms 1884 KB Output is correct
59 Correct 11 ms 1856 KB Output is correct
60 Correct 10 ms 1880 KB Output is correct
61 Correct 51 ms 6500 KB Output is correct
62 Correct 80 ms 20168 KB Output is correct
63 Correct 332 ms 148728 KB Output is correct
64 Correct 426 ms 148912 KB Output is correct
65 Correct 632 ms 149076 KB Output is correct
66 Correct 693 ms 149916 KB Output is correct
67 Correct 908 ms 155132 KB Output is correct
68 Correct 455 ms 149020 KB Output is correct
69 Correct 594 ms 154356 KB Output is correct
70 Correct 535 ms 149416 KB Output is correct
71 Correct 541 ms 148852 KB Output is correct
72 Correct 638 ms 149216 KB Output is correct
73 Correct 721 ms 149996 KB Output is correct
74 Correct 382 ms 148724 KB Output is correct
75 Correct 480 ms 149000 KB Output is correct