답안 #141436

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
141436 2019-08-08T05:42:06 Z meatrow Uzastopni (COCI15_uzastopni) C++17
128 / 160
8 ms 1460 KB
//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

const int N = 1e4 + 5;
const int M = 101;

int jojoke[N];
int l[N], r[N];
vector<int> g[N];
int a[M];
pair<int, int> p[N * 2];

void dfs(int v) {
    l[v] = r[v] = jojoke[v];
    for (int u : g[v]) {
        dfs(u);
    }
    fill(a, a + M, 0);
    int sz = 0;
    for (int u : g[v]) {
        if (l[u] <= jojoke[v] && jojoke[v] <= r[u]) {
            if (jojoke[u] < jojoke[v]) {
                p[sz++] = { l[u], 1 };
                p[sz++] = { jojoke[v] + 1, -1 };
            } 
            if (jojoke[u] > jojoke[v]) {
                p[sz++] = { jojoke[v], 1 };
                p[sz++] = { r[u] + 1, -1 };
            }
        } else {
            p[sz++] = { l[u], 1 };
            p[sz++] = { r[u] + 1, -1 }; 
        }
    }
    sort(p, p + sz);
    int b = 0;
    for (int i = 0; i < sz; i++) {
        b += p[i].second;
        if (b > 0) {
            for (int j = p[i].first; j < p[i + 1].first; j++) {
                a[j] = 1;
            }
        }
    }
    for (int i = jojoke[v] + 1; i < M; i++) {
        if (a[i] == 0) {
            break;
        }
        r[v] = i;
    }
    for (int i = jojoke[v] - 1; i > 0; i--) {
        if (a[i] == 0) {
            break;
        }
        l[v] = i;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> jojoke[i];
    }
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
    }
    dfs(1);
    cout << (jojoke[1] - l[1] + 1) * (r[1] - jojoke[1] + 1) << endl;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 504 KB Output isn't correct
2 Correct 2 ms 508 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 504 KB Output is correct
5 Incorrect 2 ms 632 KB Output isn't correct
6 Correct 2 ms 632 KB Output is correct
7 Correct 2 ms 632 KB Output is correct
8 Correct 2 ms 632 KB Output is correct
9 Correct 2 ms 508 KB Output is correct
10 Correct 2 ms 632 KB Output is correct
11 Incorrect 7 ms 888 KB Output isn't correct
12 Incorrect 7 ms 888 KB Output isn't correct
13 Correct 8 ms 808 KB Output is correct
14 Correct 8 ms 1400 KB Output is correct
15 Correct 8 ms 1400 KB Output is correct
16 Correct 8 ms 1460 KB Output is correct
17 Correct 7 ms 888 KB Output is correct
18 Correct 8 ms 888 KB Output is correct
19 Correct 7 ms 760 KB Output is correct
20 Correct 8 ms 760 KB Output is correct