Submission #301494

# Submission time Handle Problem Language Result Execution time Memory
301494 2020-09-18T02:59:44 Z CodePlatina Comparing Plants (IOI20_plants) C++14
100 / 100
1732 ms 85940 KB
#include "plants.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <utility>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<long long, long long>
#define plll pair<long long, pll>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss

using namespace std;

struct Node
{
    pii mn, lazy;
}seg[808080];

void prop(int ind)
{
    Node &x = seg[ind << 1], &y = seg[ind << 1 | 1], &nd = seg[ind];
    if(nd.lazy.ff)
    {
        x.mn.ff += nd.lazy.ff;
        y.mn.ff += nd.lazy.ff;
        x.lazy.ff += nd.lazy.ff;
        y.lazy.ff += nd.lazy.ff;
        nd.lazy.ff = 0;
    }
    if(nd.lazy.ss)
    {
        x.mn.ss += nd.lazy.ss;
        y.mn.ss += nd.lazy.ss;
        x.lazy.ss += nd.lazy.ss;
        y.lazy.ss += nd.lazy.ss;
        nd.lazy.ss = 0;
    }
}

void mrge(int ind)
{
    seg[ind].mn = min(seg[ind << 1].mn, seg[ind << 1 | 1].mn);
}

void init_seg(vector<int> &r, int ind, int s, int e)
{
    if(s + 1 == e)
    {
        seg[ind].mn = {r[s], 0};
        seg[ind].lazy = {0, 0};
        return;
    }

    int mid = s + e >> 1;
    init_seg(r, ind << 1, s, mid);
    init_seg(r, ind << 1 | 1, mid, e);
    mrge(ind);
}

void upd1(int ind, int s, int e, int l, int r, int x)
{
    if(e <= l || r <= s) return;
    if(l <= s && e <= r)
    {
        seg[ind].mn.ff += x;
        seg[ind].lazy.ff += x;
        return;
    }

    prop(ind);
    int mid = s + e >> 1;
    upd1(ind << 1, s, mid, l, r, x);
    upd1(ind << 1 | 1, mid, e, l, r, x);
    mrge(ind);
}

void upd2(int ind, int s, int e, int l, int r, int x)
{
    if(e <= l || r <= s) return;
    if(l <= s && e <= r)
    {
        seg[ind].mn.ss += x;
        seg[ind].lazy.ss += x;
        return;
    }

    prop(ind);
    int mid = s + e >> 1;
    upd2(ind << 1, s, mid, l, r, x);
    upd2(ind << 1 | 1, mid, e, l, r, x);
    mrge(ind);
}

void qr1(vector<int> &ret, int ind, int s, int e, int l, int r)
{
    if(e <= l || r <= s || seg[ind].mn.ff) return;
    if(s + 1 == e)
    {
        ret.push_back(s);
        return;
    }

    prop(ind);
    int mid = s + e >> 1;
    qr1(ret, ind << 1, s, mid, l, r);
    qr1(ret, ind << 1 | 1, mid, e, l, r);
}

int qr2(int ind, int s, int e)
{
    if(s + 1 == e) return s;

    prop(ind);
    int mid = s + e >> 1;
    if(seg[ind << 1].mn == pii{0, 0}) return qr2(ind << 1, s, mid);
    else return qr2(ind << 1 | 1, mid, e);
}

int N, K;
int ord[404040];
int rev[202020];
int ils[1616161];
int igr[1616161];
int sls[404040][20];
int sgr[404040][20];

void init3(int ind, int s, int e)
{
    if(s + 1 == e)
    {
        ils[ind] = ord[s];
        return;
    }

    int mid = s + e >> 1;
    init3(ind << 1, s, mid);
    init3(ind << 1 | 1, mid, e);
    ils[ind] = max(ils[ind << 1], ils[ind << 1 | 1]);
}

void init4(int ind, int s, int e)
{
    if(s + 1 == e)
    {
        igr[ind] = ord[s];
        return;
    }

    int mid = s + e >> 1;
    init4(ind << 1, s, mid);
    init4(ind << 1 | 1, mid, e);
    igr[ind] = min(igr[ind << 1], igr[ind << 1 | 1]);
}

void upd3(int ind, int s, int e, int l, int r, int x)
{
    if(e <= l || r <= s || ils[ind] <= ord[x]) return;
    if(s + 1 == e)
    {
        sls[s][0] = x;
        ils[ind] = -1;
        return;
    }

    int mid = s + e >> 1;
    upd3(ind << 1, s, mid, l, r, x);
    upd3(ind << 1 | 1, mid, e, l, r, x);
    ils[ind] = max(ils[ind << 1], ils[ind << 1 | 1]);
}

void upd4(int ind, int s, int e, int l, int r, int x)
{
    if(e <= l || r <= s || igr[ind] >= ord[x]) return;
    if(s + 1 == e)
    {
        sgr[s][0] = x;
        igr[ind] = (int)1e9;
        return;
    }

    int mid = s + e >> 1;
    upd4(ind << 1, s, mid, l, r, x);
    upd4(ind << 1 | 1, mid, e, l, r, x);
    igr[ind] = min(igr[ind << 1], igr[ind << 1 | 1]);
}

void init(int k, vector<int> r)
{
	N = r.size(); K = k;
	init_seg(r, 1, 0, N);
	for(int i = 0; i <= N - K; ++i) if(!r[i]) upd2(1, 0, N, i + 1, i + K, 1);
	for(int i = N - K + 1; i < N; ++i) if(!r[i]) upd2(1, 0, N, i + 1, N, 1), upd2(1, 0, N, 0, K - N + i, 1);

	for(int i = 0; i < N; ++i)
    {
        int t = qr2(1, 0, N);
        ord[t] = i;

        vector<int> ls;

        upd1(1, 0, N, t, t + 1, N);

        if(t >= K - 1)
        {
            upd1(1, 0, N, t - K + 1, t, -1);
            qr1(ls, 1, 0, N, t - K + 1, t);
        }
        else
        {
            upd1(1, 0, N, 0, t, -1);
            upd1(1, 0, N, N - K + t + 1, N, -1);
            qr1(ls, 1, 0, N, 0, t);
            qr1(ls, 1, 0, N, N - K + t + 1, N);
        }

        if(t <= N - K) upd2(1, 0, N, t + 1, t + K, -1);
        else
        {
            upd2(1, 0, N, t + 1, N, -1);
            upd2(1, 0, N, 0, K - N + t, -1);
        }

        for(int j : ls)
        {
            if(j <= N - K) upd2(1, 0, N, j + 1, j + K, 1);
            else
            {
                upd2(1, 0, N, j + 1, N, 1);
                upd2(1, 0, N, 0, K - N + j, 1);
            }
        }
    }

    for(int i = 0; i < N; ++i) rev[ord[i]] = i;
    for(int i = 0; i < N; ++i) ord[i + N] = ord[i];
    init3(1, 0, 2 * N);
    init4(1, 0, 2 * N);
    for(int i = 0; i <= 2 * N; ++i) sls[i][0] = sgr[i][0] = 2 * N;
    for(int i = N - 1; i >= 0; --i)
    {
        upd3(1, 0, 2 * N, rev[i] - K + 1, rev[i], rev[i]);
        upd3(1, 0, 2 * N, rev[i] + N - K + 1, rev[i] + N, rev[i] + N);
    }
    for(int i = 0; i < N; ++i)
    {
        upd4(1, 0, 2 * N, rev[i] - K + 1, rev[i], rev[i]);
        upd4(1, 0, 2 * N, rev[i] + N - K + 1, rev[i] + N, rev[i] + N);
    }
    for(int i = 1; i < 20; ++i) for(int j = 0; j <= 2 * N; ++j)
        sls[j][i] = sls[sls[j][i - 1]][i - 1], sgr[j][i] = sgr[sgr[j][i - 1]][i - 1];
}

int compare_plants(int x, int y)
{
    if(ord[x] < ord[y])
    {
        int tmp = x;
        for(int i = 19; i >= 0; --i) if(sgr[tmp][i] + K - 1 < y) tmp = sgr[tmp][i];
        if(tmp + K - 1 < y) tmp = sgr[tmp][0];
        if(tmp != 2 * N && ord[tmp] < ord[y]) return 1;

        tmp = y; x += N;
        for(int i = 19; i >= 0; --i) if(sls[tmp][i] + K - 1 < x) tmp = sls[tmp][i];
        if(tmp + K - 1 < x) tmp = sls[tmp][0];
        if(tmp != 2 * N && ord[tmp] > ord[x]) return 1;
    }
    else
    {
        int tmp = x;
        for(int i = 19; i >= 0; --i) if(sls[tmp][i] + K - 1 < y) tmp = sls[tmp][i];
        if(tmp + K - 1 < y) tmp = sls[tmp][0];
        if(tmp != 2 * N && ord[tmp] > ord[y]) return -1;

        tmp = y; x += N;
        for(int i = 19; i >= 0; --i) if(sgr[tmp][i] + K - 1 < x) tmp = sgr[tmp][i];
        if(tmp + K - 1 < x) tmp = sgr[tmp][0];
        if(tmp != 2 * N && ord[tmp] < ord[x]) return -1;
    }
    return 0;
}

Compilation message

plants.cpp: In function 'void init_seg(std::vector<int>&, int, int, int)':
plants.cpp:58:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   58 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void upd1(int, int, int, int, int, int)':
plants.cpp:75:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   75 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void upd2(int, int, int, int, int, int)':
plants.cpp:92:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   92 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void qr1(std::vector<int>&, int, int, int, int, int)':
plants.cpp:108:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  108 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'int qr2(int, int, int)':
plants.cpp:118:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  118 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void init3(int, int, int)':
plants.cpp:139:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  139 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void init4(int, int, int)':
plants.cpp:153:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  153 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void upd3(int, int, int, int, int, int)':
plants.cpp:169:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  169 |     int mid = s + e >> 1;
      |               ~~^~~
plants.cpp: In function 'void upd4(int, int, int, int, int, int)':
plants.cpp:185:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  185 |     int mid = s + e >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 95 ms 3192 KB Output is correct
7 Correct 169 ms 11896 KB Output is correct
8 Correct 822 ms 85752 KB Output is correct
9 Correct 853 ms 85624 KB Output is correct
10 Correct 874 ms 85752 KB Output is correct
11 Correct 894 ms 85724 KB Output is correct
12 Correct 888 ms 85912 KB Output is correct
13 Correct 882 ms 85940 KB Output is correct
14 Correct 917 ms 85728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 6 ms 768 KB Output is correct
7 Correct 112 ms 5368 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 6 ms 768 KB Output is correct
10 Correct 112 ms 5368 KB Output is correct
11 Correct 108 ms 5368 KB Output is correct
12 Correct 111 ms 5496 KB Output is correct
13 Correct 120 ms 5368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 6 ms 768 KB Output is correct
7 Correct 112 ms 5368 KB Output is correct
8 Correct 4 ms 512 KB Output is correct
9 Correct 6 ms 768 KB Output is correct
10 Correct 112 ms 5368 KB Output is correct
11 Correct 108 ms 5368 KB Output is correct
12 Correct 111 ms 5496 KB Output is correct
13 Correct 120 ms 5368 KB Output is correct
14 Correct 204 ms 11896 KB Output is correct
15 Correct 1732 ms 85752 KB Output is correct
16 Correct 215 ms 11896 KB Output is correct
17 Correct 1720 ms 85752 KB Output is correct
18 Correct 1024 ms 85728 KB Output is correct
19 Correct 1014 ms 85804 KB Output is correct
20 Correct 1574 ms 85756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 105 ms 3960 KB Output is correct
4 Correct 1102 ms 85880 KB Output is correct
5 Correct 1241 ms 85756 KB Output is correct
6 Correct 1535 ms 85752 KB Output is correct
7 Correct 1700 ms 85624 KB Output is correct
8 Correct 1717 ms 85752 KB Output is correct
9 Correct 1141 ms 85752 KB Output is correct
10 Correct 1059 ms 85880 KB Output is correct
11 Correct 877 ms 85752 KB Output is correct
12 Correct 967 ms 85880 KB Output is correct
13 Correct 1024 ms 85880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 24 ms 1152 KB Output is correct
8 Correct 23 ms 1152 KB Output is correct
9 Correct 24 ms 1280 KB Output is correct
10 Correct 21 ms 1152 KB Output is correct
11 Correct 23 ms 1152 KB Output is correct
12 Correct 23 ms 1152 KB Output is correct
13 Correct 21 ms 1152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
5 Correct 4 ms 768 KB Output is correct
6 Correct 1125 ms 85752 KB Output is correct
7 Correct 1418 ms 85752 KB Output is correct
8 Correct 1577 ms 85752 KB Output is correct
9 Correct 1681 ms 85880 KB Output is correct
10 Correct 1045 ms 85624 KB Output is correct
11 Correct 1344 ms 85752 KB Output is correct
12 Correct 989 ms 85752 KB Output is correct
13 Correct 1159 ms 85820 KB Output is correct
14 Correct 1456 ms 85752 KB Output is correct
15 Correct 1593 ms 85880 KB Output is correct
16 Correct 964 ms 85752 KB Output is correct
17 Correct 1058 ms 85880 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 95 ms 3192 KB Output is correct
7 Correct 169 ms 11896 KB Output is correct
8 Correct 822 ms 85752 KB Output is correct
9 Correct 853 ms 85624 KB Output is correct
10 Correct 874 ms 85752 KB Output is correct
11 Correct 894 ms 85724 KB Output is correct
12 Correct 888 ms 85912 KB Output is correct
13 Correct 882 ms 85940 KB Output is correct
14 Correct 917 ms 85728 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 6 ms 768 KB Output is correct
21 Correct 112 ms 5368 KB Output is correct
22 Correct 4 ms 512 KB Output is correct
23 Correct 6 ms 768 KB Output is correct
24 Correct 112 ms 5368 KB Output is correct
25 Correct 108 ms 5368 KB Output is correct
26 Correct 111 ms 5496 KB Output is correct
27 Correct 120 ms 5368 KB Output is correct
28 Correct 204 ms 11896 KB Output is correct
29 Correct 1732 ms 85752 KB Output is correct
30 Correct 215 ms 11896 KB Output is correct
31 Correct 1720 ms 85752 KB Output is correct
32 Correct 1024 ms 85728 KB Output is correct
33 Correct 1014 ms 85804 KB Output is correct
34 Correct 1574 ms 85756 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 105 ms 3960 KB Output is correct
38 Correct 1102 ms 85880 KB Output is correct
39 Correct 1241 ms 85756 KB Output is correct
40 Correct 1535 ms 85752 KB Output is correct
41 Correct 1700 ms 85624 KB Output is correct
42 Correct 1717 ms 85752 KB Output is correct
43 Correct 1141 ms 85752 KB Output is correct
44 Correct 1059 ms 85880 KB Output is correct
45 Correct 877 ms 85752 KB Output is correct
46 Correct 967 ms 85880 KB Output is correct
47 Correct 1024 ms 85880 KB Output is correct
48 Correct 1 ms 384 KB Output is correct
49 Correct 1 ms 384 KB Output is correct
50 Correct 1 ms 384 KB Output is correct
51 Correct 1 ms 384 KB Output is correct
52 Correct 1 ms 384 KB Output is correct
53 Correct 3 ms 512 KB Output is correct
54 Correct 24 ms 1152 KB Output is correct
55 Correct 23 ms 1152 KB Output is correct
56 Correct 24 ms 1280 KB Output is correct
57 Correct 21 ms 1152 KB Output is correct
58 Correct 23 ms 1152 KB Output is correct
59 Correct 23 ms 1152 KB Output is correct
60 Correct 21 ms 1152 KB Output is correct
61 Correct 109 ms 3960 KB Output is correct
62 Correct 185 ms 11796 KB Output is correct
63 Correct 985 ms 85724 KB Output is correct
64 Correct 1189 ms 85752 KB Output is correct
65 Correct 1521 ms 85752 KB Output is correct
66 Correct 1654 ms 85656 KB Output is correct
67 Correct 1730 ms 85764 KB Output is correct
68 Correct 1123 ms 85880 KB Output is correct
69 Correct 1383 ms 85756 KB Output is correct
70 Correct 1082 ms 85624 KB Output is correct
71 Correct 1241 ms 85624 KB Output is correct
72 Correct 1574 ms 85752 KB Output is correct
73 Correct 1651 ms 85884 KB Output is correct
74 Correct 1012 ms 85824 KB Output is correct
75 Correct 1069 ms 85624 KB Output is correct