답안 #1104395

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1104395 2024-10-23T15:25:46 Z TrendBattles Plahte (COCI17_plahte) C++14
0 / 160
292 ms 41228 KB
#include <bits/stdc++.h>
using namespace std;
using lli = int64_t;

#define INFILE "present.inp"
#define OUTFILE "present.out"

struct Rectangle {
    int a, b, c, d;
    Rectangle(int _a = 0, int _b = 0, int _c = 0, int _d = 0):
        a(_a), b(_b), c(_c), d(_d) {}
};

void compress(vector <int>& DIFF_X, vector <int>& DIFF_Y, vector <Rectangle>& rect, vector <pair <int, int>>& points, vector <int>& color) {
    for (Rectangle& R : rect) {
        DIFF_X.push_back(R.a);
        DIFF_X.push_back(R.c);
        DIFF_Y.push_back(R.b);
        DIFF_Y.push_back(R.d);
    }
    for (pair <int, int>& P : points) {
        DIFF_X.push_back(P.first);
        DIFF_Y.push_back(P.second);
    }
    sort(DIFF_X.begin(), DIFF_X.end());
    sort(DIFF_Y.begin(), DIFF_Y.end());

    DIFF_X.erase(unique(DIFF_X.begin(), DIFF_X.end()), DIFF_X.end());
    DIFF_Y.erase(unique(DIFF_Y.begin(), DIFF_Y.end()), DIFF_Y.end());

    for (Rectangle& R : rect) {
        R.a = lower_bound(DIFF_X.begin(), DIFF_X.end(), R.a) - DIFF_X.begin();
        R.c = lower_bound(DIFF_X.begin(), DIFF_X.end(), R.c) - DIFF_X.begin();

        R.b = lower_bound(DIFF_Y.begin(), DIFF_Y.end(), R.b) - DIFF_Y.begin();
        R.d = lower_bound(DIFF_Y.begin(), DIFF_Y.end(), R.d) - DIFF_Y.begin();
    }

    for (pair <int, int>& P : points) {
        P.first = lower_bound(DIFF_X.begin(), DIFF_X.end(), P.first) - DIFF_X.begin();
        P.second = lower_bound(DIFF_Y.begin(), DIFF_Y.end(), P.second) - DIFF_Y.begin();
    }

    vector <int> diff_color = color;
    sort(diff_color.begin(), diff_color.end());
    diff_color.erase(unique(diff_color.begin(), diff_color.end()), diff_color.end());

    for (int& x : color) {
        x = lower_bound(diff_color.begin(), diff_color.end(), x) - diff_color.begin();
    }
}

struct Events {
    int p_x, p_y, pos;
    Events(int p_x = 0, int p_y = 0, int s = -1):
        p_x(p_x), p_y(p_y), pos(s) {}

    bool operator < (const Events& other) const {
        if (p_x != other.p_x) return p_x < other.p_x;
        if (p_y != other.p_y) return p_y < other.p_y;
        return pos > other.pos;
    }
};

int time_passed = -1;

//int debugging = false;
struct SegmentTree {
    vector <int> node, lazy, last_update, last_lazy;
    int N;
    SegmentTree(int N): N(N), node(N << 2, -1), lazy(N << 2, -1), last_update(N << 2, -1), last_lazy(N << 2, -1) {}

    void Apply(int v, int id) {
        node[v] = id;
        lazy[v] = id;
    }
    void Push(int v) {
        if (lazy[v] == -1) return;

        for (int child : {v << 1, v << 1 | 1}) {
            Apply(child, lazy[v]);

            last_update[child] = last_lazy[child] = last_lazy[v];
        }

        lazy[v] = last_lazy[v] = -1;
    }
    void update_range(int v, int tl, int tr, int l, int r, int id) {
        if (l > r) return;
        if (l == tl and r == tr) {
            Apply(v, id);
            last_update[v] = last_lazy[v] = time_passed;
            return;
        }

        int tm = (tl + tr) >> 1;
        Push(v);
        update_range(v << 1, tl, tm, l, min(r, tm), id);
        update_range(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r, id);

        last_update[v] = max(last_update[v << 1], last_update[v << 1 | 1]);
    }

    void update_range(int l, int r, int id) {
        update_range(1, 0, N - 1, l, r, id);
    }

    int get_val(int pos) {
        int v = 1, l = 0, r = N - 1;
        while (l < r) {
            Push(v);
            int mid = (l + r) >> 1;
            if (pos <= mid) {
                r = mid; v <<= 1;
            } else {
                l = mid + 1; v = v << 1 | 1;
            }
        }
        return node[v];
    }

    int get_latest(int v, int tl, int tr, int l, int r) {
        if (l > r) return -1;

        //if (debugging) cerr << v << ' ' << tl << ' ' << tr << ' ' << l << ' ' << r << '\n';
        if (l == tl and r == tr) return last_update[v];

        Push(v);

        //if (debugging) cerr << "DONE PUSHING\n";
        int tm = (tl + tr) >> 1;

        return max(
            get_latest(v << 1, tl, tm, l, min(r, tm)),
            get_latest(v << 1 | 1, tm + 1, tr, max(l, tm + 1), r)
        );
    }
    int get_latest(int l, int r) {
        return get_latest(1, 0, N - 1, l, r);
    }
};
int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    if (fopen(INFILE, "r")) {
        freopen(INFILE, "r", stdin);
        freopen(OUTFILE, "w", stdout);
    }

    int N, Q; cin >> N >> Q;
    
    vector <Rectangle> rect(N);
    for (Rectangle& R : rect) cin >> R.a >> R.b >> R.c >> R.d;

    vector <pair <int, int>> points(Q);
    vector <int> color;
    for (pair <int, int>& P : points) {
        cin >> P.first >> P.second;
        int x; cin >> x;
        color.push_back(x);
    }

    vector <int> DIFF_X, DIFF_Y;
    compress(DIFF_X, DIFF_Y, rect, points, color);

    vector <Events> events;
    for (int i = 0; i < N; ++i) {
        events.emplace_back(rect[i].c, rect[i].b, i + N);
        events.emplace_back(rect[i].a, rect[i].b, i);
    }
    
    for (int i = 0; i < Q; ++i) {
        pair <int, int>& P = points[i];
        events.emplace_back(P.first, P.second, ~i);
    }

    sort(events.begin(), events.end());

    vector <vector <int>> graph(N);
    vector <int> out_vertex(N, true);
    vector <set <int>> itself(N), for_parent(N);

    const int M = (int) DIFF_X.size(), K = (int) DIFF_Y.size();
    SegmentTree tree(K);

    for (Events& EV : events) {
        time_passed += 1;

        if (EV.pos < 0) {
            EV.pos = ~EV.pos;
            int val = tree.get_val(EV.p_y);

            if (val == -1) continue;

            if (val < N) {
                itself[val].insert(color[EV.pos]);
                continue;
            }

            val -= N;
            if (rect[val].c == EV.p_x) {
                itself[val].insert(color[EV.pos]);
            } else {
                for_parent[val].insert(color[EV.pos]);
            }
            continue;
        }

        if (EV.pos < N) {
            tree.update_range(rect[EV.pos].b, rect[EV.pos].d, EV.pos);
            continue;
        }

        int id = EV.pos - N;

        int val = tree.get_latest(rect[id].b, rect[id].d);
        
        if (events[val].pos >= N and events[val].pos - N != id) {
            graph[id].push_back(events[val].pos - N);
            //cerr << id << ' ' << events[val].pos - N << '\n';
            out_vertex[events[val].pos - N] = false;
        }
        
        tree.update_range(rect[id].b, rect[id].d, EV.pos);
    }

    vector <int> finale(N);
    auto DFS = [&] (auto DFS, int u) -> void {
        for (int v : graph[u]) {
            DFS(DFS, v);


            if ((int) for_parent[v].size() < (int) itself[v].size()) {
                for_parent[v].swap(itself[v]);
            }

            for (int x : itself[v]) for_parent[v].insert(x);
            itself[v].clear();

            if ((int) for_parent[v].size() > (int) itself[u].size()) {
                itself[u].swap(for_parent[v]);
            }

            for (int x : for_parent[v]) itself[u].insert(x);
            for_parent[v].clear();
        }

        finale[u] = (int) itself[u].size();
    }; 
    for (int i = 0; i < N; ++i) {
        if (out_vertex[i]) DFS(DFS, i);
    }

    for (int x : finale) cout << x << '\n';
    return 0;
}

Compilation message

plahte.cpp: In constructor 'SegmentTree::SegmentTree(int)':
plahte.cpp:70:9: warning: 'SegmentTree::N' will be initialized after [-Wreorder]
   70 |     int N;
      |         ^
plahte.cpp:69:18: warning:   'std::vector<int> SegmentTree::node' [-Wreorder]
   69 |     vector <int> node, lazy, last_update, last_lazy;
      |                  ^~~~
plahte.cpp:71:5: warning:   when initialized here [-Wreorder]
   71 |     SegmentTree(int N): N(N), node(N << 2, -1), lazy(N << 2, -1), last_update(N << 2, -1), last_lazy(N << 2, -1) {}
      |     ^~~~~~~~~~~
plahte.cpp: In function 'int main()':
plahte.cpp:182:15: warning: unused variable 'M' [-Wunused-variable]
  182 |     const int M = (int) DIFF_X.size(), K = (int) DIFF_Y.size();
      |               ^
plahte.cpp:145:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  145 |         freopen(INFILE, "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~
plahte.cpp:146:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  146 |         freopen(OUTFILE, "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 81 ms 12412 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 100 ms 15604 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 180 ms 26416 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 292 ms 41228 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 291 ms 41148 KB Output isn't correct
2 Halted 0 ms 0 KB -