답안 #537798

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
537798 2022-03-15T14:12:53 Z EqualTurtle 식물 비교 (IOI20_plants) C++14
100 / 100
1157 ms 61732 KB
#include "plants.h"
#include <bits/stdc++.h>
using namespace std;
constexpr int pot = 262144;
constexpr int inf = 1e9 + 7;

class Tree{
public:
	int b[pot * 2 + 7]; // - pot
	int e[pot * 2 + 7]; // - pot
	int mini[pot * 2 + 7];
	int lazy[pot * 2 + 7]; // < 0
	int lmo[pot * 2 + 7]; // if no 0 than -1                - pot
	int rmo[pot * 2 + 7]; // if no 0 than -1                - pot
	int cand[pot * 2 + 7]; // -1 <=> doesn't exist          - pot

	int k, n;

    void quick_update(int w){
        if (w >= pot)
            return;
        mini[w] = min(mini[w * 2], mini[w * 2 + 1]);
        lmo[w] = (mini[w * 2] == 0 ? lmo[w * 2] : lmo[w * 2 + 1]);
        rmo[w] = (mini[w * 2 + 1] == 0 ? rmo[w * 2 + 1] : rmo[w * 2]);

        if (max(cand[w * 2], cand[w * 2 + 1]) != -1)
            cand[w] = max(cand[w * 2], cand[w * 2 + 1]);
        else
        {
            if (mini[w * 2 + 1] == 0 && (lmo[w * 2 + 1] - max(rmo[w * 2], b[w * 2] - 1) >= k))
                cand[w] = lmo[w * 2 + 1];
            else
                cand[w] = -1;
        }

        if (w == 1 && cand[w] == -1)
            cand[w] = lmo[w];
    }

    void newo(int w){
        if (w >= pot){
            lmo[w] = w - pot;
            rmo[w] = w - pot;
            return;
        }
        push_lazy(w * 2);
        push_lazy(w * 2 + 1);
        quick_update(w);
    }

    void push_lazy(int w){
        if (lazy[w] == 0)
            return;
        
        if (w < pot){
            mini[w * 2] += lazy[w];
            lazy[w * 2] += lazy[w];
            mini[w * 2 + 1] += lazy[w];
            lazy[w * 2 + 1] += lazy[w];
        }
        lazy[w] = 0;

        if (mini[w] == 0)
            newo(w);
    }
	
	void init(int ck, vector <int> r){
		k = ck;
		n = (int)r.size();

        for (int i = pot; i < pot * 2; i++) // bottom layer
        {
            b[i] = i - pot;
            e[i] = i - pot;
            mini[i] = (i - pot < n ? r[i - pot] : inf);
            lazy[i] = 0;
            lmo[i] = (mini[i] == 0 ? i - pot : -1);
            rmo[i] = (mini[i] == 0 ? i - pot : -1);
            cand[i] = -1;
        }
        for (int i = pot - 1; i > 0; i--)
        {
            b[i] = b[i * 2];
            e[i] = e[i * 2 + 1];
            lazy[i] = 0;
            quick_update(i);
        }
	}

    void sub(int bq, int eq, int w){
        if (bq > e[w] || b[w] > eq)
            return;
        if (bq <= b[w] && e[w] <= eq){
            mini[w]--;
            lazy[w]--;
            push_lazy(w);
            return;
        }

        push_lazy(w);
        sub(bq, eq, w * 2);
        sub(bq, eq, w * 2 + 1);
        quick_update(w);
    }

    void del(int bq, int eq, int w){
        if (bq > e[w] || b[w] > eq)
            return;
        if (bq <= b[w] && e[w] <= eq){
            mini[w] = inf;
            lmo[w] = -1;
            rmo[w] = -1;
            cand[w] = -1;
            lazy[w] = 0;
            return;
        }
        
        push_lazy(w);
        del(bq, eq, w * 2);
        del(bq, eq, w * 2 + 1);
        quick_update(w);
    }

    int get_big(){
        int curr = cand[1];
        del(curr, curr, 1);

        if (curr - k + 1 >= 0)
            sub(curr - k + 1, curr, 1);
        else{
            sub(0, curr, 1);
            sub(n - k + curr + 1, n - 1, 1);
        }

        return curr;
    }
};

int n;
Tree tree;
vector <int> h;
vector <int> lef[23], rig[23];

void debug(){
    for (int i : lef[1])
		cout << i << " ";
	cout << "\n";
    for (int i : rig[1])
		cout << i << " ";
	cout << "\n";
}

int conv(int a, int b){
    return (a - b + n) % n;
}

void get_dag(int k)
{
    lef[0].resize(n);
    rig[0].resize(n);

    set <pair <int, int> > sett; // h, id
    for (int i = 1; i < k; i++)
        sett.insert({h[i], i});
    
    for (int i = 0; i < n; i++)
    {
        int j = (i + k) % n;
        set <pair <int, int> >::iterator it;
        it = sett.lower_bound(make_pair(h[i], i));
        if (it == sett.begin())
            rig[0][i] = -1;
        else
            rig[0][i] = prev(it)->second; 
        
        it = sett.lower_bound(make_pair(h[j], j));
        if (it == sett.begin())
            lef[0][j] = -1;
        else
            lef[0][j] = prev(it)->second; 

        // --------------------------------------------

        sett.erase(make_pair(h[(i + 1) % n], (i + 1) % n));
        sett.insert({h[j], j});
    }

    for (int step = 1; step <= 20; step++){
        lef[step].resize(n);
        rig[step].resize(n);

        for (int i = 0; i < n; i++){
            lef[step][i] = (lef[step - 1][i] > -1 ? lef[step - 1][lef[step - 1][i]] : -1);
            rig[step][i] = (rig[step - 1][i] > -1 ? rig[step - 1][rig[step - 1][i]] : -1);

            if (conv(lef[step - 1][i], i) < conv(lef[step][i], i))
                lef[step][i] = -1;
            if (conv(rig[step - 1][i], i) > conv(rig[step][i], i))
                rig[step][i] = -1;
        }
    }
}

void init(int k, vector<int> r) 
{
    n = (int)r.size();
	tree.init(k, r);
	h.resize(n);

	for (int i = n; i > 0; i--){
		int curr = tree.get_big();
		h[curr] = i;
	}

    get_dag(k);

	//debug();
	return;
}

bool is_path(int x, int y)
{
    int xl = x;
    for (int step = 20; step >= 0; step--){
        if (lef[step][xl] > -1 && conv(lef[step][xl], x) >= conv(y, x) && (conv(lef[step][xl], x) < conv(xl, x) || xl == x))
            xl = lef[step][xl];
    }
    
    if (xl == y)
        return true;
    
    if (lef[0][xl] > -1)
        if (h[y] < h[xl])
            return true;

    int xr = x;
    for (int step = 20; step >= 0; step--){
        if (rig[step][xr] > -1 && conv(rig[step][xr], x) <= conv(y, x) && conv(rig[step][xr], x) > conv(xr, x))
            xr = rig[step][xr];
    }
    
    if (xr == y)
        return true;
    
    if (rig[0][xr] > -1)
        if (h[y] < h[xr])
            return true;
    return false;
}

int compare_plants(int x, int y) {
    if (is_path(x, y)){
        return 1;
    }
    //return -1;
    if (is_path(y, x)){
        return -1;
    }
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14548 KB Output is correct
2 Correct 8 ms 14668 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14656 KB Output is correct
5 Correct 8 ms 14676 KB Output is correct
6 Correct 78 ms 18424 KB Output is correct
7 Correct 196 ms 23128 KB Output is correct
8 Correct 447 ms 55096 KB Output is correct
9 Correct 547 ms 55116 KB Output is correct
10 Correct 574 ms 55096 KB Output is correct
11 Correct 563 ms 55140 KB Output is correct
12 Correct 626 ms 55092 KB Output is correct
13 Correct 561 ms 55128 KB Output is correct
14 Correct 663 ms 55096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14548 KB Output is correct
2 Correct 10 ms 14656 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14636 KB Output is correct
5 Correct 8 ms 14676 KB Output is correct
6 Correct 13 ms 14940 KB Output is correct
7 Correct 95 ms 18556 KB Output is correct
8 Correct 10 ms 14676 KB Output is correct
9 Correct 11 ms 14900 KB Output is correct
10 Correct 94 ms 18568 KB Output is correct
11 Correct 125 ms 18388 KB Output is correct
12 Correct 159 ms 18508 KB Output is correct
13 Correct 114 ms 18504 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 11 ms 14548 KB Output is correct
2 Correct 10 ms 14656 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14636 KB Output is correct
5 Correct 8 ms 14676 KB Output is correct
6 Correct 13 ms 14940 KB Output is correct
7 Correct 95 ms 18556 KB Output is correct
8 Correct 10 ms 14676 KB Output is correct
9 Correct 11 ms 14900 KB Output is correct
10 Correct 94 ms 18568 KB Output is correct
11 Correct 125 ms 18388 KB Output is correct
12 Correct 159 ms 18508 KB Output is correct
13 Correct 114 ms 18504 KB Output is correct
14 Correct 150 ms 21420 KB Output is correct
15 Correct 1141 ms 58024 KB Output is correct
16 Correct 154 ms 21460 KB Output is correct
17 Correct 1048 ms 58028 KB Output is correct
18 Correct 756 ms 56860 KB Output is correct
19 Correct 986 ms 56844 KB Output is correct
20 Correct 1115 ms 61732 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14660 KB Output is correct
2 Correct 8 ms 14636 KB Output is correct
3 Correct 125 ms 17736 KB Output is correct
4 Correct 836 ms 52132 KB Output is correct
5 Correct 980 ms 52216 KB Output is correct
6 Correct 1030 ms 55512 KB Output is correct
7 Correct 1107 ms 56072 KB Output is correct
8 Correct 1157 ms 60196 KB Output is correct
9 Correct 833 ms 55188 KB Output is correct
10 Correct 768 ms 55280 KB Output is correct
11 Correct 656 ms 55080 KB Output is correct
12 Correct 752 ms 55340 KB Output is correct
13 Correct 698 ms 58084 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14548 KB Output is correct
2 Correct 8 ms 14548 KB Output is correct
3 Correct 9 ms 14580 KB Output is correct
4 Correct 9 ms 14648 KB Output is correct
5 Correct 11 ms 14616 KB Output is correct
6 Correct 11 ms 14696 KB Output is correct
7 Correct 30 ms 15592 KB Output is correct
8 Correct 27 ms 15672 KB Output is correct
9 Correct 30 ms 15548 KB Output is correct
10 Correct 25 ms 15660 KB Output is correct
11 Correct 28 ms 15620 KB Output is correct
12 Correct 29 ms 15568 KB Output is correct
13 Correct 24 ms 15572 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14632 KB Output is correct
2 Correct 10 ms 14640 KB Output is correct
3 Correct 7 ms 14548 KB Output is correct
4 Correct 8 ms 14640 KB Output is correct
5 Correct 12 ms 14836 KB Output is correct
6 Correct 610 ms 54484 KB Output is correct
7 Correct 725 ms 54736 KB Output is correct
8 Correct 659 ms 55356 KB Output is correct
9 Correct 886 ms 59264 KB Output is correct
10 Correct 557 ms 54328 KB Output is correct
11 Correct 627 ms 58740 KB Output is correct
12 Correct 538 ms 54228 KB Output is correct
13 Correct 690 ms 54368 KB Output is correct
14 Correct 729 ms 54668 KB Output is correct
15 Correct 780 ms 55300 KB Output is correct
16 Correct 459 ms 54332 KB Output is correct
17 Correct 540 ms 54484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14548 KB Output is correct
2 Correct 8 ms 14668 KB Output is correct
3 Correct 9 ms 14548 KB Output is correct
4 Correct 8 ms 14656 KB Output is correct
5 Correct 8 ms 14676 KB Output is correct
6 Correct 78 ms 18424 KB Output is correct
7 Correct 196 ms 23128 KB Output is correct
8 Correct 447 ms 55096 KB Output is correct
9 Correct 547 ms 55116 KB Output is correct
10 Correct 574 ms 55096 KB Output is correct
11 Correct 563 ms 55140 KB Output is correct
12 Correct 626 ms 55092 KB Output is correct
13 Correct 561 ms 55128 KB Output is correct
14 Correct 663 ms 55096 KB Output is correct
15 Correct 11 ms 14548 KB Output is correct
16 Correct 10 ms 14656 KB Output is correct
17 Correct 9 ms 14548 KB Output is correct
18 Correct 8 ms 14636 KB Output is correct
19 Correct 8 ms 14676 KB Output is correct
20 Correct 13 ms 14940 KB Output is correct
21 Correct 95 ms 18556 KB Output is correct
22 Correct 10 ms 14676 KB Output is correct
23 Correct 11 ms 14900 KB Output is correct
24 Correct 94 ms 18568 KB Output is correct
25 Correct 125 ms 18388 KB Output is correct
26 Correct 159 ms 18508 KB Output is correct
27 Correct 114 ms 18504 KB Output is correct
28 Correct 150 ms 21420 KB Output is correct
29 Correct 1141 ms 58024 KB Output is correct
30 Correct 154 ms 21460 KB Output is correct
31 Correct 1048 ms 58028 KB Output is correct
32 Correct 756 ms 56860 KB Output is correct
33 Correct 986 ms 56844 KB Output is correct
34 Correct 1115 ms 61732 KB Output is correct
35 Correct 8 ms 14660 KB Output is correct
36 Correct 8 ms 14636 KB Output is correct
37 Correct 125 ms 17736 KB Output is correct
38 Correct 836 ms 52132 KB Output is correct
39 Correct 980 ms 52216 KB Output is correct
40 Correct 1030 ms 55512 KB Output is correct
41 Correct 1107 ms 56072 KB Output is correct
42 Correct 1157 ms 60196 KB Output is correct
43 Correct 833 ms 55188 KB Output is correct
44 Correct 768 ms 55280 KB Output is correct
45 Correct 656 ms 55080 KB Output is correct
46 Correct 752 ms 55340 KB Output is correct
47 Correct 698 ms 58084 KB Output is correct
48 Correct 8 ms 14548 KB Output is correct
49 Correct 8 ms 14548 KB Output is correct
50 Correct 9 ms 14580 KB Output is correct
51 Correct 9 ms 14648 KB Output is correct
52 Correct 11 ms 14616 KB Output is correct
53 Correct 11 ms 14696 KB Output is correct
54 Correct 30 ms 15592 KB Output is correct
55 Correct 27 ms 15672 KB Output is correct
56 Correct 30 ms 15548 KB Output is correct
57 Correct 25 ms 15660 KB Output is correct
58 Correct 28 ms 15620 KB Output is correct
59 Correct 29 ms 15568 KB Output is correct
60 Correct 24 ms 15572 KB Output is correct
61 Correct 114 ms 19520 KB Output is correct
62 Correct 174 ms 23096 KB Output is correct
63 Correct 628 ms 55132 KB Output is correct
64 Correct 780 ms 55264 KB Output is correct
65 Correct 952 ms 55504 KB Output is correct
66 Correct 920 ms 56224 KB Output is correct
67 Correct 970 ms 60156 KB Output is correct
68 Correct 813 ms 55228 KB Output is correct
69 Correct 770 ms 59624 KB Output is correct
70 Correct 846 ms 55084 KB Output is correct
71 Correct 959 ms 55352 KB Output is correct
72 Correct 975 ms 55544 KB Output is correct
73 Correct 894 ms 56088 KB Output is correct
74 Correct 670 ms 55116 KB Output is correct
75 Correct 755 ms 55424 KB Output is correct