답안 #738790

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
738790 2023-05-09T13:47:27 Z GrindMachine Plahte (COCI17_plahte) C++17
0 / 160
436 ms 105300 KB
// Om Namah Shivaya

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template<typename T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define sz(a) a.size()
#define setbits(x) __builtin_popcountll(x)
#define ff first
#define ss second
#define conts continue
#define ceil2(x, y) ((x + y - 1) / (y))
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl

#define rep(i, n) for(int i = 0; i < n; ++i)
#define rep1(i, n) for(int i = 1; i <= n; ++i)
#define rev(i, s, e) for(int i = s; i >= e; --i)
#define trav(i, a) for(auto &i : a)

template<typename T>
void amin(T &a, T b) {
    a = min(a, b);
}

template<typename T>
void amax(T &a, T b) {
    a = max(a, b);
}

#ifdef LOCAL
#include "debug.h"
#else
#define debug(x) 42
#endif

/*

refs:
edi
https://oj.uz/submission/735829


got the main idea right
but couldnt build the tree efficiently
tree can be built using sweepline (ref submission above)

either use sweepline + fenwick
or use sweepline + set

*/

const int MOD = 1e9 + 7;
const int N = 8e4 + 5;
const int inf1 = int(1e9) + 5;
const ll inf2 = ll(1e18) + 5;

template<typename T>
struct fenwick {
    int siz;
    vector<T> tree;

    fenwick(int n) {
        siz = n;
        tree = vector<T>(n + 1);
    }

    int lsb(int x) {
        return x & -x;
    }

    void build(vector<T> &a, int n) {
        for (int i = 1; i <= n; ++i) {
            int par = i + lsb(i);
            tree[i] += a[i];

            if (par <= siz) {
                tree[par] += tree[i];
            }
        }
    }

    void pupd(int i, T v) {
        i++;

        while (i <= siz) {
            tree[i] += v;
            i += lsb(i);
        }
    }

    T sum(int i) {
        i++;

        T res = 0;

        while (i) {
            res += tree[i];
            i -= lsb(i);
        }

        return res;
    }

    T query(int l, int r) {
        if (l > r) return 0;
        T res = sum(r) - sum(l - 1);
        return res;
    }
};

vector<ll> adj[N];
vector<ll> subsiz(N);
set<ll> here[N];

void dfs1(ll u, ll p) {
    subsiz[u] = sz(here[u]);
    trav(v, adj[u]) {
        if (v == p) conts;
        dfs1(v, u);
        subsiz[u] += subsiz[v];
    }
}

vector<ll> ans(N);
set<ll> st[N];

void dfs2(ll u, ll p) {
    ll mx = -1, largest = -1;
    trav(v, adj[u]) {
        if (v == p) conts;
        if (subsiz[v] > mx) {
            mx = subsiz[v];
            largest = v;
        }
    }

    if (largest != -1) {
        dfs2(largest, u);
        swap(st[u], st[largest]);
    }

    trav(v, adj[u]) {
        if (v == p or v == largest) conts;
        dfs2(v, u);

        trav(x, st[v]) {
            st[u].insert(x);
        }

        st[v].clear();
    }

    trav(x, here[u]) {
        st[u].insert(x);
    }

    ans[u] = sz(st[u]);
}

void solve(int test_case)
{
    ll n, m; cin >> n >> m;
    vector<array<ll, 4>> a(n + 5);
    vector<array<ll, 3>> b(m + 5);
    rep1(i, n) rep(j, 4) cin >> a[i][j];
    rep1(i, m) rep(j, 3) cin >> b[i][j];

    vector<ll> ys;
    rep1(i, n) {
        auto [x1, y1, x2, y2] = a[i];
        ys.pb(y1), ys.pb(y2);
    }
    rep1(i, m) {
        auto [x, y, k] = b[i];
        ys.pb(y);
    }

    sort(all(ys));
    ys.resize(unique(all(ys)) - ys.begin());
    ll siz = sz(ys);

    map<ll, vector<pll>> mp;
    rep1(i, n) {
        auto &[x1, y1, x2, y2] = a[i];

        mp[x1].pb({1, i});
        mp[x2].pb({3, i});

        y1 = lower_bound(all(ys), y1) - ys.begin();
        y2 = lower_bound(all(ys), y2) - ys.begin();
    }

    rep1(i, m) {
        auto &[x, y, k] = b[i];
        mp[x].pb({2, i});

        y = lower_bound(all(ys), y) - ys.begin();
    }

    vector<ll> par(n + 5);
    fenwick<ll> fenw(siz + 5);
    set<array<ll, 3>> st;

    for (auto [xx, v] : mp) {
        sort(all(v));

        for (auto [t, i] : v) {
            if (t == 1) {
                auto [x1, y1, x2, y2] = a[i];
                auto it = st.upper_bound({y1 + 1, -1, -1});
                ll p = 0;

                if (it != st.begin()) {
                    it--;
                    auto [y3, y4, id] = *it;
                    if (y1 >= y3 and y1 <= y4) {
                        p = id;
                        st.erase(it);
                        st.insert({y3, y1 - 1, p});
                        st.insert({y1, y2, i});
                        st.insert({y2 + 1, y4, p});
                    }
                }

                if (!p) {
                    st.insert({y1, y2, i});
                }

                par[i] = p;

                p = fenw.query(0, y1);
                // par[i] = p;

                fenw.pupd(y1, -p + i);
                fenw.pupd(y2 + 1, p - i);

                assert(p == par[i]);
            }
            else if (t == 3) {
                auto [x1, y1, x2, y2] = a[i];
                ll p = par[i];

                vector<array<ll, 3>> toerase;
                vector<array<ll, 3>> toinsert;

                auto it = st.lower_bound({y1, -1, -1});
                for (; it != st.end(); ++it) {
                    auto [l, r, id] = *it;
                    if (id != i) break;
                    toerase.pb({l, r, id});
                    if (p) {
                        toinsert.pb({l, r, p});
                    }
                }

                trav(ar, toerase) st.erase(ar);
                trav(ar, toinsert) st.insert(ar);

                fenw.pupd(y1, p - i);
                fenw.pupd(y2 + 1, -p + i);
            }
            else {
                auto [x, y, k] = b[i];
                auto it = st.upper_bound({y + 1, -1, -1});
                ll p = 0;

                if (it != st.begin()) {
                    it--;
                    auto [y3, y4, id] = *it;
                    if (y >= y3 and y <= y4) {
                        p = id;
                    }
                }

                here[p].insert(k);

                // assert(p == fenw.query(0, y));
            }
        }
    }

    rep1(i, n) {
        ll p = par[i];
        adj[p].pb(i);
        // cout << p << " " << i << endl;
    }

    dfs1(0, -1);
    dfs2(0, -1);

    rep1(i, n) cout << ans[i] << endl;
}

int main()
{
    fastio;

    int t = 1;
    // cin >> t;

    rep1(i, t) {
        solve(i);
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 113 ms 44872 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 168 ms 53456 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 276 ms 75520 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 436 ms 105288 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 407 ms 105300 KB Execution killed with signal 6
2 Halted 0 ms 0 KB -