QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#77377#5458. Shortest Path QueryrniyaAC ✓219ms328636kbC++1720.8kb2023-02-14 15:32:022024-06-21 12:37:43

Judging History

你现在查看的是最新测评结果

  • [2024-06-21 12:37:43]
  • 管理员手动重测本题所有提交记录
  • 测评结果:AC
  • 用时:219ms
  • 内存:328636kb
  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-14 15:32:05]
  • 评测
  • 测评结果:100
  • 用时:276ms
  • 内存:328496kb
  • [2023-02-14 15:32:02]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) void(0)
#endif

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}

template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; }
template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; }

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }

const int INF = (1 << 30) - 1;
const long long IINF = (1LL << 60) - 1;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MOD = 998244353;
// const int MOD = 1000000007;

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <tuple>
#include <vector>

namespace geometry {
using Real = ll;  // change this flexibly if you want more precision
constexpr Real EPS = 1e-8;
constexpr Real PI = 3.14159265358979323846L;

inline int sgn(Real x) { return x < -EPS ? -1 : x > EPS ? 1 : 0; }

inline int compare(Real a, Real b) { return sgn(a - b); }

inline bool equals(Real a, Real b) { return compare(a, b) == 0; }

struct Point {
    Real x, y;

    Point() {}

    Point(Real x, Real y) : x(x), y(y) {}

    Point& operator+=(const Point& p) {
        x += p.x, y += p.y;
        return *this;
    }

    Point& operator-=(const Point& p) {
        x -= p.x, y -= p.y;
        return *this;
    }

    Point& operator*=(const Real& k) {
        x *= k, y *= k;
        return *this;
    }

    Point& operator/=(const Real& k) {
        x /= k, y /= k;
        return *this;
    }

    Point operator+(const Point& p) const { return Point(*this) += p; }

    Point operator-(const Point& p) const { return Point(*this) -= p; }

    Point operator*(const Real& k) const { return Point(*this) *= k; }

    Point operator/(const Real& k) const { return Point(*this) /= k; }

    Point operator*(const Point& p) const { return Point(x * p.x - y * p.y, x * p.y + y * p.x); }

    Point operator-() const { return Point(-x, -y); }

    bool operator==(const Point& p) const { return (compare(x, p.x) == 0 && compare(y, p.y) == 0); }

    bool operator!=(const Point& p) const { return !(*this == p); }

    bool operator<(const Point& p) const {
        return compare(x, p.x) < 0 || (compare(x, p.x) == 0 && compare(y, p.y) < 0);
    }

    bool operator>(const Point& p) const {
        return compare(x, p.x) > 0 || (compare(x, p.x) == 0 && compare(y, p.y) > 0);
    }

    friend std::istream& operator>>(std::istream& is, Point& p) { return is >> p.x >> p.y; }

    friend std::ostream& operator<<(std::ostream& os, const Point& p) { return os << p.x << ' ' << p.y; }

    Real norm() const { return x * x + y * y; }

    Real abs() const { return std::hypot(x, y); }

    Real arg() const { return std::atan2(y, x); }

    Point normal() const { return Point(-y, x); }

    Point unit() const { return *this / abs(); }

    Point conj() const { return Point(x, -y); }

    Point rotate(Real theta) const {
        return Point(x * std::cos(theta) - y * std::sin(theta), x * std::sin(theta) + y * std::cos(theta));
    }
};

Point polar(const Real& r, const Real& theta) { return Point(cos(theta), sin(theta)) * r; }

Real dot(const Point& p, const Point& q) { return p.x * q.x + p.y * q.y; }

Real cross(const Point& p, const Point& q) { return p.x * q.y - p.y * q.x; }

Real distance(const Point& p, const Point& q) { return (p - q).abs(); }

struct Line {
    Point a, b;

    Line() {}

    Line(const Point& a, const Point& b) : a(a), b(b) {}

    Line(const Real& A, const Real& B, const Real& C) {  // Ax + By = c
        if (equals(A, 0)) {
            assert(!equals(B, 0));
            a = Point(0, C / B);
            b = Point(1, C / B);
        } else if (equals(B, 0)) {
            a = Point(C / A, 0);
            b = Point(C / A, 1);
        } else if (equals(C, 0)) {
            a = Point(0, 0);
            b = Point(B, -A);
        } else {
            a = Point(0, C / B);
            b = Point(C / A, 0);
        }
    }

    Point diff() const { return b - a; }

    friend std::istream& operator>>(std::istream& is, Line& l) { return is >> l.a >> l.b; }

    friend std::ostream& operator<<(std::ostream& os, const Line& l) { return os << l.a << " to " << l.b; }
};

struct Segment : Line {
    Segment() {}

    Segment(Point a, Point b) : Line(a, b) {}

    Real length() const { return diff().abs(); }
};

Point proj(const Line& l, const Point& p) {
    Point v = l.diff().unit();
    return l.a + v * dot(v, p - l.a);
}

Point refl(const Line& l, const Point& p) {
    Point h = proj(l, p);
    return h + (h - p);
}

bool orthogonal(const Line& l, const Line& m) { return equals(dot(l.diff(), m.diff()), 0); }

bool parallel(const Line& l, const Line& m) { return equals(cross(l.diff(), m.diff()), 0); }

enum Position { COUNTER_CLOCKWISE = +1, CLOCKWISE = -1, ONLINE_BACK = +2, ONLINE_FRONT = -2, ON_SEGMENT = 0 };

Position ccw(const Point& a, Point b, Point c) {
    b -= a, c -= a;
    if (sgn(cross(b, c)) == +1) return COUNTER_CLOCKWISE;
    if (sgn(cross(b, c)) == -1) return CLOCKWISE;
    if (sgn(dot(b, c)) == -1) return ONLINE_BACK;
    if (compare(b.norm(), c.norm()) == -1) return ONLINE_FRONT;
    return ON_SEGMENT;
}

bool intersect(const Line& l, const Point& p) { return abs(ccw(l.a, l.b, p)) != 1; }

bool intersect(const Line& l, const Line& m) {
    Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);
    if (equals(A, 0) && equals(B, 0)) return true;
    return !parallel(l, m);
}

bool intersect(const Line& l, const Segment& s) {
    return sgn(cross(l.diff(), s.a - l.a)) * sgn(cross(l.diff(), s.b - l.a)) <= 0;
}

bool intersect(const Segment& s, const Point& p) { return ccw(s.a, s.b, p) == ON_SEGMENT; }

bool intersect(const Segment& s, const Segment& t) {
    return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0 && ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

Real distance(const Line& l, const Point& p) { return distance(p, proj(l, p)); }

Real distance(const Line& l, const Line& m) { return intersect(l, m) ? 0 : distance(l, m.a); }

Real distance(const Line& l, const Segment& s) {
    return intersect(l, s) ? 0 : std::min(distance(l, s.a), distance(l, s.b));
}

Real distance(const Segment& s, const Point& p) {
    Point h = proj(s, p);
    return intersect(s, h) ? distance(p, h) : std::min(distance(p, s.a), distance(p, s.b));
}

Real distance(const Segment& s, const Segment& t) {
    if (intersect(s, t)) return 0;
    return std::min({distance(s, t.a), distance(s, t.b), distance(t, s.a), distance(t, s.b)});
}

Point crosspoint(const Line& l, const Line& m) {
    assert(intersect(l, m));
    Real A = cross(l.diff(), m.diff()), B = cross(l.diff(), l.b - m.a);
    if (equals(A, 0) && equals(B, 0)) return m.a;
    return m.a + m.diff() * B / A;
}

struct Circle {
    Point center;
    Real radius;

    Circle() {}

    Circle(const Point& center, const Real& radius) : center(center), radius(radius) {}

    friend std::istream& operator>>(std::istream& is, Circle& c) { return is >> c.center >> c.radius; }

    friend std::ostream& operator<<(std::ostream& os, const Circle& c) { return os << c.center << ' ' << c.radius; }
};

bool intersect(const Circle& c, const Line& l) { return compare(c.radius, distance(l, c.center)) >= 0; }

int intersect(const Circle& c, const Segment& s) {
    Point h = proj(s, c.center);
    if (compare(distance(c.center, h), c.radius) > 0) return 0;
    Real d1 = (c.center - s.a).abs(), d2 = (c.center - s.b).abs();
    if (compare(c.radius, d1) >= 0 && compare(c.radius, d2) >= 0) return 0;
    if (compare(c.radius, d1) * compare(c.radius, d2) < 0) return 1;
    if (sgn(dot(s.a - h, s.b - h)) < 0) return 2;
    return 0;
}

std::vector<Point> crosspoint(const Circle& c, const Line& l) {
    Point h = proj(l, c.center);
    Real d = c.radius * c.radius - (c.center - h).norm();
    if (sgn(d) < 0) return {};
    if (sgn(d) == 0) return {h};
    Point v = l.diff().unit() * sqrt(d);
    return {h - v, h + v};
}

std::vector<Point> crosspoint(const Circle& c, const Segment& s) {
    int num = intersect(c, s);
    if (num == 0) return {};
    auto res = crosspoint(c, Line(s.a, s.b));
    if (num == 2) return res;
    if (sgn(dot(s.a - res[0], s.b - res[0])) > 0) std::swap(res[0], res[1]);
    return {res[0]};
}

// requirement : c != d
std::vector<Point> crosspoint(const Circle& c1, const Circle& c2) {
    Real r1 = c1.radius, r2 = c2.radius;
    if (r1 < r2) return crosspoint(c2, c1);
    Real d = distance(c1.center, c2.center);
    if (compare(d, r1 + r2) > 0 || compare(d, r1 - r2) < 0) return {};
    Real alpha = std::acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d));
    Real theta = (c2.center - c1.center).arg();
    Point p = c1.center + polar(r1, theta + alpha);
    Point q = c1.center + polar(r1, theta - alpha);
    if (p == q) return {p};
    return {p, q};
}

Real commonarea(Circle c1, Circle c2) {
    Real r1 = c1.radius, r2 = c2.radius;
    Real d = (c1.center - c2.center).abs();
    if (compare(r1 + r2, d) <= 0) return 0;
    if (compare(std::fabs(r1 - r2), d) >= 0) return PI * min(r1, r2) * min(r1, r2);
    Real res = 0;
    for (int _ = 0; _ < 2; _++) {
        r1 = c1.radius, r2 = c2.radius;
        Real cosine = (d * d + r1 * r1 - r2 * r2) / (2 * d * r1);
        Real theta = std::acos(cosine) * 2;
        res += (theta - std::sin(theta)) * r1 * r1 / 2;
        swap(c1, c2);
    }
    return res;
}

Line bisector(const Point& p, const Point& q) {
    Point c = (p + q) * 0.5;
    Point v = (q - p).normal();
    return Line(c - v, c + v);
}

Circle circumcircle(Point a, Point b, const Point& c) {
    Point center = crosspoint(bisector(a, c), bisector(b, c));
    return Circle(center, distance(c, center));
}

Circle incircle(const Point& a, const Point& b, const Point& c) {
    Real A = (b - c).abs(), B = (c - a).abs(), C = (a - b).abs();
    Point center = (a * A + b * B + c * C) / (A + B + C);
    return Circle(center, distance(Segment(a, b), center));
}

std::vector<Point> center_given_radius(const Point& a, const Point& b, const Real& r) {
    Point m = (b - a) * 0.5;
    Real d1 = m.abs();
    if (equals(d1, 0) || d1 > r) return {};
    Real d2 = sqrt(r * r - d1 * d1);
    Point n = m.normal() * d2 / d1;
    Point p = a + m - n, q = a + m + n;
    if (p == q) return {p};
    return {p, q};
}

int count_tangent(const Circle& c1, const Circle& c2) {
    Real r1 = c1.radius, r2 = c2.radius;
    if (r1 < r2) return count_tangent(c2, c1);
    Real d = distance(c1.center, c2.center);
    if (compare(d, r1 + r2) > 0) return 4;
    if (compare(d, r1 + r2) == 0) return 3;
    if (compare(d, r1 - r2) > 0) return 2;
    if (compare(d, r1 - r2) == 0) return 1;
    return 0;
}

std::vector<Point> tangent_to_circle(const Circle& c, const Point& p) {
    return crosspoint(c, Circle(p, sqrt((c.center - p).norm() - c.radius * c.radius)));
}

std::vector<Line> common_tangent(const Circle& c1, const Circle& c2) {
    if (c1.radius < c2.radius) return common_tangent(c2, c1);
    std::vector<Line> res;
    Real g = distance(c1.center, c2.center);
    if (equals(g, 0)) return res;
    Point u = (c2.center - c1.center) / g, v = u.normal();
    for (int s : {-1, 1}) {
        Real h = (c1.radius + c2.radius * s) / g;
        if (equals(1 - h * h, 0))
            res.emplace_back(c1.center + u * c1.radius, c1.center + (u + v) * c1.radius);
        else if (compare(1 - h * h, 0) > 0) {
            Point U = u * h, V = v * std::sqrt(1 - h * h);
            res.emplace_back(c1.center + (U + V) * c1.radius, c2.center - (U + V) * c2.radius * s);
            res.emplace_back(c1.center + (U - V) * c1.radius, c2.center - (U - V) * c2.radius * s);
        }
    }
    return res;
}

enum Contain { OUT, ON, IN };

struct Polygon : std::vector<Point> {
    using std::vector<Point>::vector;

    Polygon(int n) : std::vector<Point>(n) {}

    std::vector<Segment> segments() const {
        assert(size() > 1);
        std::vector<Segment> segs;
        for (size_t i = 0; i < size(); i++) segs.emplace_back((*this)[i], (*this)[(i + 1) % size()]);
        return segs;
    }

    Real area() const {
        Real sum = 0;
        for (size_t i = 0; i < size(); i++) sum += cross((*this)[i], (*this)[(i + 1) % size()]);
        return std::abs(sum) / 2;
    }

    bool is_convex(bool accept_on_segment = true) const {
        int n = size();
        for (int i = 0; i < n; i++) {
            if (accept_on_segment) {
                if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) == CLOCKWISE) {
                    return false;
                }
            } else {
                if (ccw((*this)[i], (*this)[(i + 1) % n], (*this)[(i + 2) % n]) != COUNTER_CLOCKWISE) {
                    return false;
                }
            }
        }
        return true;
    }
};

Contain contain(const Polygon& P, const Point& p) {
    bool in = false;
    for (size_t i = 0; i < P.size(); i++) {
        if (ccw(P[i], P[(i + 1) % P.size()], p) == ON_SEGMENT) return ON;
        Point a = P[i] - p, b = P[(i + 1) % P.size()] - p;
        if (a.y > b.y) std::swap(a, b);
        if (sgn(a.y) <= 0 && sgn(b.y) > 0 && sgn(cross(a, b)) < 0) in = !in;
    }
    return in ? IN : OUT;
}

Contain contain(const Circle& c, const Point& p) {
    Real d = distance(c.center, p);
    int cp = compare(d, c.radius);
    if (cp > 0) return OUT;
    if (cp < 0) return IN;
    return ON;
}

Polygon convex_hull(Polygon P, bool accept_on_segment = false) {
    int n = P.size(), k = 0;
    if (n <= 2) return P;
    std::sort(P.begin(), P.end());
    Polygon ch(n * 2);
    auto check = [&](int i) {
        if (accept_on_segment) return ccw(ch[k - 2], ch[k - 1], P[i]) == CLOCKWISE;
        return ccw(ch[k - 2], ch[k - 1], P[i]) != COUNTER_CLOCKWISE;
    };
    for (int i = 0; i < n; ch[k++] = P[i++]) {
        while (k >= 2 && check(i)) {
            k--;
        }
    }
    for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = P[i--]) {
        while (k >= t && check(i)) {
            k--;
        }
    }
    ch.resize(k - 1);
    int start = 0;
    for (int i = 1; i < k - 1; i++) {
        if (equals(ch[i].y, ch[start].y) ? ch[i].x < ch[start].x : ch[i].y < ch[start].y) {
            start = i;
        }
    }
    std::rotate(ch.begin(), ch.begin() + start, ch.end());
    return ch;
}

std::vector<Point> lower_convex_hull(std::vector<Point> P, bool accept_on_segment = false) {
    std::sort(P.begin(), P.end());
    std::vector<Point> Q;
    for (auto& p : P) {
        if (Q.empty() or p.y < Q.back().y) {
            Q.emplace_back(p);
        }
    }
    swap(P, Q);
    int n = P.size(), k = 0;
    if (n <= 2) return P;
    std::vector<Point> ch(n * 2);
    auto check = [&](int i) {
        if (accept_on_segment) return ccw(ch[k - 2], ch[k - 1], P[i]) == CLOCKWISE;
        return ccw(ch[k - 2], ch[k - 1], P[i]) != COUNTER_CLOCKWISE;
    };
    for (int i = 0; i < n; ch[k++] = P[i++]) {
        while (k >= 2 and check(i)) {
            k--;
        }
    }
    ch.resize(k);
    return ch;
}

std::tuple<int, int, Real> convex_diameter(const Polygon& convex) {
    assert(convex.is_convex());
    int n = convex.size();
    Real max_dist = -1;
    std::pair<int, int> argmax = {-1, -1};
    for (int i = 0, j = 0; i < n; i++) {
        while (j + 1 < n && distance(convex[i], convex[j + 1]) > distance(convex[i], convex[j])) j++;
        double cur_dist = distance(convex[i], convex[j]);
        if (max_dist < cur_dist) {
            max_dist = cur_dist;
            argmax = {i, j};
        }
    }
    return {argmax.first, argmax.second, max_dist};
}

Polygon convex_cut(const Polygon& convex, const Line& l) {
    assert(convex.is_convex());
    int n = convex.size();
    Polygon res;
    for (int i = 0; i < n; i++) {
        const Point& cur = convex[i];
        const Point& nxt = convex[(i + 1) % n];
        if (ccw(l.a, l.b, cur) != CLOCKWISE) res.emplace_back(cur);
        if (ccw(l.a, l.b, cur) * ccw(l.a, l.b, nxt) < 0) res.emplace_back(crosspoint(Segment(cur, nxt), l));
    }
    return res;
}

Polygon voronoi(const Polygon& P, const std::vector<Point>& ps, size_t idx) {
    Polygon res = P;
    for (size_t i = 0; i < ps.size(); i++) {
        if (i == idx) continue;
        res = convex_cut(res, bisector(ps[idx], ps[i]));
    }
    return res;
}

namespace internal {

Real commonarea_impl(const Circle& c, const Point& a, const Point& b) {
    auto va = c.center - a, vb = c.center - b;
    Real f = cross(va, vb), res = 0;
    if (equals(f, 0)) return res;
    if (compare(std::max(va.abs(), vb.abs()), c.radius) <= 0) return f;
    if (compare(distance(Segment(a, b), c.center), c.radius) >= 0) return c.radius * c.radius * (vb * va.conj()).arg();
    auto cand = crosspoint(c, Segment(a, b));
    cand.emplace(cand.begin(), a);
    cand.emplace_back(b);
    for (size_t i = 0; i + 1 < cand.size(); i++) res += commonarea_impl(c, cand[i], cand[i + 1]);
    return res;
}

}  // namespace internal

Real commonarea(const Circle& c, const Polygon& P) {
    if (P.size() < 3) return 0;
    Real res = 0;
    int n = P.size();
    for (int i = 0; i < n; i++) res += internal::commonarea_impl(c, P[i], P[(i + 1) % n]);
    return res / 2;
}

Real closest_pair(std::vector<Point> ps) {
    int n = ps.size();
    if (n == 1) return 0;
    sort(ps.begin(), ps.end());
    auto compare_y = [&](const Point& p, const Point& q) { return p.y < q.y; };
    vector<Point> cand(n);
    const Real inf = 1e18;

    auto dfs = [&](auto self, int l, int r) -> Real {
        if (r - l <= 1) return inf;
        int mid = (l + r) >> 1;
        auto x_mid = ps[mid].x;
        auto res = std::min(self(self, l, mid), self(self, mid, r));
        std::inplace_merge(ps.begin() + l, ps.begin() + mid, ps.begin() + r, compare_y);
        for (int i = l, cur = 0; i < r; i++) {
            if (std::fabs(ps[i].x - x_mid) >= res) continue;
            for (int j = cur - 1; j >= 0; j--) {
                auto diff = ps[i] - cand[j];
                if (diff.y >= res) break;
                res = std::min(res, diff.abs());
            }
            cand[cur++] = ps[i];
        }
        return res;
    };
    return dfs(dfs, 0, n);
}

}  // namespace geometry

using namespace geometry;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, int>>> G(n);
    for (; m--;) {
        int u, v, c;
        cin >> u >> v >> c;
        G[--u].emplace_back(--v, c);
    }

    vector<vector<Point>> dp(n);
    dp[0].emplace_back(0, 0);
    for (int v = 0; v < n; v++) {
        dp[v] = lower_convex_hull(dp[v]);
        for (auto [u, c] : G[v]) {
            for (auto [x, y] : dp[v]) {
                dp[u].emplace_back(x + (c == 0), y + (c == 1));
            }
        }
    }

    auto query = [&](int a, int b, int x) -> ll {
        ll res = IINF;
        for (auto [x, y] : dp[x]) res = min(res, 1LL * a * x + 1LL * b * y);
        return res;
    };
    int q;
    cin >> q;
    for (; q--;) {
        int a, b, x;
        cin >> a >> b >> x;
        cout << query(a, b, --x) << '\n';
    }
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3632kb

input:

4 4
1 2 0
1 3 1
2 4 0
3 4 1
3
3 5 2
3 2 4
2 3 4

output:

3
4
4

result:

ok 3 number(s): "3 4 4"

Test #2:

score: 0
Accepted
time: 55ms
memory: 26108kb

input:

50000 100000
1 2 1
2 3 0
3 4 1
4 5 0
5 6 1
6 7 0
7 8 1
8 9 1
9 10 0
10 11 1
11 12 1
12 13 1
13 14 0
14 15 0
15 16 0
16 17 0
17 18 1
18 19 1
19 20 0
20 21 1
21 22 0
22 23 0
23 24 1
24 25 1
25 26 0
26 27 1
27 28 0
28 29 0
29 30 0
30 31 0
31 32 1
32 33 0
33 34 1
34 35 1
35 36 1
36 37 1
37 38 0
38 39 0
...

output:

164602050
208733870
228180204
248456409
87574800
16685198
46684062
64713954
46949896
240633535
94777502
83016099
259833741
167838804
214963500
147454419
111021650
80187604
184782450
78138570
86528820
203553394
188095596
202049239
290053220
172790198
168899028
97757186
96431009
266952297
164349486
26...

result:

ok 50000 numbers

Test #3:

score: 0
Accepted
time: 80ms
memory: 55396kb

input:

50000 100000
1 2 1
2 3 0
3 4 1
4 5 0
5 6 0
6 7 1
7 8 0
8 9 0
9 10 1
10 11 1
11 12 1
12 13 1
13 14 0
14 15 1
15 16 1
16 17 1
17 18 0
18 19 0
19 20 1
20 21 1
21 22 1
22 23 0
23 24 0
24 25 1
25 26 1
26 27 0
27 28 0
28 29 1
29 30 0
30 31 1
31 32 1
32 33 1
33 34 0
34 35 1
35 36 1
36 37 1
37 38 0
38 39 0
...

output:

100196045
31414400
96903432
4429901
131353947
100466556
96621590
116427456
86564241
138309577
111227766
96757449
98894394
113624940
103437724
32090169
118889544
27383865
145395709
52415186
44958306
178247166
101837390
123750212
66411806
29005113
61920301
53700468
83473964
101048973
24035890
82224385...

result:

ok 50000 numbers

Test #4:

score: 0
Accepted
time: 120ms
memory: 72360kb

input:

50000 100000
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
7 8 0
8 9 0
9 10 0
10 11 1
11 12 0
12 13 0
13 14 1
14 15 0
15 16 1
16 17 0
17 18 0
18 19 1
19 20 0
20 21 0
21 22 0
22 23 0
23 24 1
24 25 1
25 26 0
26 27 1
27 28 1
28 29 0
29 30 0
30 31 1
31 32 1
32 33 1
33 34 1
34 35 1
35 36 1
36 37 1
37 38 0
38 39 1
...

output:

41086586
22479083
65941117
52313422
27188549
98552837
41170647
18070874
39704907
37300025
33494097
12541197
85953980
97466762
54255520
55975530
82137682
80760412
36734523
80227468
57771407
53423371
35392992
38772417
24348238
129485865
50694526
41529532
35522018
64188507
64483980
20809109
88158268
62...

result:

ok 50000 numbers

Test #5:

score: 0
Accepted
time: 162ms
memory: 118560kb

input:

50000 100000
1 2 0
2 3 0
3 4 1
4 5 1
5 6 1
6 7 1
7 8 1
8 9 0
9 10 1
10 11 1
11 12 1
12 13 0
13 14 1
14 15 0
15 16 1
16 17 0
17 18 0
18 19 0
19 20 0
20 21 0
21 22 0
22 23 0
23 24 1
24 25 1
25 26 1
26 27 1
27 28 1
28 29 0
29 30 1
30 31 1
31 32 0
32 33 0
33 34 1
34 35 0
35 36 0
36 37 0
37 38 0
38 39 0
...

output:

21363040
19817072
33235630
2642743
12734703
31561956
16868881
12347713
34007395
31539206
21812869
11469295
13583164
35332256
19432281
13050400
27543307
30865175
23535331
10932941
10731700
8935711
32438529
30147704
11201224
15475486
18918233
29097672
1865520
1717197
10847438
17918300
9085519
22377502...

result:

ok 50000 numbers

Test #6:

score: 0
Accepted
time: 187ms
memory: 117776kb

input:

50000 100000
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 0
7 8 1
8 9 0
9 10 1
10 11 0
11 12 1
12 13 0
13 14 1
14 15 0
15 16 1
16 17 1
17 18 0
18 19 1
19 20 1
20 21 1
21 22 0
22 23 1
23 24 0
24 25 0
25 26 0
26 27 0
27 28 0
28 29 0
29 30 1
30 31 1
31 32 0
32 33 0
33 34 0
34 35 1
35 36 1
36 37 1
37 38 0
38 39 1
...

output:

7034164
2604311
9210306
13276159
7323558
11457505
5477798
10888155
4546292
13775110
4723690
3315532
7247352
14850537
8847725
7929292
5197554
2059467
7544756
2500593
3970752
12419699
9568962
4563223
10626816
3277034
12260607
6928168
4303017
5299690
8738156
9317082
9746787
10042419
13632702
8481147
11...

result:

ok 50000 numbers

Test #7:

score: 0
Accepted
time: 147ms
memory: 77808kb

input:

50000 100000
1 2 1
2 3 1
3 4 1
4 5 0
5 6 0
6 7 0
7 8 1
8 9 0
9 10 0
10 11 1
11 12 1
12 13 0
13 14 1
14 15 0
15 16 1
16 17 1
17 18 0
18 19 1
19 20 0
20 21 0
21 22 0
22 23 1
23 24 1
24 25 0
25 26 1
26 27 1
27 28 0
28 29 1
29 30 1
30 31 1
31 32 0
32 33 1
33 34 1
34 35 0
35 36 1
36 37 1
37 38 1
38 39 1
...

output:

2881748
379663
1968885
883510
1429377
1317566
1691388
425238
1498644
703328
976532
252540
2157673
2046415
2184358
1823525
1052010
1808512
1132815
987060
2350191
1248681
2155738
502600
2363042
1527856
2063953
1042845
914460
1787808
1741740
1992040
730062
1592241
1369515
1084786
1140249
2712241
754218...

result:

ok 50000 numbers

Test #8:

score: 0
Accepted
time: 88ms
memory: 34756kb

input:

50000 100000
1 2 1
2 3 1
3 4 0
4 5 1
5 6 1
6 7 0
7 8 0
8 9 1
9 10 1
10 11 0
11 12 0
12 13 1
13 14 1
14 15 1
15 16 1
16 17 1
17 18 0
18 19 1
19 20 1
20 21 1
21 22 0
22 23 0
23 24 1
24 25 0
25 26 1
26 27 1
27 28 1
28 29 0
29 30 0
30 31 0
31 32 1
32 33 1
33 34 0
34 35 1
35 36 0
36 37 0
37 38 1
38 39 1
...

output:

196425
220970
672134
128953
248040
374496
332056
388195
69312
404828
506828
470044
440937
427759
304069
412812
560795
698232
549476
191135
57468
173200
255364
763568
250616
668286
251232
71252
152194
430194
671010
10672
671999
197794
308836
393499
324938
191963
182472
234993
495528
453559
86128
9629...

result:

ok 50000 numbers

Test #9:

score: 0
Accepted
time: 91ms
memory: 36692kb

input:

50000 100000
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 0
7 8 0
8 9 0
9 10 0
10 11 1
11 12 0
12 13 1
13 14 0
14 15 0
15 16 1
16 17 1
17 18 0
18 19 1
19 20 1
20 21 0
21 22 1
22 23 1
23 24 0
24 25 0
25 26 0
26 27 0
27 28 0
28 29 0
29 30 0
30 31 0
31 32 1
32 33 0
33 34 1
34 35 1
35 36 0
36 37 0
37 38 0
38 39 1
...

output:

468255
105020
312484
469028
46500
433476
348092
241180
416482
136152
497776
571977
530352
219162
562176
138675
705269
353652
287899
214580
13380
294272
27828
132826
749244
506820
295440
417272
370316
114926
552490
6486
834970
359255
601821
208311
337232
101539
489665
169404
93039
64173
382258
775608...

result:

ok 50000 numbers

Test #10:

score: 0
Accepted
time: 74ms
memory: 33084kb

input:

50000 100000
1 2 0
2 3 0
3 4 0
4 5 0
5 6 1
6 7 1
7 8 0
8 9 1
9 10 1
10 11 1
11 12 0
12 13 1
13 14 0
14 15 0
15 16 1
16 17 1
17 18 1
18 19 0
19 20 0
20 21 0
21 22 1
22 23 0
23 24 1
24 25 0
25 26 0
26 27 0
27 28 0
28 29 0
29 30 0
30 31 0
31 32 0
32 33 0
33 34 1
34 35 1
35 36 0
36 37 0
37 38 0
38 39 1
...

output:

663010
158597
758130
682380
443574
514836
623881
348012
155945
542531
400539
240444
166313
307926
556860
383720
97095
367865
372270
204195
787005
79165
455970
495416
156456
33165
223166
481715
416854
524138
316025
105263
274806
781025
177352
653420
491795
743770
506808
72480
505734
444805
198372
700...

result:

ok 50000 numbers

Test #11:

score: 0
Accepted
time: 26ms
memory: 14300kb

input:

50000 99998
1 2 0
1 2 1
2 3 0
2 3 1
3 4 0
3 4 1
4 5 0
4 5 1
5 6 0
5 6 1
6 7 0
6 7 1
7 8 0
7 8 1
8 9 0
8 9 1
9 10 0
9 10 1
10 11 0
10 11 1
11 12 0
11 12 1
12 13 0
12 13 1
13 14 0
13 14 1
14 15 0
14 15 1
15 16 0
15 16 1
16 17 0
16 17 1
17 18 0
17 18 1
18 19 0
18 19 1
19 20 0
19 20 1
20 21 0
20 21 1
21...

output:

357392852
286944261
25899482
106697866
266744665
188046239
246595068
282194356
149697006
36449271
55148897
105197896
112647747
361542769
153346933
426991460
799984
17749645
256444871
329993400
275444491
344593108
81998360
305443891
233695326
82148357
92148157
17449651
36449271
34349313
175796484
354...

result:

ok 50000 numbers

Test #12:

score: 0
Accepted
time: 219ms
memory: 328636kb

input:

50000 50299
1 2 0
2 3 0
3 4 0
4 5 0
5 6 0
6 7 0
7 8 0
8 9 0
9 10 0
10 11 0
11 12 0
12 13 0
13 14 0
14 15 0
15 16 0
16 17 0
17 18 0
18 19 0
19 20 0
20 21 0
21 22 0
22 23 0
23 24 0
24 25 0
25 26 0
26 27 0
27 28 0
28 29 0
29 30 0
30 31 0
31 32 0
32 33 0
33 34 0
34 35 0
35 36 0
36 37 0
37 38 0
38 39 0
3...

output:

28885380
45069427
33432760
30206043
5611707
40119773
20814082
10397200
11616787
7910426
49163370
9368174
31732491
43615079
41538187
27076798
41917819
32019460
17871476
14080806
5899035
42800174
14990686
29049896
25022003
9316314
27915136
19878279
43675167
30658188
2990993
2704160
12805154
19507614
5...

result:

ok 50000 numbers