Submission #1038340

# Submission time Handle Problem Language Result Execution time Memory
1038340 2024-07-29T17:05:14 Z Dan4Life Comparing Plants (IOI20_plants) C++17
100 / 100
1810 ms 77828 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a), end(a)
using ar2 = array<int,2>;
const int mxN = (int)2e5+10;
const int mxLg = (int)19;
const int INF = (int)1e9;
int n, k, h[mxN*2];
int jmp[2][mxLg][mxN*2];

template<class T, int SZ> struct SegTree{
    T seg[SZ], lzy[SZ];
    
    void init(){ memset(seg,0,sizeof(seg)); memset(lzy,0,sizeof(lzy)); }
    
    void prop(int p, int l, int r){
        if(!lzy[p] or l==r) return;
        int mid = (l+r)/2; int rp = p+2*(mid-l+1);
        seg[p+1]+=lzy[p]; seg[rp]+=lzy[p];
        lzy[p+1]+=lzy[p]; lzy[rp]+=lzy[p]; lzy[p]=0;
    }
    
    void upd(int i, int j, int v, int p=0, int l=0, int r=n-1){
        if(i>r or j<l or i>j) return; prop(p,l,r);
        if(i<=l and r<=j){ seg[p]+=v; lzy[p]+=v; return; }
        int mid = (l+r)/2; int rp = p+2*(mid-l+1);
        if(i<=mid) upd(i,j,v,p+1,l,mid);
        if(j>mid) upd(i,j,v,rp,mid+1,r);
        seg[p] = min(seg[p+1],seg[rp]);
    }
    
    int findFirstMin(int i, int j, int p=0, int l=0, int r=n-1){
        if(i>r or j<l or i>j) return -1; prop(p,l,r);
        if(l==r) return l; 
        int mid = (l+r)/2; int rp = p+2*(mid-l+1);
        if(i>mid) return findFirstMin(i,j,rp,mid+1,r);
        if(j<=mid) return findFirstMin(i,j,p+1,l,mid);
        if(query(i,mid)==query(i,j)) return findFirstMin(i,j,p+1,l,mid);
        return findFirstMin(i,j,rp,mid+1,r);
    }
    
    T query(int i, int j, int p=0, int l=0, int r=n-1){
        if(j<l or i>r or i>j) return INF; prop(p,l,r);
        if(i<=l and r<=j) return seg[p];
        int mid = (l+r)/2; int rp = p+2*(mid-l+1);
        return min(query(i,j,p+1,l,mid), query(i,j,rp,mid+1,r)); 
    }
};
const int mxSeg = 2*mxN;
SegTree<int,mxSeg> segTree;

int cur;
void solve(int i){
    bool ok = 1;
    while(ok){
        ok = 0;
        if(i<k){
            int i1 = segTree.findFirstMin(n-k+i,n-1);
            if(i1!=-1 and !segTree.query(i1,i1)) solve(i1), ok=1;
        }
        int i1 = segTree.findFirstMin(max(0,i-k),i-1);
        if(i1!=-1 and !segTree.query(i1,i1)) solve(i1), ok=1;
    }
    if(i<k){
        segTree.upd(0,i-1,-1);
        segTree.upd(n-k+i,n-1,-1);
    }
    else segTree.upd(i-k,i-1,-1);
    segTree.upd(i,i,INF); h[i]=cur--;
}

void init(int K, vector<int> r){
    n = sz(r); k=K-1; cur = n; 
    memset(jmp,-1, sizeof jmp);
    segTree.init(); 
    for(int i = 0; i < n; i++) 
        segTree.upd(i,i,r[i]);
    while(!segTree.query(0,n-1)) 
        solve(segTree.findFirstMin(0,n-1));
    for(int i = n; i < 2*n; i++) h[i] = h[i-n];
    set<ar2> S; int j = 0;
    for(int i = 0; i < 2*n; i++){
        while(j<2*n and abs(i-j)<=k)
            S.insert({h[j],j}),j++;
        S.erase({h[i],i});
        auto itr = S.lower_bound({h[i],i});
        if(itr!=end(S)) jmp[0][0][i]=(*itr)[1];
        if(itr!=begin(S)) jmp[1][0][i]=(*prev(itr))[1];
    }
    for(int t = 0; t < 2; t++)
        for(int i = 1; i < mxLg; i++)
            for(int j = 0; j < 2*n; j++)
                if(jmp[t][i-1][j]!=-1) jmp[t][i][j]
                    =jmp[t][i-1][jmp[t][i-1][j]];
}

int comp(int x, int y, bool g){
    for(int i = mxLg-1; i>=0; i--){
        int j = jmp[g][i][x];
        if(j!=-1 and j<=y) x=j;
    }
    return y-x<=k and (g?h[x]>=h[y]:h[x]<=h[y]);
}

int compare_plants(int x, int y){
    if(comp(x,y,1) or comp(y,x+n,0)) return 1;
    if(comp(x,y,0) or comp(y,x+n,1)) return -1;
    return 0;
}

Compilation message

plants.cpp: In member function 'void SegTree<T, SZ>::upd(int, int, int, int, int, int)':
plants.cpp:27:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   27 |         if(i>r or j<l or i>j) return; prop(p,l,r);
      |         ^~
plants.cpp:27:39: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   27 |         if(i>r or j<l or i>j) return; prop(p,l,r);
      |                                       ^~~~
plants.cpp: In member function 'int SegTree<T, SZ>::findFirstMin(int, int, int, int, int)':
plants.cpp:36:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   36 |         if(i>r or j<l or i>j) return -1; prop(p,l,r);
      |         ^~
plants.cpp:36:42: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   36 |         if(i>r or j<l or i>j) return -1; prop(p,l,r);
      |                                          ^~~~
plants.cpp: In member function 'T SegTree<T, SZ>::query(int, int, int, int, int)':
plants.cpp:46:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   46 |         if(j<l or i>r or i>j) return INF; prop(p,l,r);
      |         ^~
plants.cpp:46:43: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   46 |         if(j<l or i>r or i>j) return INF; prop(p,l,r);
      |                                           ^~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64088 KB Output is correct
2 Correct 9 ms 64092 KB Output is correct
3 Correct 8 ms 64092 KB Output is correct
4 Correct 9 ms 64092 KB Output is correct
5 Correct 9 ms 64092 KB Output is correct
6 Correct 49 ms 66836 KB Output is correct
7 Correct 108 ms 67156 KB Output is correct
8 Correct 374 ms 68432 KB Output is correct
9 Correct 362 ms 68436 KB Output is correct
10 Correct 377 ms 68436 KB Output is correct
11 Correct 374 ms 68432 KB Output is correct
12 Correct 378 ms 68436 KB Output is correct
13 Correct 342 ms 68356 KB Output is correct
14 Correct 418 ms 68352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64088 KB Output is correct
2 Correct 8 ms 64088 KB Output is correct
3 Correct 8 ms 64024 KB Output is correct
4 Correct 8 ms 64088 KB Output is correct
5 Correct 8 ms 64092 KB Output is correct
6 Correct 13 ms 64092 KB Output is correct
7 Correct 79 ms 67148 KB Output is correct
8 Correct 9 ms 64088 KB Output is correct
9 Correct 13 ms 64092 KB Output is correct
10 Correct 79 ms 67120 KB Output is correct
11 Correct 61 ms 66900 KB Output is correct
12 Correct 92 ms 67156 KB Output is correct
13 Correct 79 ms 67152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64088 KB Output is correct
2 Correct 8 ms 64088 KB Output is correct
3 Correct 8 ms 64024 KB Output is correct
4 Correct 8 ms 64088 KB Output is correct
5 Correct 8 ms 64092 KB Output is correct
6 Correct 13 ms 64092 KB Output is correct
7 Correct 79 ms 67148 KB Output is correct
8 Correct 9 ms 64088 KB Output is correct
9 Correct 13 ms 64092 KB Output is correct
10 Correct 79 ms 67120 KB Output is correct
11 Correct 61 ms 66900 KB Output is correct
12 Correct 92 ms 67156 KB Output is correct
13 Correct 79 ms 67152 KB Output is correct
14 Correct 190 ms 67672 KB Output is correct
15 Correct 1751 ms 74324 KB Output is correct
16 Correct 183 ms 67488 KB Output is correct
17 Correct 1794 ms 74320 KB Output is correct
18 Correct 924 ms 72988 KB Output is correct
19 Correct 1068 ms 73044 KB Output is correct
20 Correct 1810 ms 77828 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 64088 KB Output is correct
2 Correct 8 ms 64092 KB Output is correct
3 Correct 64 ms 66872 KB Output is correct
4 Correct 555 ms 71248 KB Output is correct
5 Correct 820 ms 68688 KB Output is correct
6 Correct 1175 ms 68412 KB Output is correct
7 Correct 1530 ms 68872 KB Output is correct
8 Correct 1752 ms 72736 KB Output is correct
9 Correct 630 ms 68684 KB Output is correct
10 Correct 622 ms 68432 KB Output is correct
11 Correct 318 ms 68432 KB Output is correct
12 Correct 583 ms 68436 KB Output is correct
13 Correct 836 ms 71200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64092 KB Output is correct
2 Correct 8 ms 64092 KB Output is correct
3 Correct 8 ms 64036 KB Output is correct
4 Correct 8 ms 64028 KB Output is correct
5 Correct 8 ms 64176 KB Output is correct
6 Correct 9 ms 64092 KB Output is correct
7 Correct 21 ms 64728 KB Output is correct
8 Correct 18 ms 64604 KB Output is correct
9 Correct 19 ms 64856 KB Output is correct
10 Correct 19 ms 64860 KB Output is correct
11 Correct 20 ms 64604 KB Output is correct
12 Correct 18 ms 64856 KB Output is correct
13 Correct 18 ms 64856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64088 KB Output is correct
2 Correct 8 ms 64220 KB Output is correct
3 Correct 8 ms 64204 KB Output is correct
4 Correct 8 ms 64092 KB Output is correct
5 Correct 11 ms 63988 KB Output is correct
6 Correct 772 ms 68436 KB Output is correct
7 Correct 1109 ms 68396 KB Output is correct
8 Correct 1444 ms 68900 KB Output is correct
9 Correct 1786 ms 72736 KB Output is correct
10 Correct 649 ms 68600 KB Output is correct
11 Correct 1145 ms 72272 KB Output is correct
12 Correct 542 ms 72016 KB Output is correct
13 Correct 812 ms 68860 KB Output is correct
14 Correct 1046 ms 68432 KB Output is correct
15 Correct 1428 ms 68900 KB Output is correct
16 Correct 504 ms 68440 KB Output is correct
17 Correct 741 ms 68360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 64088 KB Output is correct
2 Correct 9 ms 64092 KB Output is correct
3 Correct 8 ms 64092 KB Output is correct
4 Correct 9 ms 64092 KB Output is correct
5 Correct 9 ms 64092 KB Output is correct
6 Correct 49 ms 66836 KB Output is correct
7 Correct 108 ms 67156 KB Output is correct
8 Correct 374 ms 68432 KB Output is correct
9 Correct 362 ms 68436 KB Output is correct
10 Correct 377 ms 68436 KB Output is correct
11 Correct 374 ms 68432 KB Output is correct
12 Correct 378 ms 68436 KB Output is correct
13 Correct 342 ms 68356 KB Output is correct
14 Correct 418 ms 68352 KB Output is correct
15 Correct 8 ms 64088 KB Output is correct
16 Correct 8 ms 64088 KB Output is correct
17 Correct 8 ms 64024 KB Output is correct
18 Correct 8 ms 64088 KB Output is correct
19 Correct 8 ms 64092 KB Output is correct
20 Correct 13 ms 64092 KB Output is correct
21 Correct 79 ms 67148 KB Output is correct
22 Correct 9 ms 64088 KB Output is correct
23 Correct 13 ms 64092 KB Output is correct
24 Correct 79 ms 67120 KB Output is correct
25 Correct 61 ms 66900 KB Output is correct
26 Correct 92 ms 67156 KB Output is correct
27 Correct 79 ms 67152 KB Output is correct
28 Correct 190 ms 67672 KB Output is correct
29 Correct 1751 ms 74324 KB Output is correct
30 Correct 183 ms 67488 KB Output is correct
31 Correct 1794 ms 74320 KB Output is correct
32 Correct 924 ms 72988 KB Output is correct
33 Correct 1068 ms 73044 KB Output is correct
34 Correct 1810 ms 77828 KB Output is correct
35 Correct 7 ms 64088 KB Output is correct
36 Correct 8 ms 64092 KB Output is correct
37 Correct 64 ms 66872 KB Output is correct
38 Correct 555 ms 71248 KB Output is correct
39 Correct 820 ms 68688 KB Output is correct
40 Correct 1175 ms 68412 KB Output is correct
41 Correct 1530 ms 68872 KB Output is correct
42 Correct 1752 ms 72736 KB Output is correct
43 Correct 630 ms 68684 KB Output is correct
44 Correct 622 ms 68432 KB Output is correct
45 Correct 318 ms 68432 KB Output is correct
46 Correct 583 ms 68436 KB Output is correct
47 Correct 836 ms 71200 KB Output is correct
48 Correct 8 ms 64092 KB Output is correct
49 Correct 8 ms 64092 KB Output is correct
50 Correct 8 ms 64036 KB Output is correct
51 Correct 8 ms 64028 KB Output is correct
52 Correct 8 ms 64176 KB Output is correct
53 Correct 9 ms 64092 KB Output is correct
54 Correct 21 ms 64728 KB Output is correct
55 Correct 18 ms 64604 KB Output is correct
56 Correct 19 ms 64856 KB Output is correct
57 Correct 19 ms 64860 KB Output is correct
58 Correct 20 ms 64604 KB Output is correct
59 Correct 18 ms 64856 KB Output is correct
60 Correct 18 ms 64856 KB Output is correct
61 Correct 72 ms 66900 KB Output is correct
62 Correct 121 ms 66968 KB Output is correct
63 Correct 487 ms 68432 KB Output is correct
64 Correct 742 ms 68436 KB Output is correct
65 Correct 1177 ms 68436 KB Output is correct
66 Correct 1563 ms 68896 KB Output is correct
67 Correct 1768 ms 72732 KB Output is correct
68 Correct 632 ms 68756 KB Output is correct
69 Correct 1113 ms 72228 KB Output is correct
70 Correct 571 ms 71764 KB Output is correct
71 Correct 881 ms 68864 KB Output is correct
72 Correct 1186 ms 68420 KB Output is correct
73 Correct 1577 ms 68948 KB Output is correct
74 Correct 582 ms 68380 KB Output is correct
75 Correct 758 ms 68384 KB Output is correct