Submission #893932

# Submission time Handle Problem Language Result Execution time Memory
893932 2023-12-27T17:20:08 Z azosi Min-max tree (BOI18_minmaxtree) C++17
0 / 100
173 ms 28028 KB
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 70002;
const int INF = 1e9;
vector<int> g[MAX_N], tree[MAX_N];
int n, k;

struct SegmentTree {
    typedef int (*F)(int, int);

    F merge;
    int identity;
    int tree[MAX_N * 4]{}, lazy[MAX_N * 4]{};

    SegmentTree(F func, int identity) : merge(func), identity(identity) {
        for (int i = 0; i < MAX_N * 4; ++i) tree[i] = lazy[i] = identity;
    }

    void push(int node, int l, int r) {
        if (lazy[node] != identity) {
            tree[node] = merge(tree[node], lazy[node]);
            if (l != r) {
                lazy[node * 2] = merge(lazy[node * 2], lazy[node]);
                lazy[node * 2 + 1] = merge(lazy[node * 2 + 1], lazy[node]);
            }
        }
        lazy[node] = identity;
    }

    void update(int s, int e, int val, int node = 1, int l = 1, int r = n) {
        push(node, l, r);
        if (e < l || r < s) return;
        if (s <= l && r <= e) {
            lazy[node] = val;
            push(node, l, r);
            return;
        }
        int mid = (l + r) / 2;
        update(s, e, val, node * 2, l, mid);
        update(s, e, val, node * 2 + 1, mid + 1, r);
        tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
    }

    int query(int s, int e, int node = 1, int l = 1, int r = n) {
        push(node, l, r);
        if (e < l || r < s) return identity;
        if (s <= l && r <= e) return tree[node];
        int mid = (l + r) / 2;
        return merge(query(s, e, node * 2, l, mid), query(s, e, node * 2 + 1, mid + 1, r));
    }

};

int _mn(int a, int b) {
    return min(a, b);
}

int _mx(int a, int b) {
    return max(a, b);
}

SegmentTree minSeg(_mn, INF);
SegmentTree maxSeg(_mx, -INF);

void makeTree(int here = 1, int prev = 0) {
    for (auto there: g[here]) {
        if (there == prev) continue;
        tree[here].push_back(there);
        makeTree(there, here);
    }
}

int sz[MAX_N], dep[MAX_N], par[MAX_N], top[MAX_N], in[MAX_N];

void dfs1(int here = 1) {
    sz[here] = 1;
    for (auto &there: tree[here]) {
        dep[there] = dep[here] + 1;
        par[there] = here;
        dfs1(there);
        sz[here] += sz[there];
        if (sz[there] > sz[tree[here][0]]) swap(there, tree[here][0]);
    }
}

int counter;


void dfs2(int here = 1) {
    in[here] = ++counter;
    for (auto there: tree[here]) {
        top[there] = there == tree[here][0] ? top[here] : there;
        dfs2(there);
    }
}

void update(int a, int b, int val, bool qType) {
    while (top[a] != top[b]) {
        if (dep[top[a]] < dep[top[b]]) swap(a, b);
        int st = top[a];

        qType ? minSeg.update(in[st], in[a], val) : maxSeg.update(in[st], in[a], val);
        a = par[st];
    }
    if (a == b) return;
    if (dep[a] > dep[b]) swap(a, b);
    qType ? minSeg.update(in[a] + 1, in[b], val) : maxSeg.update(in[a] + 1, in[b], val);
}

pair<int, int> query(int a, int b) {
    pair<int, int> ret = {INF, -INF};
    while (top[a] != top[b]) {
        if (dep[top[a]] < dep[top[b]]) swap(a, b);
        int st = top[a];
        ret.first = min(ret.first, minSeg.query(in[st], in[a]));
        ret.second = max(ret.second, maxSeg.query(in[st], in[a]));
        a = par[st];
    }
    if (a == b) return ret;
    if (dep[a] > dep[b]) swap(a, b);
    ret.first = min(ret.first, minSeg.query(in[a] + 1, in[b]));
    ret.second = max(ret.second, maxSeg.query(in[a] + 1, in[b]));
    return ret;
}

struct Query {
    int type, x, y, z;
};
int mn[MAX_N], mx[MAX_N], cnt[MAX_N], ans[MAX_N], ans2[MAX_N], chk[MAX_N];
vector<int> bench[MAX_N];


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n;

    for (int i = 0; i < n - 1; ++i) {
        int a, b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cin >> k;
    makeTree();
    dfs1();
    dfs2();

    vector<int> zz;
    for (int i = 0; i < k; ++i) {
        char q;
        int x, y, z;
        cin >> q >> x >> y >> z;
        if (q == 'M') {
            update(x, y, z, false);
        } else {
            update(x, y, z, true);
        }
        zz.push_back(z);
    }
    sort(zz.begin(), zz.end());
    map<int, int> idx;
    zz.resize(unique(zz.begin(), zz.end()) - zz.begin());
    for (int i = 0; i < zz.size(); ++i) {
        idx[zz[i]] = i;
    }

    for (int i = 2; i <= n; ++i) {
        auto [mini, maxi] = query(i, par[i]);
        if (mini != INF) {
            mn[i] = idx[mini];
            bench[idx[mini]].push_back(i);
        }
        if (maxi != -INF) {
            mx[i] = idx[maxi];
            bench[idx[maxi]].push_back(i);
        }
    }
    set<pair<int, int>> s;
    for (int i = 0; i < zz.size(); ++i) {
        cnt[i] = bench[i].size();
        s.insert({bench[i].size(), i});
    }
    while (!s.empty()) {
        int id = s.begin()->second;
        s.erase(s.begin());
        chk[id] = 1;
        int v = 0;
        for (auto node: bench[id])
            if (ans[node] == 0) {
                v = node;
                break;
            }
        if (mn[v] != INF && !chk[idx[mn[v]]]) {
            s.erase({cnt[idx[mn[v]]], idx[mn[v]]});
            cnt[idx[mn[v]]]--;
            s.insert({cnt[idx[mn[v]]], idx[mn[v]]});
        }
        if (idx[mx[v]] != -1 && !chk[idx[mx[v]]]) {
            s.erase({cnt[idx[mx[v]]], idx[mx[v]]});
            cnt[idx[mx[v]]]--;
            s.insert({cnt[idx[mx[v]]], idx[mx[v]]});
        }
        ans[v] = zz[id];
    }
    for (int i = 2; i <= n; ++i) {
        cout << i << ' ' << par[i] << ' ';
        if (ans[i] != INF)
            cout << ans[i];
        else {
            cout << mx[i];
        }
        cout << '\n';
    }


}

Compilation message

minmaxtree.cpp: In function 'int main()':
minmaxtree.cpp:167:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  167 |     for (int i = 0; i < zz.size(); ++i) {
      |                     ~~^~~~~~~~~~~
minmaxtree.cpp:183:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  183 |     for (int i = 0; i < zz.size(); ++i) {
      |                     ~~^~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 10744 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 173 ms 28028 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 110 ms 19792 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 10744 KB Output isn't correct
2 Halted 0 ms 0 KB -