Submission #1284213

#TimeUsernameProblemLanguageResultExecution timeMemory
1284213hiepsimauhongBuilding Bridges (CEOI17_building)C++20
100 / 100
40 ms9184 KiB
#include <bits/stdc++.h>

using namespace std;

#define int long long

#define FOR(I, L, R) for(int I(L) ; I <= (int)R ; ++I)
#define FOD(I, R, L) for(int I(R) ; I >= (int)L ; --I)
#define FOA(I, A) for(auto &I : A)

#define print(A,L,R) FOR(OK, L, R){if(A[OK]<=-oo / 10||A[OK]>=oo)cout<<"- ";else cout<<A[OK]<<' ';}cout<<'\n';
#define prints(A) FOA(OK, A){cout<<OK<<' ';}cout << '\n';
#define printz(A,L,R) FOR(OK, 0, L){FOR(KO, 0, R){if(A[OK][KO]>-oo&&A[OK][KO]<oo)cout<<A[OK][KO]<<' ';else cout << "- ";} cout << '\n';}cout << '\n';

#define fs first
#define sd second
#define ii pair<int,int>
#define iii pair<int, ii>
#define all(A) A.begin(), A.end()
#define quickly ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define div asdjfh
#define FILE kangaroo

const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int oo = 1e18;

struct line{
        int a, b;
        mutable int p;

        line(){a = b = 0, p = -oo;}
        line(int _a, int _b){a = _a, b = _b, p = -oo;}

        int eval(int x) const{
                return a * x + b;
        }

        bool operator < (const line &L) const{
                return a < L.a;
        }

        bool operator < (const int x) const{
                return p < x;
        }
};

struct DynamicCHT{
        multiset<line, less<>> hull;

        int div(int a, int b){
                return a / b - ((a ^ b) < 0 && a % b);
        }

        bool isect(multiset<line>::iterator x, multiset<line>::iterator y){
                if(y == hull.end()){
                        x -> p = oo;
                        return 0;
                }
                if(x -> a == y -> a){
                        x -> p = (x -> b >= y -> b ? oo : -oo);
                }
                else{
                        x -> p = div(y -> b - x -> b, x -> a - y -> a);
                }

                return x -> p >= y -> p;
        }

        void add_line(int a, int b){
                auto z = hull.insert(line(a, b));
                auto y = z++;
                auto x = y;

                while(isect(y, z)){
                        z = hull.erase(z);
                }

                if(x != hull.begin() && isect((--x), y)){
                        isect(x, hull.erase(y));
                        return;
                }

                while((y = x) != hull.begin() && (--x) -> p >= y -> p){
                        isect(x, hull.erase(y));
                }
        }

        int get(int x){
                if(hull.empty()) return -oo;
                auto it = hull.lower_bound(x);
                if(it == hull.end()) --it;
                return it -> eval(x);
        }
} cht;

int n;
int a[N], h[N], dp[N];

signed main(){ quickly
        if(fopen("FILE.in", "r")){
                freopen("FILE.in", "r", stdin);
                freopen("FILE.out", "w", stdout);
        }

        cin >> n;

        FOR(i, 1, n){
                cin >> h[i];
        }

        FOR(i, 1, n){
                cin >> a[i];
                a[i] += a[i - 1];
        }

        /// h[i] ^ 2 - 2 * h[i] * h[j] + h[j] ^ 2 + a[i - 1] - a[j]
        cht.add_line(2 * h[1], -(h[1] * h[1] - a[1]));

        FOR(i, 2, n){
                dp[i] = -cht.get(h[i]) + a[i - 1] + h[i] * h[i];
                cht.add_line(2 * h[i], -(dp[i] - a[i] + h[i] * h[i]));
        }

        cout << dp[n];
}

Compilation message (stderr)

building.cpp: In function 'int main()':
building.cpp:102:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  102 |                 freopen("FILE.in", "r", stdin);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
building.cpp:103:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  103 |                 freopen("FILE.out", "w", stdout);
      |                 ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...