diff --git a/BZOJ/3669/3669.cpp b/BZOJ/3669/3669.cpp new file mode 100644 index 00000000..4dbf368f --- /dev/null +++ b/BZOJ/3669/3669.cpp @@ -0,0 +1,237 @@ +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 50005, + M = 1e5 + 5; + +class LinkCutTree { + private: + struct node { + size_t l, r, f; + int v, m; + unsigned id; + bool rev; + + node() + : l(0), r(0), f(0), v(0), m(std::numeric_limits::min()), id(0), rev(false) {} + + node(int _v, size_t _f, unsigned _id) + : l(0), r(0), f(_f), v(_v), m(_v), id(_id), rev(false) {} + + size_t &child(unsigned x) { + return !x ? l : r; + } + } tr[N + M]; + + inline void pushup(size_t u) { + tr[u].m = tr[u].v; + tr[u].id = u; + + if (tr[u].l && tr[u].m < tr[tr[u].l].m) { + tr[u].m = tr[tr[u].l].m; + tr[u].id = tr[tr[u].l].id; + } + + if (tr[u].r && tr[u].m < tr[tr[u].r].m) { + tr[u].m = tr[tr[u].r].m; + tr[u].id = tr[tr[u].r].id; + } + } + + inline void pushdown(const size_t &u) { + if (!tr[u].rev) return; + + std::swap(tr[u].l, tr[u].r); + tr[tr[u].l].rev = !tr[tr[u].l].rev; + tr[tr[u].r].rev = !tr[tr[u].r].rev; + tr[u].rev = false; + } + + unsigned relation(const size_t &u) { + return u == tr[tr[u].f].l ? 0 : 1; + } + + bool isRoot(const size_t &u) { + return tr[tr[u].f].l != u && tr[tr[u].f].r != u; + } + + void rotate(size_t u) { + size_t p = tr[u].f; + unsigned x = relation(u); + + if (!isRoot(p)) { + tr[tr[p].f].child(relation(p)) = u; + } + tr[u].f = tr[p].f; + + if (tr[u].child(x ^ 1)) { + tr[tr[u].child(x ^ 1)].f = p; + } + tr[p].child(x) = tr[u].child(x ^ 1); + + tr[u].child(x ^ 1) = p; + tr[p].f = u; + + pushup(p); + pushup(u); + } + + void splay(size_t u) { + std::stack st; + + size_t cur = u; + st.push(cur); + while (!isRoot(cur)) { + st.push(tr[cur].f); + cur = tr[cur].f; + } + + while (!st.empty()) { + pushdown(st.top()); + st.pop(); + } + + while (!isRoot(u)) { + if (isRoot(tr[u].f)) { + rotate(u); + } else if (relation(u) == relation(tr[u].f)) { + rotate(tr[u].f); + rotate(u); + } else { + rotate(u); + rotate(u); + } + } + } + + void access(size_t u) { + for (size_t f = 0; u; u = tr[f = u].f) { + splay(u); + tr[u].r = f; + pushup(u); + } + } + + void makeRoot(const size_t &u) { + access(u); + splay(u); + tr[u].rev = !tr[u].rev; + } + + size_t findRoot(size_t u) { + access(u); + splay(u); + + while (tr[u].l) { + u = tr[u].l; + } + + return u; + } + + public: + void set(int p, int v) { + tr[p].m = tr[p].v = v; + } + + int query(int x, int y) { + split(x, y); + + return tr[y].m; + } + + void split(const size_t &x, const size_t &y) { + makeRoot(x); + access(y); + splay(y); + } + + void link(const int &x, const int &y) { + makeRoot(x); + + if (findRoot(y) != x) { + tr[x].f = y; + } + } + + void cut(int x, int y) { + split(x, y); + + if (tr[y].l == x) { + tr[y].l = 0; + tr[x].f = 0; + } + } + + bool isConnected(const int &x, const int &y) { + makeRoot(x); + + return x == findRoot(y); + } + + const unsigned &id(const int x) { + return tr[x].id; + } + + const int &max(const int &x) { + return tr[x].m; + } +} lct; + +int n, m, ans = std::numeric_limits::max(); + +struct node { + int x, y, a, b; +} q[M]; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> m; + + for (int i = 1; i <= m; i++) { + cin >> q[i].x >> q[i].y >> q[i].a >> q[i].b; + } + + std::sort(q + 1, q + m + 1, [](const node &a, const node &b) { + return a.a == b.a ? a.b < b.b : a.a < b.a; + }); + + for (int i = 1; i <= m; i++) { + lct.set(i + n, q[i].b); + + if (q[i].x == q[i].y) continue; + + if (lct.isConnected(q[i].x, q[i].y)) { + lct.split(q[i].x, q[i].y); + + int x = lct.id(q[i].y), y = lct.max(q[i].y); + int a = q[x - n].x, b = q[x - n].y; + + if (y > q[i].b) { + lct.cut(a, x); + lct.cut(x, b); + lct.link(q[i].x, i + n); + lct.link(i + n, q[i].y); + } + } else { + lct.link(q[i].x, i + n); + lct.link(i + n, q[i].y); + } + + if (lct.isConnected(1, n)) { + ans = std::min(ans, lct.query(1, n) + q[i].a); + } + } + + cout << (ans == std::numeric_limits::max() ? -1 : ans) << endl; + + return 0; +} diff --git a/BZOJ/3669/data/1.in b/BZOJ/3669/data/1.in new file mode 100644 index 00000000..06f7d742 --- /dev/null +++ b/BZOJ/3669/data/1.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:79b9d77298c67430adc23a6d9477c160764d97848434ea4496d70f5b588032f2 +size 2003884 diff --git a/BZOJ/3669/data/1.out b/BZOJ/3669/data/1.out new file mode 100644 index 00000000..0895bf82 --- /dev/null +++ b/BZOJ/3669/data/1.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ca60d1ba11e7fc586d12a4c226b3f80ae5b89811dc7c0342ef182efe9e7f5ad9 +size 6 diff --git a/BZOJ/3669/data/10.in b/BZOJ/3669/data/10.in new file mode 100644 index 00000000..bd6bafa8 --- /dev/null +++ b/BZOJ/3669/data/10.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8a1feca2bf7c30d2d37ab8d4c5c04a89c33c6d2878e0734461d70ec5c0397fa0 +size 2198818 diff --git a/BZOJ/3669/data/10.out b/BZOJ/3669/data/10.out new file mode 100644 index 00000000..2dc51405 --- /dev/null +++ b/BZOJ/3669/data/10.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:af2cc29044585d0d1d9455d29d4478ba3d77c5be78fe382b991793032747961c +size 5 diff --git a/BZOJ/3669/data/11.in b/BZOJ/3669/data/11.in new file mode 100644 index 00000000..82cd9d5c --- /dev/null +++ b/BZOJ/3669/data/11.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4171822d54c8fa1c53e19e7217a543235d5ece0578b448cdb4632c9cd0c74c69 +size 2259063 diff --git a/BZOJ/3669/data/11.out b/BZOJ/3669/data/11.out new file mode 100644 index 00000000..c3284be1 --- /dev/null +++ b/BZOJ/3669/data/11.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:dfd6462ab5ede51dc5decb59fdb2e25f95c0612b48dd80c79d66df59c68df072 +size 6 diff --git a/BZOJ/3669/data/12.in b/BZOJ/3669/data/12.in new file mode 100644 index 00000000..88fd8e12 --- /dev/null +++ b/BZOJ/3669/data/12.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fb658048f87f760430d7192c8476bf915342aa0822e42a661c36b529ac660b4b +size 87 diff --git a/BZOJ/3669/data/12.out b/BZOJ/3669/data/12.out new file mode 100644 index 00000000..d595cdb8 --- /dev/null +++ b/BZOJ/3669/data/12.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:7de1555df0c2700329e815b93b32c571c3ea54dc967b89e81ab73b9972b72d1d +size 2 diff --git a/BZOJ/3669/data/13.in b/BZOJ/3669/data/13.in new file mode 100644 index 00000000..585de9ac --- /dev/null +++ b/BZOJ/3669/data/13.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:bec895074755fa9accd3df0de3a6a5ae5e1de4b4802a7926e59299bb6221fca6 +size 2220928 diff --git a/BZOJ/3669/data/13.out b/BZOJ/3669/data/13.out new file mode 100644 index 00000000..3b60ed9e --- /dev/null +++ b/BZOJ/3669/data/13.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:6ef69cbd08d0c24a526a381ecc296116f339bda071b2b31dc0dcebfaf4e6b116 +size 6 diff --git a/BZOJ/3669/data/14.in b/BZOJ/3669/data/14.in new file mode 100644 index 00000000..b5966e1b --- /dev/null +++ b/BZOJ/3669/data/14.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e3cd216812f114c035a17394a604e0fec38bbc13310066308c1d42356ba0adc8 +size 57209 diff --git a/BZOJ/3669/data/14.out b/BZOJ/3669/data/14.out new file mode 100644 index 00000000..dc19836f --- /dev/null +++ b/BZOJ/3669/data/14.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d2cf6b3b546c41c49583f319c2fe210b46182fcb771f4cf6a99fe59bbce0b4ba +size 6 diff --git a/BZOJ/3669/data/15.in b/BZOJ/3669/data/15.in new file mode 100644 index 00000000..b35aa9ee --- /dev/null +++ b/BZOJ/3669/data/15.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9bb6521ff61f952b151faa6ae307096e46beba983ce5dcd858c64e66b951e039 +size 57258 diff --git a/BZOJ/3669/data/15.out b/BZOJ/3669/data/15.out new file mode 100644 index 00000000..0d416d8c --- /dev/null +++ b/BZOJ/3669/data/15.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:aa6b40405953af8be26f591f47ac6c2874d07eb75732abb38ca73c05192420b9 +size 6 diff --git a/BZOJ/3669/data/16.in b/BZOJ/3669/data/16.in new file mode 100644 index 00000000..3933bbd7 --- /dev/null +++ b/BZOJ/3669/data/16.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4044e595a8f7e2441fec0cdfe11fda1f114958d57376e5210f9b97382647ea99 +size 2003152 diff --git a/BZOJ/3669/data/16.out b/BZOJ/3669/data/16.out new file mode 100644 index 00000000..802e6301 --- /dev/null +++ b/BZOJ/3669/data/16.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4f40bd16338fb167189afcb3e09c58d8fd2c19d2dbdd457e07249c4f4b3dd82 +size 6 diff --git a/BZOJ/3669/data/17.in b/BZOJ/3669/data/17.in new file mode 100644 index 00000000..27bac022 --- /dev/null +++ b/BZOJ/3669/data/17.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:e93721c2cf9ef476d568f814739771e60bdd931a42eb5b4d201b8edf1030d43b +size 87 diff --git a/BZOJ/3669/data/17.out b/BZOJ/3669/data/17.out new file mode 100644 index 00000000..311c4506 --- /dev/null +++ b/BZOJ/3669/data/17.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:ee3aa64bb94a50845d5024cd4bd20202a4567aed5cd5328c0d97e9920775fc28 +size 3 diff --git a/BZOJ/3669/data/18.in b/BZOJ/3669/data/18.in new file mode 100644 index 00000000..ad34ee9d --- /dev/null +++ b/BZOJ/3669/data/18.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:743bbc0bacd062db4564712a369b0f9bcd9775dfeca0666c26d350d6b9613f28 +size 2215771 diff --git a/BZOJ/3669/data/18.out b/BZOJ/3669/data/18.out new file mode 100644 index 00000000..cc4d3c5a --- /dev/null +++ b/BZOJ/3669/data/18.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:403455693f38c1c2b33004833d775fa6513e87ee3364eda3dd39e8295e3b75f5 +size 6 diff --git a/BZOJ/3669/data/19.in b/BZOJ/3669/data/19.in new file mode 100644 index 00000000..b09fab60 --- /dev/null +++ b/BZOJ/3669/data/19.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:50c3f96ccabd2bd5735cff637394bc2f439fc6f223cc7b4cee2fd9165769fac1 +size 210705 diff --git a/BZOJ/3669/data/19.out b/BZOJ/3669/data/19.out new file mode 100644 index 00000000..3f091cf5 --- /dev/null +++ b/BZOJ/3669/data/19.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:9e503b95ae93c386e6f2269ba527e10a618d948da75d52fb618c0d8abfa96eef +size 6 diff --git a/BZOJ/3669/data/2.in b/BZOJ/3669/data/2.in new file mode 100644 index 00000000..14560d07 --- /dev/null +++ b/BZOJ/3669/data/2.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:4f73ae01816ab65564cef4c01001cc63c9834e21c0d86d14ccee04cbe986ec1a +size 2224071 diff --git a/BZOJ/3669/data/2.out b/BZOJ/3669/data/2.out new file mode 100644 index 00000000..f964486d --- /dev/null +++ b/BZOJ/3669/data/2.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d17373a37b8d198965c5d14c9874a9550fead1f881da9c0cc1cd40f4382ea3c3 +size 6 diff --git a/BZOJ/3669/data/20.in b/BZOJ/3669/data/20.in new file mode 100644 index 00000000..c0ed2078 --- /dev/null +++ b/BZOJ/3669/data/20.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:72b885f08b5ce21d9483d9cd08470132dbdbe5319e7c113d67c1e28de05378ac +size 2254500 diff --git a/BZOJ/3669/data/20.out b/BZOJ/3669/data/20.out new file mode 100644 index 00000000..e47355b8 --- /dev/null +++ b/BZOJ/3669/data/20.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:d55a2caa7734865a691e60772c8b32d02585ec890a98cc35e54013c6c0857b7b +size 6 diff --git a/BZOJ/3669/data/3.in b/BZOJ/3669/data/3.in new file mode 100644 index 00000000..e80f38fc --- /dev/null +++ b/BZOJ/3669/data/3.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:38602fb3163a5f25e704c77f544f4ef9260763dfd92b87aa90f8f16aacfb1720 +size 200963 diff --git a/BZOJ/3669/data/3.out b/BZOJ/3669/data/3.out new file mode 100644 index 00000000..2adfce74 --- /dev/null +++ b/BZOJ/3669/data/3.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:245c8b184b5c0c6ab4fe06e159eb7d9a122e250ee37aa439280045ff331de52c +size 5 diff --git a/BZOJ/3669/data/4.in b/BZOJ/3669/data/4.in new file mode 100644 index 00000000..c47938d8 --- /dev/null +++ b/BZOJ/3669/data/4.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:089b093a620199fdd039281a2dec88d5758598cf92d0021a6f9b785ad98b295a +size 2003300 diff --git a/BZOJ/3669/data/4.out b/BZOJ/3669/data/4.out new file mode 100644 index 00000000..e2297d42 --- /dev/null +++ b/BZOJ/3669/data/4.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:57b59c616e9995dfbdd1b7a375dd07c9d405795812b28f636b283aeeed2db8cb +size 6 diff --git a/BZOJ/3669/data/5.in b/BZOJ/3669/data/5.in new file mode 100644 index 00000000..9daaf38e --- /dev/null +++ b/BZOJ/3669/data/5.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f4bab0d01d5a740a2ae6b8462227f79d6bec62de7bdfffee847088fd4e79de7c +size 210487 diff --git a/BZOJ/3669/data/5.out b/BZOJ/3669/data/5.out new file mode 100644 index 00000000..be39611c --- /dev/null +++ b/BZOJ/3669/data/5.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:96a8f23229102b40001a599c1f40c51c65aa640b2d636fbbd9f95ab209c3bc3f +size 6 diff --git a/BZOJ/3669/data/6.in b/BZOJ/3669/data/6.in new file mode 100644 index 00000000..47b772d2 --- /dev/null +++ b/BZOJ/3669/data/6.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:c7e1dc2c64125700cf222b53c957b9653275f92d2c2b3fb671c6e210df4725bc +size 56473 diff --git a/BZOJ/3669/data/6.out b/BZOJ/3669/data/6.out new file mode 100644 index 00000000..02a58993 --- /dev/null +++ b/BZOJ/3669/data/6.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:b698536710d829897899dae60050787781cfef1f11cf5c7377764918697de3d7 +size 6 diff --git a/BZOJ/3669/data/7.in b/BZOJ/3669/data/7.in new file mode 100644 index 00000000..0732484a --- /dev/null +++ b/BZOJ/3669/data/7.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:faad0683f5c3d021edb51b0987d1e9af3102fca8daf4f3369aa57d39f045aa5c +size 87 diff --git a/BZOJ/3669/data/7.out b/BZOJ/3669/data/7.out new file mode 100644 index 00000000..c7967cd5 --- /dev/null +++ b/BZOJ/3669/data/7.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:25d4f2a86deb5e2574bb3210b67bb24fcc4afb19f93a7b65a057daa874a9d18e +size 3 diff --git a/BZOJ/3669/data/8.in b/BZOJ/3669/data/8.in new file mode 100644 index 00000000..8b08c602 --- /dev/null +++ b/BZOJ/3669/data/8.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:8c5599873783cd2a8fb7edc21a650ea36426f32e6e0365ee0b67f420e367e4c8 +size 2003864 diff --git a/BZOJ/3669/data/8.out b/BZOJ/3669/data/8.out new file mode 100644 index 00000000..6c2b9ffb --- /dev/null +++ b/BZOJ/3669/data/8.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fca28fdf958320d4f970543033a3b0dbee39b1d31d5ac1768058f37534764252 +size 6 diff --git a/BZOJ/3669/data/9.in b/BZOJ/3669/data/9.in new file mode 100644 index 00000000..5a04614e --- /dev/null +++ b/BZOJ/3669/data/9.in @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:81df68f0aa3761f7aec17e69d5670b69fd21e463b2702802d66abea100a57613 +size 203536 diff --git a/BZOJ/3669/data/9.out b/BZOJ/3669/data/9.out new file mode 100644 index 00000000..58109a51 --- /dev/null +++ b/BZOJ/3669/data/9.out @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fbc243fc66f75d0a320e829239cdf90e0a84df8688755fec5bf6e27576e859ce +size 6