Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define $ ios::sync_with_stdio(0);
- #define INF 0x3f3f3f3f
- #define INFL 0x3f3f3f3f3f3f3f3f
- #define MAXN 1000100
- using namespace std;
- typedef long long int ll;
- typedef pair<int, int> ii;
- typedef vector<int> vi;
- typedef pair<int, ii> iii;
- typedef vector<vi> graph;
- struct Line {
- ll m, b;
- Line(){m = 0, b = 0;}
- Line(ll mm, ll bb) {m = mm, b = bb;}
- ll getCost(ll x) const {
- return m*x + b;
- }
- void newSpeed(ll t, ll mm) {
- b = (m-mm)*t+b; //inclinate Line
- m = mm;
- }
- bool operator<(const ll& val) const{return m < val;}
- bool operator<(const Line &rhs) const {return m < rhs.m;}
- };
- Line cyc[50005];
- set<Line> hull; //envelope
- bool hasNext(set<Line>::iterator it) {
- return it != hull.end() && next(it) != hull.end();
- }
- bool hasPrev(set<Line>::iterator it) {
- return it != hull.begin();
- }
- bool to(set<Line>::iterator it) { // time overtaken
- if (hasPrev(it) && hasNext(it)) { //verifies if Line *it is irrelevant
- Line i = *prev(it);
- Line j = *it;
- Line k = *next(it);
- return !((i.b - k.b)*(k.m - j.m) < (j.b - k.b) * (k.m - i.m)); // intersect(k, i) >= intersect(k, j)
- }
- return 0;
- }
- void addLine(Line l) {
- auto p = hull.lower_bound(l);
- if (p != hull.end()) {
- if (p->m == l.m) { // if has some parallel Line
- if (p->b < l.b) { // if y-intercept is lower, discard it
- p = hull.erase(p);
- } else { //else don't add this Line
- return;
- }
- }
- }
- p = hull.insert(p, l); //insert this Line
- if (to(p)) { //if this line is irrelevant, delete it
- hull.erase(p);
- return;
- }
- while (hasPrev(p) && to(prev(p))) hull.erase(prev(p)); //try to maintain invariants with lower Lines
- while (hasNext(p) && to(next(p))) hull.erase(next(p)); //try to maintain invariants with upper Lines
- }
- ll getBest(ll x) {
- if (hull.empty()) return 0;
- while (hasNext(hull.begin()) && hull.begin()->getCost(x) <= next(hull.begin())->getCost(x)) { // x1 <= x2, so the best in on the bottom
- hull.erase(hull.begin());
- }
- return hull.begin()->getCost(x);
- }
- int main() {
- int n, m, q, k, t, c, sp;
- scanf("%d %d", &n, &q);
- while (q--) {
- scanf("%d", &t);
- if (t&1) {
- scanf("%d %d %d",&t, &c, &sp);
- cyc[c].newSpeed(t, sp); // inclinate Line
- Line cur = cyc[c];
- addLine(cur);//try add new Line
- } else {
- scanf("%d", &t);
- printf("%lld\n", getBest(t)); //get Best score
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement