답안 #738931

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
738931 2023-05-09T16:38:36 Z GrindMachine Plahte (COCI17_plahte) C++17
160 / 160
433 ms 61144 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)

sweepline + fenwick
tried sweepline + set, but couldnt get it to work (even after some intense debugging)

*/

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);

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

        for (auto [t, i] : v) {
            if (t == 1) {
                auto [x1, y1, x2, y2] = a[i];
                ll p = fenw.query(0, y1);
                par[i] = p;

                fenw.pupd(y1, -p + i);
                fenw.pupd(y2 + 1, p - i);
            }
            else if (t == 3) {
                auto [x1, y1, x2, y2] = a[i];
                ll p = par[i];

                fenw.pupd(y1, p - i);
                fenw.pupd(y2 + 1, -p + i);
            }
            else {
                auto [x, y, k] = b[i];
                ll p = fenw.query(0, y);
                if (p) {
                    here[p].insert(k);
                    // cout << p << " " << i << endl;
                }
            }
        }
    }

    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 Correct 98 ms 25416 KB Output is correct
2 Correct 99 ms 24888 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 117 ms 29192 KB Output is correct
2 Correct 126 ms 27804 KB Output is correct
3 Correct 6 ms 10920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 224 ms 42564 KB Output is correct
2 Correct 211 ms 37604 KB Output is correct
3 Correct 5 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 410 ms 61144 KB Output is correct
2 Correct 403 ms 54620 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 403 ms 59484 KB Output is correct
2 Correct 433 ms 51824 KB Output is correct
3 Correct 6 ms 10984 KB Output is correct