답안 #470380

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
470380 2021-09-03T16:02:26 Z hoanghq2004 Building Bridges (CEOI17_building) C++14
0 / 100
92 ms 5904 KB
#include <bits/stdc++.h>

using namespace std;

int n, k, m;
pair <int, int> a[100010], s[100010];

int cmp(pair <int, int> a, pair <int, int> b) {
    return a.first + a.second < b.first + b.second;
}

long long res = 1e18, sum;

struct node {
    int num;
    long long val;
} seg[2][800010];

void update(int type, int id, int l, int r, int i, int val) {
    if (i > r || i < l) return;
    if (l == r) {
        if (val == -1) seg[type][id] = {0, 0};
        else seg[type][id] = {1, val};
        return;
    }
    int mid = l + r >> 1;
    update(type, id * 2, l, mid, i, val);
    update(type, id * 2 + 1, mid + 1, r, i, val);
    seg[type][id].val = seg[type][id * 2].val + seg[type][id * 2 + 1].val;
    seg[type][id].num = seg[type][id * 2].num + seg[type][id * 2 + 1].num;
}

long long curl, curr;

long long get(int type, int id, int l, int r, int k, int pos) {
    if (pos == 0) return 0;
    if (l == r) {
        return (seg[type][id].val * (pos - 1) - curl) + (curr - seg[type][id].val * (seg[type][1].num - pos));
    }
    int mid = l + r >> 1;
    if (seg[type][id * 2].num < k) {
        curl += seg[type][id * 2].val;
        return get(type, id * 2 + 1, mid + 1, r, k - seg[type][id * 2].num, pos);
    }
    else {
        curr += seg[type][id * 2 + 1].val;
        return get(type, id * 2, l, mid, k, pos);
    }
}

void compress() {
    vector <pair <int, int> > data;
    for (int i = 1; i <= n; ++i) {
        data.push_back({a[i].first, i});
        data.push_back({a[i].second, i + n});
    }
    sort(data.begin(), data.end());
    for (int i = 0; i < data.size(); ++i) {
        if (data[i].second <= n) s[data[i].second].first = i + 1;
        else s[data[i].second - n].second = i + 1;
    }
}

int main() {
    ios_base :: sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    cin >> k >> m;
    for (int i = 1; i <= m; ++i) {
        char c1, c2;
        int x1, x2;
        cin >> c1 >> x1 >> c2 >> x2;
        if (c1 == c2) {
            sum += abs(x1 - x2);
            continue;
        }
        a[++n] = {x1, x2};
    }
    sort(a + 1, a + n + 1, cmp);
    compress();
    for (int i = 1; i <= n; ++i) {
        update(0, 1, 1, n * 2, s[i].first, a[i].first);
        update(0, 1, 1, n * 2, s[i].second, a[i].second);
    }
    if (k == 1) {
        curl = 0, curr = 0;
        cout << get(0, 1, 1, n * 2, (2 * n + 1) / 2, (2 * n + 1) / 2) + sum + n << "\n";
        exit(0);
    }
    res = get(0, 1, 1, n * 2, (2 * n + 1) / 2, (2 * n + 1) / 2) + sum + n;
    for (int i = 1; i <= n; ++i) {
        curl = curr = 0;
        update(1, 1, 1, n * 2, s[i].first, a[i].first);
        update(1, 1, 1, n * 2, s[i].second, a[i].second);
        update(0, 1, 1, n * 2, s[i].first, -1);
        update(0, 1, 1, n * 2, s[i].second, -1);
        long long tmp0 = get(0, 1, 1, n * 2, (seg[0][1].num + 1) / 2, (seg[0][1].num + 1) / 2);
        curl = curr = 0;
        long long tmp1 = get(1, 1, 1, n * 2, (seg[1][1].num + 1) / 2, (seg[1][1].num + 1) / 2);
        res = min(res, tmp0 + tmp1 + sum + n);
    }
    cout << res;
}

Compilation message

building.cpp: In function 'void update(int, int, int, int, int, int)':
building.cpp:26:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   26 |     int mid = l + r >> 1;
      |               ~~^~~
building.cpp: In function 'long long int get(int, int, int, int, int, int)':
building.cpp:40:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   40 |     int mid = l + r >> 1;
      |               ~~^~~
building.cpp: In function 'void compress()':
building.cpp:58:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     for (int i = 0; i < data.size(); ++i) {
      |                     ~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 588 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 92 ms 5904 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 588 KB Output isn't correct
2 Halted 0 ms 0 KB -