답안 #585533

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
585533 2022-06-29T04:24:02 Z Tien_Noob Palembang Bridges (APIO15_bridge) C++17
100 / 100
585 ms 32928 KB
//Make CSP great again
//Vengeance
#include <bits/stdc++.h>
#define TASK "TESTCODE"
#define Log2(x) 31 - __builtin_clz(x)
using namespace std;
const int N = 2e5;
int k, n;
long long add, res = 1e18;
set<int> pos;
multiset<pair<int, int> > L, mid, R;
long long sum[3];
map<int, int> comp;
long long t[N + 1];
int cnt[2];
struct FenwickTree
{
    long long tree[N + 1];
    void add(int pos, int val)
    {
        for (; pos <= N; pos += (pos & -pos))
        {
            tree[pos] += val;
        }
    }
    long long sum(int pos)
    {
        long long ret = 0;
        for (; pos > 0; pos -= (pos & -pos))
        {
            ret += tree[pos];
        }
        return ret;
    }
    long long sum(int l, int r)
    {
        return sum(r) - sum(l - 1);
    }
} tree[4];
void read()
{
    cin >> k >> n;
    while(n--)
    {
        char P, Q;
        int s, t;
        cin >> P >> s >> Q >> t;
        if (P == Q)
        {
            add += abs(s - t);
            continue;
        }
        pos.insert(s);
        pos.insert(t);
        if (s > t)
        {
            swap(s, t);
        }
        L.insert({s, t});
        //R.insert({t, s});
        ++cnt[1];
        sum[1] += s + t;
        ++add;
    }
}
vector<pair<int, int> > point;
long long Get(int i, int sz)
{
    int low = 1, high = N;
    while(low <= high)
    {
        int mid = (low + high)/2;
        if (tree[i].sum(mid) >= sz)
        {
            high = mid - 1;
        }
        else
        {
            low = mid + 1;
        }
    }
    long long ret = t[low] * tree[i].sum(low - 1) - tree[i + 1].sum(low - 1) + tree[i + 1].sum(low + 1, N) - tree[i].sum(low + 1, N) * t[low];
    assert(ret >= 0);
    //cerr << t[low] << ' ' << ret << ' ';
    return ret;
}
void solve()
{
    if (pos.empty())
    {
        cout << add;
        return ;
    }
    if (k == 1)
    {
        for (int x : pos)
        {
            while(!L.empty() && L.begin()->first <= x)
            {
                sum[2] += L.begin()->second - L.begin()->first;
                --cnt[1];
                sum[1] -= L.begin()->first + L.begin()->second;
                mid.insert({L.begin()->second, L.begin()->first});
                L.erase(L.begin());
            }
            while(!mid.empty() && mid.begin()->first <= x)
            {
                ++cnt[0];
                sum[0] += mid.begin()->first + mid.begin()->second;
                sum[2] -= mid.begin()->first - mid.begin()->second;
                mid.erase(mid.begin());
            }
            res = min(res, 2LL * x * cnt[0] - sum[0] + sum[1] - 2LL * cnt[1] * x + sum[2]);
        }
        cout << res + add;
        return ;
    }
    int j = 0;
    for (auto &p : pos)
    {
        comp[p] = ++j;
        t[j] = p;
    }
    for (auto &p : L)
    {
        point.emplace_back(p.first, p.second);
        tree[2].add(comp[p.first], 1);
        tree[2].add(comp[p.second], 1);
        tree[3].add(comp[p.first], p.first);
        tree[3].add(comp[p.second], p.second);
    }
    sort(point.begin(), point.end(), [&](auto &x, auto &y)
         {
             return x.first + x.second < y.first + y.second;
         });
    for (int i = 0; i < point.size(); ++ i)
    {
        res = min(res, Get(0, i) + Get(2, point.size() - i));
        tree[2].add(comp[point[i].first], -1);
        tree[2].add(comp[point[i].second], -1);
        tree[3].add(comp[point[i].first], -point[i].first);
        tree[3].add(comp[point[i].second], -point[i].second);

        tree[0].add(comp[point[i].first], 1);
        tree[0].add(comp[point[i].second], 1);
        tree[1].add(comp[point[i].first], point[i].first);
        tree[1].add(comp[point[i].second], point[i].second);
        //cerr << '\n';
    }
    cout << res + add;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        //freopen(TASK".OUT", "w", stdout);
    }
    int t = 1;
    bool typetest = false;
    if (typetest)
    {
        cin >> t;
    }
    for (int __ = 1; __ <= t; ++ __)
    {
        //cout << "Case " << __ << ": ";
        read();
        solve();
    }
}

Compilation message

bridge.cpp: In function 'void solve()':
bridge.cpp:136: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]
  136 |     for (int i = 0; i < point.size(); ++ i)
      |                     ~~^~~~~~~~~~~~~~
bridge.cpp: In function 'int main()':
bridge.cpp:158:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 57 ms 4980 KB Output is correct
13 Correct 208 ms 14368 KB Output is correct
14 Correct 85 ms 4940 KB Output is correct
15 Correct 113 ms 8612 KB Output is correct
16 Correct 70 ms 4916 KB Output is correct
17 Correct 88 ms 14296 KB Output is correct
18 Correct 109 ms 14412 KB Output is correct
19 Correct 118 ms 13968 KB Output is correct
20 Correct 54 ms 4960 KB Output is correct
21 Correct 96 ms 14408 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 232 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 500 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 468 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 3 ms 724 KB Output is correct
21 Correct 3 ms 724 KB Output is correct
22 Correct 3 ms 724 KB Output is correct
23 Correct 2 ms 468 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 1 ms 468 KB Output is correct
9 Correct 1 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 0 ms 468 KB Output is correct
12 Correct 1 ms 468 KB Output is correct
13 Correct 2 ms 468 KB Output is correct
14 Correct 2 ms 588 KB Output is correct
15 Correct 3 ms 724 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 2 ms 468 KB Output is correct
18 Correct 2 ms 596 KB Output is correct
19 Correct 2 ms 468 KB Output is correct
20 Correct 3 ms 780 KB Output is correct
21 Correct 2 ms 724 KB Output is correct
22 Correct 3 ms 724 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 3 ms 724 KB Output is correct
25 Correct 91 ms 6192 KB Output is correct
26 Correct 134 ms 6200 KB Output is correct
27 Correct 585 ms 30248 KB Output is correct
28 Correct 579 ms 32928 KB Output is correct
29 Correct 584 ms 32764 KB Output is correct
30 Correct 237 ms 17560 KB Output is correct
31 Correct 94 ms 6484 KB Output is correct
32 Correct 328 ms 32828 KB Output is correct
33 Correct 315 ms 32780 KB Output is correct
34 Correct 379 ms 31596 KB Output is correct
35 Correct 96 ms 6468 KB Output is correct
36 Correct 338 ms 32760 KB Output is correct
37 Correct 103 ms 6336 KB Output is correct