답안 #560793

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
560793 2022-05-12T02:42:51 Z hoanghq2004 새 집 (APIO18_new_home) C++14
0 / 100
1901 ms 113940 KB
#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;

const int N = 3e5 + 10;
const int M = 3e8 + 10;

int n, k, q;
set <pair <int, int> > s[N];
int ans[N];
int cnt;
vector <pair <int, int>> dta;

struct SegmentTree {
    int maxi[N * 10];
    int mini[N * 10];
    SegmentTree() {
        memset(maxi, -1, sizeof(maxi));
        memset(mini, 60, sizeof(mini));
    }
    void upd(int id, int L, int R, int i, int val) {
        if (L == R) {
            maxi[id] = val;
            mini[id] = val;
            return;
        }
        int mid = L + R >> 1;
        if (i <= mid) upd(id * 2, L, mid, i, val);
        else upd(id * 2 + 1, mid + 1, R, i, val);
        maxi[id] = max(maxi[id * 2], maxi[id * 2 + 1]);
        mini[id] = min(mini[id * 2], mini[id * 2 + 1]);
    }
    int get_left(int id, int L, int R, int i, int k) {
        if (L >= i || maxi[id] < k) return -1;
        if (L == R) return L;
        int mid = L + R >> 1;
        int ans = get_left(id * 2 + 1, mid + 1, R, i, k);
        if (ans == -1) ans = get_left(id * 2, L, mid, i, k);
        return ans;
    }
    int get_right(int id, int L, int R, int i, int k) {
        if (i >= R || mini[id] > k) return -1;
        if (L == R) return L;
        int mid = L + R >> 1;
        int ans = get_right(id * 2, L, mid, i, k);
        if (ans == -1) ans = get_right(id * 2 + 1, mid + 1, R, i, k);
        return ans;
    }
    void upd(int i, int val) {
        upd(1, 0, dta.size() - 1, i, val);
    }
    int get_left(int x) {
        int i = lower_bound(dta.begin(), dta.end(), make_pair(x, 0)) - dta.begin();
        int id = get_left(1, 0, dta.size() - 1, i, x);
        if (id == -1) return 1e9;
//        assert(dta[id].first != x);
        return dta[id].first;
    }
    int get_right(int x) {
        int i = lower_bound(dta.begin(), dta.end(), make_pair(x, 0)) - dta.begin();
        int id = get_right(1, 0, dta.size() - 1, i, x);
        if (id == -1) return -1e9;
//        assert(dta[id].first != x);
        return dta[id].first;
    }
} S[2];

void add_(int L, int R) {
    if (dta[L].first == dta[R].first) {
        S[0].upd(L, dta[L].first);
        S[1].upd(R, dta[R].first);
        return;
    }
    int mid = dta[L].first + dta[R].first >> 1;
    S[0].upd(L, mid);
    S[1].upd(R, mid + 1);
}

void rev_(int L, int R) {
    S[0].upd(L, -1);
    S[1].upd(R, 1e9);
}

void add(int t, int X, int i) {
    int x = lower_bound(dta.begin(), dta.end(), make_pair(X, i)) - dta.begin();
    cnt -= (s[t].size() == 2);
    auto L = --s[t].upper_bound({X, x});
    auto R = s[t].upper_bound({X, x});
    rev_(L -> second, R -> second);
    add_(L -> second, x);
    add_(x, R -> second);
    s[t].insert({X, x});
}

void rev(int t, int X, int i) {
    int x = lower_bound(dta.begin(), dta.end(), make_pair(X, i)) - dta.begin();
    s[t].erase({X, x});
    auto L = --s[t].upper_bound({X, x});
    auto R = s[t].upper_bound({X, x});
    rev_(L -> second, x);
    rev_(x, R -> second);
    add_(L -> second, R -> second);
    cnt += (s[t].size() == 2);
}

int main() {
//    freopen("test.inp", "r", stdin);
//    freopen("test.out", "w", stdout);
    ios :: sync_with_stdio(0); cin.tie(0);
    vector <tuple <int, int, int, int, int> > events;
    cin >> n >> k >> q;
    for (int i = 1; i <= n; ++i) {
        int x, t, a, b;
        cin >> x >> t >> a >> b;
        x += 1e8 + 5;
        dta.push_back({x, i});
        events.push_back({a, -1, x, t, i});
        events.push_back({b + 1, 1, x, t, i});
    }
    sort(events.begin(), events.end());

    cnt = k;
    vector <tuple <int, int, int> > work;
    for (int i = 1; i <= q; ++i) {
        int x, y;
        cin >> x >> y;
        x += 1e8 + 5;
        dta.push_back({x, 0});
        work.push_back({y, x, i});
    }
    for (int i = 1; i <= k; ++i) {
        s[i].insert({0, 0}), s[i].insert({M, (int)dta.size() - 1});
        int L = 0, R = M;
        dta.push_back({L, 0}), dta.push_back({R, 0});
    }
    sort(work.begin(), work.end());
    sort(dta.begin(), dta.end());
    dta.erase(unique(dta.begin(), dta.end()), dta.end());
    add_(0, (int)dta.size() - 1);
    int i = 0;
    for (auto [y, x, id]: work) {
        while (i < events.size() && get<0>(events[i]) <= y) {
            auto [_, cmd, x, t, _i] = events[i];
            if (cmd == -1) add(t, x, _i);
            else rev(t, x, _i);
            ++i;
        }
        if (cnt) ans[id] = -1;
        else {
            int L = S[0].get_left(x);
            int R = S[1].get_right(x);
            ans[id] = max(x - L, R - x);
        }
    }
    for (int i = 1; i <= q; ++i) cout << ans[i] << '\n';
}

Compilation message

new_home.cpp: In member function 'void SegmentTree::upd(int, int, int, int, int)':
new_home.cpp:35:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   35 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In member function 'int SegmentTree::get_left(int, int, int, int, int)':
new_home.cpp:44:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   44 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In member function 'int SegmentTree::get_right(int, int, int, int, int)':
new_home.cpp:52:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   52 |         int mid = L + R >> 1;
      |                   ~~^~~
new_home.cpp: In function 'void add_(int, int)':
new_home.cpp:82:28: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |     int mid = dta[L].first + dta[R].first >> 1;
new_home.cpp: In function 'int main()':
new_home.cpp:149:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  149 |     for (auto [y, x, id]: work) {
      |               ^
new_home.cpp:150:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<int, int, int, int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |         while (i < events.size() && get<0>(events[i]) <= y) {
      |                ~~^~~~~~~~~~~~~~~
new_home.cpp:151:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |             auto [_, cmd, x, t, _i] = events[i];
      |                  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 61268 KB Output is correct
2 Correct 27 ms 61332 KB Output is correct
3 Correct 27 ms 61260 KB Output is correct
4 Incorrect 26 ms 61388 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 61268 KB Output is correct
2 Correct 27 ms 61332 KB Output is correct
3 Correct 27 ms 61260 KB Output is correct
4 Incorrect 26 ms 61388 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1147 ms 113940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1901 ms 107896 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 61268 KB Output is correct
2 Correct 27 ms 61332 KB Output is correct
3 Correct 27 ms 61260 KB Output is correct
4 Incorrect 26 ms 61388 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 61268 KB Output is correct
2 Correct 27 ms 61332 KB Output is correct
3 Correct 27 ms 61260 KB Output is correct
4 Incorrect 26 ms 61388 KB Output isn't correct
5 Halted 0 ms 0 KB -