Submission #951144

# Submission time Handle Problem Language Result Execution time Memory
951144 2024-03-21T08:44:15 Z quanlt206 Xylophone (JOI18_xylophone) C++17
47 / 100
54 ms 1716 KB
#include "xylophone.h"
#include<bits/stdc++.h>
#define X first
#define Y second
#define all(x) begin(x), end(x)
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define FORD(i, b, a) for(int i = (b); i >= (a); i--)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define mxx max_element
#define mnn min_element
#define SQR(x) (1LL * (x) * (x))
#define MASK(i) (1LL << (i))
#define Point Vector
#define left Left
#define right Right
#define div Div

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;
typedef pair<db, db> pdb;
typedef pair<ld, ld> pld;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> plll;
typedef pair<ll, int> pli;
typedef pair<ll, pii> plii;

template<class A, class B>
    bool maximize(A& x, B y) {
        if (x < y) return x = y, true; else return false;
    }
template<class A, class B>
    bool minimize(A& x, B y) {
        if (x > y) return x = y, true; else return false;
    }
/* END OF TEMPLATE */

const int N = 5007;

int n, b[N], a[N];

int cntQuery = 0;

int qr[N];

void solve_1(int l, int r, int val) {
    if (l >= r) return ;
    // a[r] == 0 : Min, 1 : Max
    if (val == 0) {
        qr[r] = 0;
        int i = r - 1;
        while (i >= l) {
            int tmp = query(i, r);
            a[i] = a[r] + tmp;
            int d = l, c = i, g, j;
            while (d <= c) {
                g = (d + c) >> 1;
                if (query(g, r) == tmp) j = g, c = g - 1; else d = g + 1;
            }
            i = j - 1;
        }
        i = r;
        while (i >= l) {
            int j = i;
            while (a[j - 1] == 0 && j > l) j--;
            solve_1(j, i, 1);
            i = j - 1;
        }
    }
    else {
        qr[r] = 0;
        int i = r - 1;
        while (i >= l) {
            int tmp = query(i, r);
            a[i] = a[r] - tmp;
            int d = l, c = i, g, j;
            while (d <= c) {
                g = (d + c) >> 1;
                if (query(g, r) == tmp) j = g, c = g - 1; else d = g + 1;
            }
            i = j - 1;
        }
        i = r;
        while (i >= l) {
            int j = i;
            while (a[j - 1] == 0 && j > l) j--;
            solve_1(j, i, 0);
            i = j - 1;
        }
    }
}

void solve_2(int l, int r, int val) {
    // a[l] == Min ? Max
    if (l >= r) return ;
    if (val == 0) {
        qr[l] = 0;
        int i = l + 1;
        while (i <= r) {
            int tmp = query(l, i);
            a[i] = a[l] + tmp;
            int d = i, c = r, g, j;
            while (d <= c) {
                g = (d + c) >> 1;
                if (query(l, g) == tmp) j = g, d = g + 1; else c = g - 1;
            }
            i = j + 1;
        }
        i = l;
        while (i <= r) {
            int j = i;
            while (a[j + 1] == 0 && j < r) j++;
            solve_2(i, j, 1);
            i = j + 1;
        }
    }
    else {
        qr[l] = 0;
        int i = l + 1;
        while (i <= r) {
            int tmp = query(l, i);
            a[i] = a[l] - tmp;
            int d = i, c = r, g, j = -1;
            while (d <= c) {
                g = (d + c) >> 1;
                if (query(l, g) == tmp) j = g, d = g + 1; else c = g - 1;
            }
            i = j + 1;
        }
        i = l;
        while (i <= r) {
            int j = i;
            while (a[j + 1] == 0 && j < r) j++;
            solve_2(i, j, 0);
            i = j + 1;
        }
    }
}

void solve(int m) {
    n = m;
    FOR(i, 1, n) a[i] = 0, qr[i] = 0;
    int pos_1 = 0;
    int d = 1, c = n, g;
    while (d <= c) {
        g = (d + c) >> 1;
        if (query(g, n) == n - 1) pos_1 = g, d = g + 1; else c = g - 1;
    }
    FOR(i, 1, n) {
        if (i != pos_1) qr[i] = query(min(i, pos_1), max(i, pos_1));
        if (qr[i] == n - 1) break;
    }
    a[pos_1] = 1;
    qr[pos_1] = 0;
    FORD(i, pos_1 - 1, 1)
        if (qr[i] > qr[i + 1]) a[i] = qr[i] + 1;
    FOR(i, pos_1 + 1, n)
        if (qr[i] > qr[i - 1]) a[i] = qr[i] + 1;
    int i = pos_1;
    vector<pii> left, right;
    while (i > 0) {
        int j = i;
        while (j > 1 && a[j - 1] == 0) j--;
        left.push_back({j, i});
        i = j - 1;
    }
    i = pos_1;
    while (i <= n) {
        int j = i;
        while (j < n && a[j + 1] == 0) j++;
        right.push_back({i, j});
        i = j + 1;
    }
    for (auto x : left) solve_1(x.X, x.Y, 1);
    for (auto x : right) solve_2(x.X, x.Y, 1);
    FOR(i, 1, n) answer(i, a[i]);
}

Compilation message

xylophone.cpp: In function 'void solve_1(int, int, int)':
xylophone.cpp:86:15: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
   86 |             i = j - 1;
      |             ~~^~~~~~~
xylophone.cpp:65:15: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
   65 |             i = j - 1;
      |             ~~^~~~~~~
xylophone.cpp: In function 'void solve_2(int, int, int)':
xylophone.cpp:112:15: warning: 'j' may be used uninitialized in this function [-Wmaybe-uninitialized]
  112 |             i = j + 1;
      |             ~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 5 ms 344 KB Output is correct
17 Correct 10 ms 344 KB Output is correct
18 Correct 16 ms 344 KB Output is correct
19 Correct 18 ms 440 KB Output is correct
20 Correct 21 ms 424 KB Output is correct
21 Correct 17 ms 444 KB Output is correct
22 Correct 22 ms 344 KB Output is correct
23 Correct 5 ms 596 KB Output is correct
24 Correct 36 ms 1472 KB Output is correct
25 Correct 54 ms 1716 KB Output is correct
26 Correct 21 ms 432 KB Output is correct
27 Correct 17 ms 540 KB Output is correct
28 Correct 15 ms 596 KB Output is correct
29 Correct 18 ms 344 KB Output is correct
30 Correct 17 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 2 ms 344 KB Output is correct
5 Correct 3 ms 444 KB Output is correct
6 Correct 2 ms 344 KB Output is correct
7 Correct 2 ms 344 KB Output is correct
8 Correct 2 ms 344 KB Output is correct
9 Correct 1 ms 440 KB Output is correct
10 Correct 3 ms 444 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 2 ms 344 KB Output is correct
13 Correct 2 ms 344 KB Output is correct
14 Correct 2 ms 344 KB Output is correct
15 Correct 2 ms 344 KB Output is correct
16 Correct 5 ms 344 KB Output is correct
17 Correct 10 ms 344 KB Output is correct
18 Correct 16 ms 344 KB Output is correct
19 Correct 18 ms 440 KB Output is correct
20 Correct 21 ms 424 KB Output is correct
21 Correct 17 ms 444 KB Output is correct
22 Correct 22 ms 344 KB Output is correct
23 Correct 5 ms 596 KB Output is correct
24 Correct 36 ms 1472 KB Output is correct
25 Correct 54 ms 1716 KB Output is correct
26 Correct 21 ms 432 KB Output is correct
27 Correct 17 ms 540 KB Output is correct
28 Correct 15 ms 596 KB Output is correct
29 Correct 18 ms 344 KB Output is correct
30 Correct 17 ms 344 KB Output is correct
31 Incorrect 48 ms 344 KB Wrong Answer [2]
32 Halted 0 ms 0 KB -