Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define $ ios::sync_with_stdio(0);
  5. #define INF 0x3f3f3f3f
  6. #define INFL 0x3f3f3f3f3f3f3f3f
  7. #define MAXN 1000100
  8. using namespace std;
  9. typedef long long int ll;
  10. typedef pair<int, int> ii;
  11. typedef vector<int> vi;
  12. typedef pair<int, ii> iii;
  13. typedef vector<vi> graph;
  14. struct Line {
  15.     ll m, b;
  16.     Line(){m = 0, b = 0;}
  17.     Line(ll mm, ll bb) {m = mm, b = bb;}
  18.  
  19.     ll getCost(ll  x) const {
  20.         return m*x + b;
  21.     }
  22.     void newSpeed(ll t, ll mm) {
  23.         b = (m-mm)*t+b; //inclinate Line
  24.         m = mm;
  25.     }
  26.     bool operator<(const ll& val) const{return m < val;}
  27.     bool operator<(const Line &rhs) const {return  m < rhs.m;}
  28.  
  29. };
  30. Line cyc[50005];
  31. set<Line> hull; //envelope
  32. bool hasNext(set<Line>::iterator it) {
  33.     return it != hull.end() && next(it) != hull.end();
  34. }
  35. bool hasPrev(set<Line>::iterator it) {
  36.     return it != hull.begin();
  37. }
  38.  
  39. bool to(set<Line>::iterator it) { // time overtaken
  40.     if (hasPrev(it) && hasNext(it)) { //verifies if Line *it is irrelevant
  41.         Line i = *prev(it);
  42.         Line j = *it;
  43.         Line k = *next(it);
  44.        
  45.         return !((i.b - k.b)*(k.m - j.m) < (j.b - k.b) * (k.m - i.m)); // intersect(k, i) >= intersect(k, j)
  46.     }
  47.     return 0;
  48. }
  49.  
  50. void addLine(Line l) {
  51.     auto p = hull.lower_bound(l);
  52.     if (p != hull.end()) {
  53.         if (p->m == l.m) { // if has some parallel Line
  54.             if (p->b < l.b) { // if y-intercept is lower, discard it
  55.                 p = hull.erase(p);
  56.             } else { //else don't add this Line
  57.                 return;
  58.             }
  59.         }
  60.     }
  61.    
  62.     p = hull.insert(p, l); //insert this Line
  63.     if (to(p)) { //if this line is irrelevant, delete it
  64.         hull.erase(p);
  65.         return;
  66.     }
  67.    
  68.     while (hasPrev(p) && to(prev(p))) hull.erase(prev(p)); //try to maintain invariants with lower Lines
  69.     while (hasNext(p) && to(next(p))) hull.erase(next(p)); //try to maintain invariants with upper Lines
  70. }
  71.  
  72. ll getBest(ll x) {
  73.     if (hull.empty()) return 0;
  74.     while (hasNext(hull.begin()) && hull.begin()->getCost(x) <= next(hull.begin())->getCost(x)) { // x1 <= x2, so the best in on the bottom
  75.         hull.erase(hull.begin());
  76.     }
  77.     return hull.begin()->getCost(x);
  78. }
  79.  
  80. int main() {
  81.     int n, m, q, k, t, c, sp;
  82.     scanf("%d %d", &n, &q);
  83.     while (q--) {
  84.         scanf("%d", &t);
  85.         if (t&1) {
  86.             scanf("%d %d %d",&t, &c, &sp);
  87.             cyc[c].newSpeed(t, sp); // inclinate Line
  88.             Line cur = cyc[c];
  89.             addLine(cur);//try add new Line
  90.         } else {
  91.             scanf("%d", &t);
  92.             printf("%lld\n", getBest(t)); //get Best score
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement