답안 #738714

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
738714 2023-05-09T12:56:39 Z GrindMachine Plahte (COCI17_plahte) C++17
64 / 160
2000 ms 56428 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)

*/

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

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

vector<ll> ans(N);
set<ll> here[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 + 1].pb({2, 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({3, i});

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

    vector<ll> par(n + 5);
    fenwick<ll> fenw(siz + 5);

    for (auto [xx, v] : mp) {
        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 == 2) {
                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 127 ms 23824 KB Output is correct
2 Correct 115 ms 24912 KB Output is correct
3 Incorrect 5 ms 10988 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 119 ms 27472 KB Output is correct
2 Correct 136 ms 27812 KB Output is correct
3 Correct 6 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 223 ms 39652 KB Output is correct
2 Correct 213 ms 37316 KB Output is correct
3 Correct 5 ms 10964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 394 ms 56428 KB Output is correct
2 Correct 393 ms 52616 KB Output is correct
3 Execution timed out 2058 ms 10980 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 473 ms 54984 KB Output is correct
2 Correct 428 ms 49716 KB Output is correct
3 Incorrect 6 ms 10964 KB Output isn't correct
4 Halted 0 ms 0 KB -