답안 #903953

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
903953 2024-01-11T15:27:55 Z Flan312 Building Bridges (CEOI17_building) C++17
100 / 100
62 ms 12148 KB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define eb emplace_back
#define task ""
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
#define fi first
#define se second
#define pii pair <int, int>
#define tii tuple <int, int, int>
using namespace std;
const int nmax = 1e5 + 2;
const ll oo = 1e18;
int n;
ll h[nmax], w[nmax], dp[nmax];
struct segment
{
    ll a, b;
    segment(ll a = 0, ll b = oo) : a(a), b(b) {}
};
struct node
{
    segment line;
    node *l, *r;
    node(const segment &line = segment(), node *l = nullptr, node *r = nullptr) : line(line), l(l), r(r) {}
};
#define check(x) if (x == nullptr) x = new node()
node *lichao = new node();
ll get(const segment &line, int x)
{
    return line.a * x + line.b;
}
void update(node *root, int l, int r, int u, int v, const segment &line)
{
    if (l > v || r < u) return;
    int mid = l + r >> 1;
    if (l >= u && r <= v)
    {
        check(root -> l);
        check(root -> r);
        ll rootL = get(root -> line, l);
        ll rootR = get(root -> line, r);
        ll rootM = get(root -> line, mid);
        ll lineL = get(line, l);
        ll lineR = get(line, r);
        ll lineM = get(line, mid);
        if (rootL <= lineL && rootR <= lineR) return;
        if (rootL >= lineL && rootR >= lineR) {root -> line = line; return;}
        if (rootL <= lineL && rootM <= lineM)
        {
            update(root -> r, mid + 1, r, u, v, line);
            return;
        }
        if (rootL >= lineL && rootM >= lineM)
        {
            update(root -> r, mid + 1, r, u, v, root -> line);
            root -> line = line;
            return;
        }
        if (get(root -> line, mid + 1) <= get(line, mid + 1) && rootR <= lineR)
        {
            update(root -> l, l, mid, u, v, line);
            return;
        }
        if (get(root -> line, mid + 1) >= get(line, mid + 1) && rootR >= lineR)
        {
            update(root -> l, l, mid, u, v, root -> line);
            root -> line = line;
            return;
        }
    }
    update(root -> l, l, mid, u, v, line);
    update(root -> r, mid + 1, r, u, v, line);
}
void update(int u, int v, const segment &line) {update(lichao, 0, 2e6, 0, 2e6, line);}
ll get(node *root, int l, int r, int x)
{
    if (root == nullptr || (l > x || r < x)) return oo;
    ll res = get(root -> line, x);
    if (l == r) return res;
    int mid = l + r >> 1;
    res = min(res, get(root -> l, l, mid, x));
    res = min(res, get(root -> r, mid + 1, r, x));
    return res;
}
int main()
{
    if (ifstream(task".inp")) nx
    fast
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> h[i];
    for (int i = 1; i <= n; ++i)
        cin >> w[i];;
    dp[1] = -w[1];
    update(0, 2e6, segment(-2 * h[1], 1ll * h[1] * h[1] + dp[1]));
    for (int i = 2; i <= n; ++i)
    {
        dp[i] = 1ll * h[i] * h[i] - w[i] + get(lichao, 0, 2e6, h[i]);
        update(0, 2e6, segment(-2 * h[i], 1ll * h[i] * h[i] + dp[i]));
    }
    cout << dp[n] + accumulate(w + 1, w + n + 1, 0LL);
}

Compilation message

building.cpp: In function 'void update(node*, int, int, int, int, const segment&)':
building.cpp:37:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   37 |     int mid = l + r >> 1;
      |               ~~^~~
building.cpp: In function 'long long int get(node*, int, int, int)':
building.cpp:82:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |     int mid = l + r >> 1;
      |               ~~^~~
building.cpp: In function 'int main()':
building.cpp:7:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
building.cpp:89:31: note: in expansion of macro 'nx'
   89 |     if (ifstream(task".inp")) nx
      |                               ^~
building.cpp:7:52: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
    7 | #define nx freopen (task".inp","r",stdin), freopen (task".out","w",stdout);
      |                                            ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
building.cpp:89:31: note: in expansion of macro 'nx'
   89 |     if (ifstream(task".inp")) nx
      |                               ^~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 44 ms 3172 KB Output is correct
2 Correct 62 ms 3156 KB Output is correct
3 Correct 43 ms 3164 KB Output is correct
4 Correct 57 ms 2952 KB Output is correct
5 Correct 33 ms 6224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 44 ms 3172 KB Output is correct
7 Correct 62 ms 3156 KB Output is correct
8 Correct 43 ms 3164 KB Output is correct
9 Correct 57 ms 2952 KB Output is correct
10 Correct 33 ms 6224 KB Output is correct
11 Correct 40 ms 3160 KB Output is correct
12 Correct 46 ms 3392 KB Output is correct
13 Correct 38 ms 2648 KB Output is correct
14 Correct 40 ms 3264 KB Output is correct
15 Correct 42 ms 12148 KB Output is correct
16 Correct 34 ms 5968 KB Output is correct
17 Correct 14 ms 2652 KB Output is correct
18 Correct 13 ms 2632 KB Output is correct