Cod sursa(job #2439367)

Utilizator ApostolIlieDanielApostol Daniel ApostolIlieDaniel Data 15 iulie 2019 18:47:06
Problema Stalpi Scor 60
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.74 kb
#include <bits/stdc++.h>

using namespace std;

const int INF = 2e9, MAX_N = 1e5;

struct Stalp {
    int x;
    int c;
    int s;
    int d;
};

Stalp a[1 + MAX_N];

bool cmp1 (Stalp a, Stalp b) {
    return a.x < b.x;
}

bool cmp2 (Stalp a, Stalp b) {
    if (a.s != b.s)
        return a.s < b.s;
    return a.d < b.d;
}

int lsb (int x) {
    return x & -x;
}

struct bit {
  int aib[MAX_N + 1];
  bit (int n = 0) {
      for (int i = 1; i <= n; i++)
        aib[i] = INF;
  }
  void add (int val, int poz, int n) {
    while (poz) {
      aib[poz] = min (aib[poz], val);
      poz -= lsb (poz);
    }
  }
  int query (int poz, int n) {
    int sol;
    sol = INF;
    while (poz <= n) {
        sol = min (sol, aib[poz]);
        poz += lsb (poz);
    }
    return sol;
  }
};
int n;
int get_poz (int x) {
    int l, r;
    l = 1; r = n;
    while (l <= r) {
        int mid = (l + r) / 2;
        if (a[mid].x > x)
            r = mid - 1;
        else
            l = mid + 1;
    }
    return r;
}

int main() {
    freopen ("stalpi.in", "r", stdin);
    freopen ("stalpi.out", "w", stdout);

    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i].x >> a[i].c >> a[i].s >> a[i].d;

    sort (a + 1, a + n + 1, cmp1);
    for (int i = 1; i <= n; i++) {
        int x = a[i].x - a[i].s;
        a[i].s = get_poz (x);
        x = a[i].x + a[i].d;
        a[i].d = get_poz (x);
    }

    bit best (n);
    sort (a + 1, a + n + 1, cmp2);
    for (int i = 1; i <= n; i++) {
        int min_cost = a[i].c;
        if (a[i].s > 0)
            min_cost += best.query (a[i].s, n);
        best.add (min_cost, a[i].d, n);
    }

    cout << best.aib[n] << "\n";
    return 0;
}