답안 #943109

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943109 2024-03-11T08:22:33 Z hariaakas646 Building Bridges (CEOI17_building) C++17
100 / 100
46 ms 5204 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define scd(t) scanf("%d", &t)
#define sclld(t) scanf("%lld", &t)
#define forr(i, l, r) for(int i=l; i<r; i++)
#define frange(i, l) forr(i, 0, l)
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
 
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
typedef long long lli;
typedef vector<vi> vvi;
typedef vector<lli> vll;
typedef vector<bool> vb;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long double ld;

void usaco()
{
    freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
//    freopen("problem.out", "w", stdout);
}

struct line {
    lli m=0, c=1e16;

    lli eval(lli x) {
        return m*x + c;
    }

    ld intersect(line y) {
        return ld(y.c - c) / ld(m - y.m);
    }
};

struct Data {
    line ln;
    int lc = -1;
    int rc = -1;
};

const int maxn = 2e5 + 10;
Data tree[maxn * 4];
int id = 0;

void add(Data data, int x, lli l, lli r) {
    if(l > r) return;
    lli mid = (l+r)/2;
    bool lb = data.ln.eval(l) < tree[x].ln.eval(l);
    bool mb = data.ln.eval(mid) < tree[x].ln.eval(mid);
    if(mb) {
        swap(data.ln, tree[x].ln);
    } 
    if(lb != mb) {
        if(tree[x].lc == -1) {
            tree[x].lc = ++id;
            tree[id] = data;
        }
        else {
            add(data, tree[x].lc, l, mid-1);
        }
    }
    else {
        if(tree[x].rc == -1) {
            tree[x].rc = ++id;
            tree[id] = data;
        }
        else {
            add(data, tree[x].rc, mid+1, r);
        }
    }
}

lli get(int x, lli l, lli r, lli val) {
    lli mid = (l + r)/2;    
    // printf("%lld ", tree[x].ln.m);
    if(val == mid) return tree[x].ln.eval(val);
    if(val < mid) {
        if(tree[x].lc != -1)
            return min(tree[x].ln.eval(val), get(tree[x].lc, l, mid-1, val));
        else 
            return tree[x].ln.eval(val);
    }
    else {
        if(tree[x].rc != -1)
            return min(tree[x].ln.eval(val), get(tree[x].rc, mid+1, r, val));
        else 
            return tree[x].ln.eval(val);
    }
}

int main() {
    // usaco();
    int n;
    scd(n);

    vector<pair<lli, lli>> vec(n);

    frange(i, n) {
        sclld(vec[i].f);
    }
    frange(i, n) {
        sclld(vec[i].s);
    }
    lli val = 0;
    Data tmp;
    tmp.ln.m = -2 * vec[0].f;
    tmp.ln.c = vec[0].f * vec[0].f - vec[0].s;
    add(tmp, 0, 0, 1e6+10);
    forr(i, 1, n) {
        // printf("%d ", i);
        lli data = get(0, 0, 1e6+10, vec[i].f);
        // printf("\n");
        val = data + vec[i].f * vec[i].f - vec[i].s;
        tmp.ln.m = -2 * vec[i].f;
        tmp.ln.c = val + vec[i].f * vec[i].f;
        // printf("%lld %lld\n", tmp.ln.m, tmp.ln.c);
        add(tmp, 0, 0, 1e6+10);
    }

    frange(i, n) {
        val += vec[i].s;
    }

    printf("%lld", val);
}

Compilation message

building.cpp: In function 'void usaco()':
building.cpp:28:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   28 |     freopen("/media/hariaakash646/785EF1075EF0BF46/CompetitiveProgramming/input.in", "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
building.cpp: In function 'int main()':
building.cpp:5:21: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    5 | #define scd(t) scanf("%d", &t)
      |                ~~~~~^~~~~~~~~~
building.cpp:103:5: note: in expansion of macro 'scd'
  103 |     scd(n);
      |     ^~~
building.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t)
      |                  ~~~~~^~~~~~~~~~~~
building.cpp:108:9: note: in expansion of macro 'sclld'
  108 |         sclld(vec[i].f);
      |         ^~~~~
building.cpp:6:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    6 | #define sclld(t) scanf("%lld", &t)
      |                  ~~~~~^~~~~~~~~~~~
building.cpp:111:9: note: in expansion of macro 'sclld'
  111 |         sclld(vec[i].s);
      |         ^~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 46 ms 2264 KB Output is correct
2 Correct 38 ms 2124 KB Output is correct
3 Correct 43 ms 2136 KB Output is correct
4 Correct 36 ms 1884 KB Output is correct
5 Correct 29 ms 3556 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 46 ms 2264 KB Output is correct
7 Correct 38 ms 2124 KB Output is correct
8 Correct 43 ms 2136 KB Output is correct
9 Correct 36 ms 1884 KB Output is correct
10 Correct 29 ms 3556 KB Output is correct
11 Correct 40 ms 2384 KB Output is correct
12 Correct 40 ms 2532 KB Output is correct
13 Correct 33 ms 1884 KB Output is correct
14 Correct 41 ms 3788 KB Output is correct
15 Correct 31 ms 5204 KB Output is correct
16 Correct 29 ms 4368 KB Output is correct
17 Correct 28 ms 2904 KB Output is correct
18 Correct 22 ms 2908 KB Output is correct