답안 #416064

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
416064 2021-06-01T21:31:43 Z duality 식물 비교 (IOI20_plants) C++11
100 / 100
1554 ms 62628 KB
#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------
#include "plants.h"

int n,kk;
int a[200000],h[200000],o[200000];
pii tree[1 << 19];
int lazy[1 << 19];
int build(int s,int e,int i) {
    if (s == e) {
        tree[i] = mp(a[s],s),lazy[i] = 0;
        return 0;
    }

    int mid = (s+e) / 2;
    build(s,mid,2*i+1),build(mid+1,e,2*i+2);
    tree[i] = min(tree[2*i+1],tree[2*i+2]),lazy[i] = 0;
    return 0;
}
int prop(int s,int e,int i) {
    tree[i].first += lazy[i];
    if (s != e) lazy[2*i+1] += lazy[i],lazy[2*i+2] += lazy[i];
    lazy[i] = 0;
    return 0;
}
int update(int s,int e,int as,int ae,int i,int num) {
    prop(s,e,i);
    if ((s > ae) || (e < as)) return 0;
    else if ((s >= as) && (e <= ae)) {
        lazy[i] += num;
        prop(s,e,i);
        return 0;
    }

    int mid = (s+e) / 2;
    update(s,mid,as,ae,2*i+1,num),update(mid+1,e,as,ae,2*i+2,num);
    tree[i] = min(tree[2*i+1],tree[2*i+2]);
    return 0;
}
pii query(int s,int e,int qs,int qe,int i) {
    prop(s,e,i);
    if ((s > qe) || (e < qs)) return mp(1e9,-1);
    else if ((s >= qs) && (e <= qe)) return tree[i];

    int mid = (s+e) / 2;
    return min(query(s,mid,qs,qe,2*i+1),query(mid+1,e,qs,qe,2*i+2));
}
set<int> S;
set<pii> S2;
int insert(int u) {
    if (S.empty()) {
        S.insert(u),S2.insert(mp(n,u));
        return 0;
    }
    auto it = S.lower_bound(u);
    if (it == S.end()) it = S.begin();
    auto it2 = it;
    if (it2 == S.begin()) it2 = S.end();
    it2--;
    S2.erase(mp(((*it-*it2+n-1) % n)+1,*it));
    S2.insert(mp(((u-*it2+n-1) % n)+1,u));
    S2.insert(mp(((*it-u+n-1) % n)+1,*it));
    S.insert(u);
    return 0;
}
int erase(int u) {
    S.erase(u);
    if (S.empty()) {
        S2.erase(mp(n,u));
        return 0;
    }
    auto it = S.lower_bound(u);
    if (it == S.end()) it = S.begin();
    auto it2 = it;
    if (it2 == S.begin()) it2 = S.end();
    it2--;
    S2.erase(mp(((u-*it2+n-1) % n)+1,u));
    S2.erase(mp(((*it-u+n-1) % n)+1,*it));
    S2.insert(mp(((*it-*it2+n-1) % n)+1,*it));
    return 0;
}
int ll[200000][18],rr[200000][18],logn;
void init(int k,vector<int> r) {
    int i;
    n = r.size(),kk = --k;
    for (i = 0; i < n; i++) a[i] = r[i];
    build(0,n-1,0);
    for (i = 0; i < n; i++) {
        if (r[i] == 0) insert(i),update(0,n-1,i,i,0,1e9);
    }
    for (i = n-1; i >= 0; i--) {
        int u = S2.rbegin()->second;
        o[i] = u,h[u] = i,a[u] = 1e9,erase(u);
        if (u-k >= 0) update(0,n-1,u-k,u-1,0,-1);
        else update(0,n-1,0,u-1,0,-1),update(0,n-1,u-k+n,n-1,0,-1);
        while (tree[0].first == 0) insert(tree[0].second),update(0,n-1,tree[0].second,tree[0].second,0,1e9);
    }
    build(0,n-1,0);
    for (i = n-1; i >= 0; i--) {
        pii p;
        int u = o[i];
        update(0,n-1,u,u,0,i-1e9);
        if (u-k >= 0) p = query(0,n-1,u-k,u-1,0);
        else p = min(query(0,n-1,0,u-1,0),query(0,n-1,u-k+n,n-1,0));
        if (p.first == 1e9) ll[u][0] = n;
        else ll[u][0] = (u-p.second+n) % n;
        if (u+k < n) p = query(0,n-1,u+1,u+k,0);
        else p = min(query(0,n-1,u+1,n-1,0),query(0,n-1,0,u+k-n,0));
        if (p.first == 1e9) rr[u][0] = n;
        else rr[u][0] = (p.second-u+n) % n;
    }
    int j;
    for (i = 1; (1 << i) < n; i++) {
        for (j = 0; j < n; j++) {
            ll[j][i] = min(ll[j][i-1]+ll[(j-ll[j][i-1]+n) % n][i-1],n);
            rr[j][i] = min(rr[j][i-1]+rr[(j+rr[j][i-1]) % n][i-1],n);
        }
    }
    logn = i;
}
int path(int x,int y) {
    int i,u = x;
    for (i = logn-1; i >= 0; i--) {
        if (ll[u][i] < ((u-y+n) % n)) u = (u-ll[u][i]+n) % n;
    }
    if ((((u-y+n) % n) <= kk) && (h[u] < h[y])) return 1;
    u = x;
    for (i = logn-1; i >= 0; i--) {
        if (rr[u][i] < ((y-u+n) % n)) u = (u+rr[u][i]) % n;
    }
    if ((((y-u+n) % n) <= kk) && (h[u] < h[y])) return 1;
    return 0;
}
int compare_plants(int x,int y) {
    if (h[x] < h[y]) return path(x,y) ? -1:0;
    else return path(y,x) ? 1:0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 70 ms 4036 KB Output is correct
7 Correct 179 ms 9828 KB Output is correct
8 Correct 935 ms 53096 KB Output is correct
9 Correct 1014 ms 52844 KB Output is correct
10 Correct 1125 ms 52992 KB Output is correct
11 Correct 1077 ms 54040 KB Output is correct
12 Correct 1206 ms 53668 KB Output is correct
13 Correct 1191 ms 62628 KB Output is correct
14 Correct 983 ms 43700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 5 ms 636 KB Output is correct
7 Correct 92 ms 5964 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 5 ms 588 KB Output is correct
10 Correct 109 ms 5988 KB Output is correct
11 Correct 111 ms 6120 KB Output is correct
12 Correct 118 ms 6084 KB Output is correct
13 Correct 116 ms 5984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 308 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 5 ms 636 KB Output is correct
7 Correct 92 ms 5964 KB Output is correct
8 Correct 3 ms 460 KB Output is correct
9 Correct 5 ms 588 KB Output is correct
10 Correct 109 ms 5988 KB Output is correct
11 Correct 111 ms 6120 KB Output is correct
12 Correct 118 ms 6084 KB Output is correct
13 Correct 116 ms 5984 KB Output is correct
14 Correct 172 ms 9276 KB Output is correct
15 Correct 1331 ms 44612 KB Output is correct
16 Correct 195 ms 9388 KB Output is correct
17 Correct 1310 ms 44608 KB Output is correct
18 Correct 1359 ms 53612 KB Output is correct
19 Correct 1055 ms 44588 KB Output is correct
20 Correct 1214 ms 44676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 1 ms 312 KB Output is correct
3 Correct 112 ms 4392 KB Output is correct
4 Correct 1148 ms 50064 KB Output is correct
5 Correct 1219 ms 45340 KB Output is correct
6 Correct 1477 ms 44324 KB Output is correct
7 Correct 1554 ms 44384 KB Output is correct
8 Correct 1422 ms 44564 KB Output is correct
9 Correct 1127 ms 48464 KB Output is correct
10 Correct 1294 ms 45376 KB Output is correct
11 Correct 1290 ms 62588 KB Output is correct
12 Correct 996 ms 43908 KB Output is correct
13 Correct 1414 ms 57080 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 304 KB Output is correct
6 Correct 5 ms 460 KB Output is correct
7 Correct 21 ms 1324 KB Output is correct
8 Correct 18 ms 1348 KB Output is correct
9 Correct 26 ms 1268 KB Output is correct
10 Correct 18 ms 1348 KB Output is correct
11 Correct 23 ms 1340 KB Output is correct
12 Correct 23 ms 1296 KB Output is correct
13 Correct 20 ms 1296 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 308 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 4 ms 460 KB Output is correct
6 Correct 1124 ms 43348 KB Output is correct
7 Correct 1323 ms 43332 KB Output is correct
8 Correct 1453 ms 43568 KB Output is correct
9 Correct 1313 ms 43700 KB Output is correct
10 Correct 1043 ms 47636 KB Output is correct
11 Correct 1385 ms 44540 KB Output is correct
12 Correct 978 ms 49192 KB Output is correct
13 Correct 1075 ms 44492 KB Output is correct
14 Correct 1335 ms 43452 KB Output is correct
15 Correct 1442 ms 43516 KB Output is correct
16 Correct 1031 ms 46096 KB Output is correct
17 Correct 1080 ms 43604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 288 KB Output is correct
2 Correct 1 ms 352 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 352 KB Output is correct
6 Correct 70 ms 4036 KB Output is correct
7 Correct 179 ms 9828 KB Output is correct
8 Correct 935 ms 53096 KB Output is correct
9 Correct 1014 ms 52844 KB Output is correct
10 Correct 1125 ms 52992 KB Output is correct
11 Correct 1077 ms 54040 KB Output is correct
12 Correct 1206 ms 53668 KB Output is correct
13 Correct 1191 ms 62628 KB Output is correct
14 Correct 983 ms 43700 KB Output is correct
15 Correct 1 ms 308 KB Output is correct
16 Correct 1 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 5 ms 636 KB Output is correct
21 Correct 92 ms 5964 KB Output is correct
22 Correct 3 ms 460 KB Output is correct
23 Correct 5 ms 588 KB Output is correct
24 Correct 109 ms 5988 KB Output is correct
25 Correct 111 ms 6120 KB Output is correct
26 Correct 118 ms 6084 KB Output is correct
27 Correct 116 ms 5984 KB Output is correct
28 Correct 172 ms 9276 KB Output is correct
29 Correct 1331 ms 44612 KB Output is correct
30 Correct 195 ms 9388 KB Output is correct
31 Correct 1310 ms 44608 KB Output is correct
32 Correct 1359 ms 53612 KB Output is correct
33 Correct 1055 ms 44588 KB Output is correct
34 Correct 1214 ms 44676 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 1 ms 312 KB Output is correct
37 Correct 112 ms 4392 KB Output is correct
38 Correct 1148 ms 50064 KB Output is correct
39 Correct 1219 ms 45340 KB Output is correct
40 Correct 1477 ms 44324 KB Output is correct
41 Correct 1554 ms 44384 KB Output is correct
42 Correct 1422 ms 44564 KB Output is correct
43 Correct 1127 ms 48464 KB Output is correct
44 Correct 1294 ms 45376 KB Output is correct
45 Correct 1290 ms 62588 KB Output is correct
46 Correct 996 ms 43908 KB Output is correct
47 Correct 1414 ms 57080 KB Output is correct
48 Correct 1 ms 332 KB Output is correct
49 Correct 1 ms 332 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 304 KB Output is correct
53 Correct 5 ms 460 KB Output is correct
54 Correct 21 ms 1324 KB Output is correct
55 Correct 18 ms 1348 KB Output is correct
56 Correct 26 ms 1268 KB Output is correct
57 Correct 18 ms 1348 KB Output is correct
58 Correct 23 ms 1340 KB Output is correct
59 Correct 23 ms 1296 KB Output is correct
60 Correct 20 ms 1296 KB Output is correct
61 Correct 100 ms 5208 KB Output is correct
62 Correct 184 ms 9376 KB Output is correct
63 Correct 1028 ms 46088 KB Output is correct
64 Correct 1217 ms 44124 KB Output is correct
65 Correct 1438 ms 44156 KB Output is correct
66 Correct 1548 ms 44356 KB Output is correct
67 Correct 1505 ms 44472 KB Output is correct
68 Correct 1158 ms 48488 KB Output is correct
69 Correct 1413 ms 45472 KB Output is correct
70 Correct 1222 ms 50068 KB Output is correct
71 Correct 1399 ms 45280 KB Output is correct
72 Correct 1380 ms 44420 KB Output is correct
73 Correct 1449 ms 44384 KB Output is correct
74 Correct 1022 ms 45292 KB Output is correct
75 Correct 1057 ms 44424 KB Output is correct