답안 #263304

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
263304 2020-08-13T15:24:42 Z Kastanda Palembang Bridges (APIO15_bridge) C++11
100 / 100
1713 ms 28996 KB
// M
#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int N = 100905 * 2;
int n, k, ID[N];
ll dp[N], FA[N], FB[N], FC[N];
pair < ll , ll > FL[N], FR[N];
pair < int , int > A[N];
vector < int > U, V, S[N], E[N];
inline void AddFen(int i, int a, int b, int c)
{
        for (i += 10; i < N; i += i & - i)
                FA[i] += a, FB[i] += b, FC[i] += c;
}
inline void AddFen(int l, int r, int a, int b, int c)
{
        AddFen(l, a, b, c);
        AddFen(r, -a, -b, -c);
}
inline vector < ll > GetFen(int i)
{
        vector < ll > rt = {0, 0, 0};
        for (i += 10; i; i -= i & - i)
                rt[0] += FA[i], rt[1] += FB[i], rt[2] += FC[i];
        return rt;
}
inline void Add(int i, int val)
{
        // [0, ID[i]] , -U[A[i]].y   ,  .x   , .y
        AddFen(-1, ID[i] + 1, -U[A[i].y] * val, 0, 1 * val);
        // [ID[i] + 1, N) U[A[i].x]
        AddFen(ID[i] + 1, N, U[A[i].x] * val, -1 * val, 0);
}
inline ll Query(int a, int b)
{
        ll rt = 0;
        rt += FL[a].x + FL[a].y * (ll)U[a];
        rt += FR[b].x + FR[b].y * (ll)U[b];

        int lb = (int)(lower_bound(V.begin(), V.end(), U[a] + U[b]) - V.begin());
        vector < ll > tmp = GetFen(lb);
        rt += tmp[0];
        rt += tmp[1] * (ll)U[a];
        rt += tmp[2] * (ll)U[b];

        return rt;
}
void Solve(int l, int r, int le, int ri)
{
        if (r < l)
                return ;
        int md = (l + r) >> 1, opt = -1;
        // [le, l) is added ...
        // Extending from right to [le, md]
        for (int i = l; i <= md; i ++)
                for (int j : E[i])
                        if (A[j].x >= le) // Should be added
                                Add(j, 1);
        // [le, md] is added, time for querying ..
        dp[md] = (ll)(1e18);
        for (int i = le; i <= min(ri, md); i ++)
        {
                ll val = Query(i, md);
                if (dp[md] >= val)
                        dp[md] = val, opt = i;
                // Shrinking from left to [i + 1, md]
                for (int j : S[i])
                        if (A[j].y <= md) // Should be deleted
                                Add(j, -1);
        }
        // Now we have [min(ri, md) + 1, md]
        // Extending from left to [opt, md]
        for (int i = min(ri, md); i >= opt; i --)
                for (int j : S[i])
                        if (A[j].y <= md) // Should be added
                                Add(j, 1);
        Solve(md + 1, r, opt, ri);
        // Extending from left to [le, md]
        for (int i = opt - 1; i >= le; i --)
                for (int j : S[i])
                        if (A[j].y <= md)
                                Add(j, 1);
        // Shrinking from right to [le, l)
        for (int i = md; i >= l; i --)
                for (int j : E[i])
                        if (A[j].x >= le)
                                Add(j, -1);
        Solve(l, md - 1, le, opt);
}
int main()
{
        ll SM = 0;
        cin >> k >> n;
        for (int i = 1; i <= n; i ++)
        {
                int a, b;
                //char sa[1], sb[1];
                string sa, sb;
                //scanf("%s%d%s%d", sa, &a, sb, &b);
                cin >> sa >> a >> sb >> b;
                if (a > b) swap(a, b);
                SM += b - a;
                if (sa[0] == sb[0])
                        {n --; i --; continue;}
                SM ++;
                A[i] = {a, b};
                U.push_back(a);
                U.push_back(b);
                V.push_back(a + b);
        }
        if (!n)
                return !printf("%lld\n", SM);
        sort(U.begin(), U.end());
        U.resize(unique(U.begin(), U.end()) - U.begin());
        sort(V.begin(), V.end());
        V.resize(unique(V.begin(), V.end()) - V.begin());
        for (int i = 1; i <= n; i ++)
        {
                ID[i] = (int)(lower_bound(V.begin(), V.end(), A[i].x + A[i].y) - V.begin());
                A[i].x = (int)(lower_bound(U.begin(), U.end(), A[i].x) - U.begin());
                A[i].y = (int)(lower_bound(U.begin(), U.end(), A[i].y) - U.begin());
                S[A[i].x].push_back(i);
                E[A[i].y].push_back(i);
        }
        for (int i = 0; i < (int)U.size(); i ++)
        {
                // Sum of .y for all .y <= U[i]
                if (i)
                        FL[i] = FL[i - 1];
                for (int j : E[i])
                        FL[i].x -= U[i], FL[i].y ++;
        }
        for (int i = (int)U.size() - 1; ~ i; i --)
        {
                // Sum of .x for all .x >= U[i]
                FR[i] = FR[i + 1];
                for (int j : S[i])
                        FR[i].x += U[i], FR[i].y --;
        }
        ll Mn = (ll)(1e18);
        for (int i = 0; i < (int)U.size(); i ++)
        {
                ll rt = 0;
                rt += FL[i].x + FL[i].y * (ll)U[i];
                rt += FR[i].x + FR[i].y * (ll)U[i];
                Mn = min(Mn, rt);
        }
        if (k == 1)
                return !printf("%lld\n", Mn * 2LL + SM);
        Solve(0, (int)U.size() - 1, 0, (int)U.size() - 1);
        for (int i = 0; i < (int)U.size(); i ++)
                Mn = min(Mn, dp[i]);
        return !printf("%lld\n", Mn * 2LL + SM);
}

Compilation message

bridge.cpp: In function 'int main()':
bridge.cpp:133:26: warning: unused variable 'j' [-Wunused-variable]
  133 |                 for (int j : E[i])
      |                          ^
bridge.cpp:140:26: warning: unused variable 'j' [-Wunused-variable]
  140 |                 for (int j : S[i])
      |                          ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 9856 KB Output is correct
2 Correct 7 ms 9856 KB Output is correct
3 Correct 8 ms 9856 KB Output is correct
4 Correct 9 ms 9984 KB Output is correct
5 Correct 9 ms 9908 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 11 ms 9984 KB Output is correct
8 Correct 10 ms 9984 KB Output is correct
9 Correct 11 ms 9984 KB Output is correct
10 Correct 8 ms 9856 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9856 KB Output is correct
2 Correct 6 ms 9856 KB Output is correct
3 Correct 8 ms 9856 KB Output is correct
4 Correct 9 ms 9984 KB Output is correct
5 Correct 9 ms 9984 KB Output is correct
6 Correct 8 ms 9856 KB Output is correct
7 Correct 9 ms 9984 KB Output is correct
8 Correct 10 ms 10016 KB Output is correct
9 Correct 11 ms 9984 KB Output is correct
10 Correct 9 ms 9856 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 136 ms 13676 KB Output is correct
13 Correct 385 ms 24688 KB Output is correct
14 Correct 196 ms 13804 KB Output is correct
15 Correct 216 ms 18544 KB Output is correct
16 Correct 180 ms 13676 KB Output is correct
17 Correct 270 ms 24684 KB Output is correct
18 Correct 232 ms 24680 KB Output is correct
19 Correct 364 ms 24556 KB Output is correct
20 Correct 209 ms 13932 KB Output is correct
21 Correct 286 ms 24712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 7 ms 9760 KB Output is correct
3 Correct 9 ms 9984 KB Output is correct
4 Correct 8 ms 9984 KB Output is correct
5 Correct 9 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 8 ms 9984 KB Output is correct
8 Correct 10 ms 9984 KB Output is correct
9 Correct 8 ms 9984 KB Output is correct
10 Correct 8 ms 9984 KB Output is correct
11 Correct 8 ms 9984 KB Output is correct
12 Correct 10 ms 9984 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9728 KB Output is correct
2 Correct 8 ms 9856 KB Output is correct
3 Correct 9 ms 9984 KB Output is correct
4 Correct 9 ms 10112 KB Output is correct
5 Correct 9 ms 9984 KB Output is correct
6 Correct 8 ms 9984 KB Output is correct
7 Correct 7 ms 9984 KB Output is correct
8 Correct 8 ms 9984 KB Output is correct
9 Correct 9 ms 9984 KB Output is correct
10 Correct 11 ms 9984 KB Output is correct
11 Correct 7 ms 9984 KB Output is correct
12 Correct 9 ms 9984 KB Output is correct
13 Correct 8 ms 9984 KB Output is correct
14 Correct 10 ms 9984 KB Output is correct
15 Correct 16 ms 10112 KB Output is correct
16 Correct 7 ms 9984 KB Output is correct
17 Correct 9 ms 9984 KB Output is correct
18 Correct 11 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 17 ms 10112 KB Output is correct
21 Correct 10 ms 10112 KB Output is correct
22 Correct 15 ms 10112 KB Output is correct
23 Correct 10 ms 9984 KB Output is correct
24 Correct 18 ms 10112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9856 KB Output is correct
2 Correct 7 ms 9728 KB Output is correct
3 Correct 8 ms 9984 KB Output is correct
4 Correct 8 ms 9984 KB Output is correct
5 Correct 7 ms 9984 KB Output is correct
6 Correct 7 ms 9984 KB Output is correct
7 Correct 9 ms 9984 KB Output is correct
8 Correct 9 ms 9984 KB Output is correct
9 Correct 8 ms 9984 KB Output is correct
10 Correct 9 ms 9984 KB Output is correct
11 Correct 9 ms 9984 KB Output is correct
12 Correct 9 ms 9984 KB Output is correct
13 Correct 9 ms 9984 KB Output is correct
14 Correct 12 ms 9984 KB Output is correct
15 Correct 15 ms 10112 KB Output is correct
16 Correct 9 ms 9984 KB Output is correct
17 Correct 10 ms 9984 KB Output is correct
18 Correct 9 ms 9984 KB Output is correct
19 Correct 11 ms 9984 KB Output is correct
20 Correct 15 ms 10240 KB Output is correct
21 Correct 10 ms 10112 KB Output is correct
22 Correct 15 ms 10112 KB Output is correct
23 Correct 10 ms 9984 KB Output is correct
24 Correct 15 ms 10112 KB Output is correct
25 Correct 174 ms 14184 KB Output is correct
26 Correct 314 ms 13416 KB Output is correct
27 Correct 1460 ms 27596 KB Output is correct
28 Correct 1381 ms 28996 KB Output is correct
29 Correct 1411 ms 28912 KB Output is correct
30 Correct 652 ms 20084 KB Output is correct
31 Correct 238 ms 14184 KB Output is correct
32 Correct 1222 ms 28888 KB Output is correct
33 Correct 360 ms 26600 KB Output is correct
34 Correct 1713 ms 28564 KB Output is correct
35 Correct 286 ms 13836 KB Output is correct
36 Correct 1378 ms 28844 KB Output is correct
37 Correct 567 ms 14064 KB Output is correct