Submission #1008721

# Submission time Handle Problem Language Result Execution time Memory
1008721 2024-06-26T17:00:58 Z bachhoangxuan Comparing Plants (IOI20_plants) C++17
100 / 100
687 ms 50500 KB
#include "plants.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
const int maxl = 20;
const int inf = 1e9;
int n,k;
vector<int> R,H;
set<int> ss,cc;
int lt[maxn][maxl],rt[maxn][maxl];

void cal(int x,int y){
    cc.erase(y);
    if((y-x+n)%n>=k){
        cc.insert(y);
    }
}
void add(int x){
    if(ss.empty()){
        ss.insert(x);
        cc.insert(x);
        return;
    }
    auto it=ss.lower_bound(x);
    if(it!=ss.end()) cal(x,*it);
    else cal(x,*ss.begin());
    if(it==ss.begin()) cal(*ss.rbegin(),x);
    else{
        it--;
        cal(*it,x);
    }
    ss.insert(x);
}
void del(int x){
    cc.erase(x);ss.erase(x);
    if(ss.empty()) return;
    auto it=ss.lower_bound(x);
    int y=(it!=ss.end()?*it:*ss.begin());
    cc.insert(y);
}

int tree[4*maxn],lazy[4*maxn];
void build(int l,int r,int id){
    lazy[id]=0;
    if(l==r){
        tree[id]=R[l];
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,id<<1);build(mid+1,r,id<<1|1);
    tree[id]=min(tree[id<<1],tree[id<<1|1]);
}
void getnew(int id,int val){
    tree[id]-=val;
    lazy[id]+=val;
}
void pushdown(int id){
    if(!lazy[id]) return;
    getnew(id<<1,lazy[id]);
    getnew(id<<1|1,lazy[id]);
    lazy[id]=0;
}
void get(int l,int r,int id){
    if(tree[id]>0) return;
    if(l==r){
        tree[id]=inf;
        add(l);
        return;
    }
    pushdown(id);
    int mid=(l+r)>>1;
    get(l,mid,id<<1);get(mid+1,r,id<<1|1);
    tree[id]=min(tree[id<<1],tree[id<<1|1]);
}
void update(int l,int r,int id,int tl,int tr){
    if(tr<l || r<tl) return;
    if(tl<=l && r<=tr){
        getnew(id,1);
        return;
    }
    pushdown(id);
    int mid=(l+r)>>1;
    update(l,mid,id<<1,tl,tr);update(mid+1,r,id<<1|1,tl,tr);
    tree[id]=min(tree[id<<1],tree[id<<1|1]);
}
vector<int> solve(vector<int> r){
    R=r;
    vector<int> p(n);
    ss.clear();cc.clear();
    build(0,n-1,1);
    for(int i=n-1;i>=0;i--){
        get(0,n-1,1);
        int x=*cc.begin();
        if((int)cc.size()>1 && x==0) x=*cc.rbegin();
        p[x]=i;del(x);
        if(x>=k-1) update(0,n-1,1,x-k+1,x);
        else{
            update(0,n-1,1,0,x);
            update(0,n-1,1,n-(k-x)+1,n-1);
        }
    }
    return p;
}

void init(int _k, std::vector<int> r) {
    k=_k;
    n=(int)r.size();
    H=solve(r);

    set<pair<int,int>> S;
    for(int i=0;i<k;i++) S.insert({H[i],i});
    for(int i=0;i<n;i++){
        auto it=S.erase(S.lower_bound({H[i],i}));
        if(it!=S.begin()) it=prev(it),rt[i][0]=(it->second-i+n)%n;
        S.insert({H[(i+k)%n],(i+k)%n});
    }
    S.clear();
    for(int i=1;i<=k;i++) S.insert({H[(n-i)%n],(n-i)%n});
    for(int i=0;i<n;i++){
        S.erase({H[(i-k+n)%n],(i-k+n)%n});
        auto it=S.insert({H[i],i}).first;
        if(it!=S.begin()) it=prev(it),lt[i][0]=(i-it->second+n)%n;
    }

    for(int j=1;j<18;j++){
        for(int i=0;i<n;i++){
            rt[i][j]=min(inf,rt[i][j-1]+rt[(i+rt[i][j-1])%n][j-1]);
            lt[i][j]=min(inf,lt[i][j-1]+lt[(i-lt[i][j-1]%n+n)%n][j-1]);
        }
    }
}

bool get_lt(int x,int y){
    int d=(x-y+n)%n;
    for(int i=17;i>=0;i--){
        if(lt[x][i]<=d) d-=lt[x][i],x=(x-lt[x][i]%n+n)%n;
    }
    return (x-y+n)%n<k && H[x]>=H[y];
}

bool get_rt(int x,int y){
    int d=(y-x+n)%n;
    for(int i=17;i>=0;i--){
        if(rt[x][i]<=d) d-=rt[x][i],x=(x+rt[x][i])%n;
    }
    return (y-x+n)%n<k && H[x]>=H[y];
}

int compare_plants(int x, int y) {
    if(get_lt(x,y) || get_rt(x,y)) return 1;
    if(get_lt(y,x) || get_rt(y,x)) return -1;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 103 ms 3092 KB Output is correct
7 Correct 124 ms 7476 KB Output is correct
8 Correct 348 ms 46404 KB Output is correct
9 Correct 322 ms 45812 KB Output is correct
10 Correct 297 ms 45888 KB Output is correct
11 Correct 286 ms 46400 KB Output is correct
12 Correct 309 ms 46076 KB Output is correct
13 Correct 277 ms 50496 KB Output is correct
14 Correct 261 ms 41280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 49 ms 4356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 57 ms 4364 KB Output is correct
11 Correct 82 ms 4176 KB Output is correct
12 Correct 107 ms 4432 KB Output is correct
13 Correct 49 ms 4432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 3 ms 604 KB Output is correct
7 Correct 49 ms 4356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 3 ms 604 KB Output is correct
10 Correct 57 ms 4364 KB Output is correct
11 Correct 82 ms 4176 KB Output is correct
12 Correct 107 ms 4432 KB Output is correct
13 Correct 49 ms 4432 KB Output is correct
14 Correct 78 ms 7476 KB Output is correct
15 Correct 601 ms 47040 KB Output is correct
16 Correct 79 ms 7504 KB Output is correct
17 Correct 597 ms 46908 KB Output is correct
18 Correct 397 ms 45968 KB Output is correct
19 Correct 400 ms 45888 KB Output is correct
20 Correct 687 ms 50496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 76 ms 3584 KB Output is correct
4 Correct 401 ms 44320 KB Output is correct
5 Correct 379 ms 41796 KB Output is correct
6 Correct 388 ms 41284 KB Output is correct
7 Correct 486 ms 41724 KB Output is correct
8 Correct 552 ms 45520 KB Output is correct
9 Correct 359 ms 43588 KB Output is correct
10 Correct 367 ms 42308 KB Output is correct
11 Correct 297 ms 50500 KB Output is correct
12 Correct 365 ms 41280 KB Output is correct
13 Correct 389 ms 47940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 3 ms 348 KB Output is correct
7 Correct 24 ms 1068 KB Output is correct
8 Correct 10 ms 1116 KB Output is correct
9 Correct 17 ms 1072 KB Output is correct
10 Correct 11 ms 1132 KB Output is correct
11 Correct 28 ms 1052 KB Output is correct
12 Correct 21 ms 1112 KB Output is correct
13 Correct 10 ms 1112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 2 ms 672 KB Output is correct
6 Correct 349 ms 41428 KB Output is correct
7 Correct 356 ms 41284 KB Output is correct
8 Correct 357 ms 41712 KB Output is correct
9 Correct 474 ms 45552 KB Output is correct
10 Correct 347 ms 45632 KB Output is correct
11 Correct 358 ms 47724 KB Output is correct
12 Correct 307 ms 46448 KB Output is correct
13 Correct 338 ms 44100 KB Output is correct
14 Correct 329 ms 43584 KB Output is correct
15 Correct 398 ms 44352 KB Output is correct
16 Correct 300 ms 44868 KB Output is correct
17 Correct 353 ms 43696 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 103 ms 3092 KB Output is correct
7 Correct 124 ms 7476 KB Output is correct
8 Correct 348 ms 46404 KB Output is correct
9 Correct 322 ms 45812 KB Output is correct
10 Correct 297 ms 45888 KB Output is correct
11 Correct 286 ms 46400 KB Output is correct
12 Correct 309 ms 46076 KB Output is correct
13 Correct 277 ms 50496 KB Output is correct
14 Correct 261 ms 41280 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 0 ms 344 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 3 ms 604 KB Output is correct
21 Correct 49 ms 4356 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 3 ms 604 KB Output is correct
24 Correct 57 ms 4364 KB Output is correct
25 Correct 82 ms 4176 KB Output is correct
26 Correct 107 ms 4432 KB Output is correct
27 Correct 49 ms 4432 KB Output is correct
28 Correct 78 ms 7476 KB Output is correct
29 Correct 601 ms 47040 KB Output is correct
30 Correct 79 ms 7504 KB Output is correct
31 Correct 597 ms 46908 KB Output is correct
32 Correct 397 ms 45968 KB Output is correct
33 Correct 400 ms 45888 KB Output is correct
34 Correct 687 ms 50496 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 76 ms 3584 KB Output is correct
38 Correct 401 ms 44320 KB Output is correct
39 Correct 379 ms 41796 KB Output is correct
40 Correct 388 ms 41284 KB Output is correct
41 Correct 486 ms 41724 KB Output is correct
42 Correct 552 ms 45520 KB Output is correct
43 Correct 359 ms 43588 KB Output is correct
44 Correct 367 ms 42308 KB Output is correct
45 Correct 297 ms 50500 KB Output is correct
46 Correct 365 ms 41280 KB Output is correct
47 Correct 389 ms 47940 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 0 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 0 ms 344 KB Output is correct
52 Correct 0 ms 348 KB Output is correct
53 Correct 3 ms 348 KB Output is correct
54 Correct 24 ms 1068 KB Output is correct
55 Correct 10 ms 1116 KB Output is correct
56 Correct 17 ms 1072 KB Output is correct
57 Correct 11 ms 1132 KB Output is correct
58 Correct 28 ms 1052 KB Output is correct
59 Correct 21 ms 1112 KB Output is correct
60 Correct 10 ms 1112 KB Output is correct
61 Correct 121 ms 5204 KB Output is correct
62 Correct 141 ms 9556 KB Output is correct
63 Correct 334 ms 45676 KB Output is correct
64 Correct 391 ms 44612 KB Output is correct
65 Correct 417 ms 44572 KB Output is correct
66 Correct 477 ms 45124 KB Output is correct
67 Correct 555 ms 49216 KB Output is correct
68 Correct 415 ms 46656 KB Output is correct
69 Correct 401 ms 48708 KB Output is correct
70 Correct 344 ms 47360 KB Output is correct
71 Correct 398 ms 45120 KB Output is correct
72 Correct 381 ms 44868 KB Output is correct
73 Correct 387 ms 45120 KB Output is correct
74 Correct 373 ms 45268 KB Output is correct
75 Correct 345 ms 44612 KB Output is correct