QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#641886 | #2429. Conquer The World | Elegia | AC ✓ | 492ms | 102236kb | C++14 | 3.8kb | 2024-10-15 03:08:05 | 2024-10-15 03:08:06 |
Judging History
answer
#include <bits/stdc++.h>
#define LOG(FMT...) fprintf(stderr, FMT)
#define fir first
#define sec second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class T>
istream &operator>>(istream &is, vector<T> &v) {
for (T &x : v)
is >> x;
return is;
}
ostream &operator<<(ostream &os, const pair<char, int> &unit) {
return os << unit.first << "^" << unit.second;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
if (!v.empty()) {
os << v.front();
for (int i = 1; i < v.size(); ++i)
os << ' ' << v[i];
}
return os;
}
const int M = 1000010;
struct Node {
ll key, lazy;
int ls, rs, dist;
} P[M << 1];
int top;
void update(int o) {
if (P[P[o].ls].dist < P[P[o].rs].dist) swap(P[o].ls, P[o].rs);
P[o].dist = P[P[o].rs].dist + 1;
}
void apply(int o, ll x) {
P[o].lazy += x;
P[o].key += x;
}
void pushdown(int o) {
if (P[o].lazy) {
apply(P[o].ls, P[o].lazy);
apply(P[o].rs, P[o].lazy);
P[o].lazy = 0;
}
}
int create(ll x) {
int o = ++top;
P[o].key = x;
return o;
}
template<class Compare>
struct LeftistManager {
Compare comp;
int merge(int x, int y) {
if (x == 0 || y == 0) return x ^ y;
pushdown(x);
pushdown(y);
if (!comp(P[x].key, P[y].key)) swap(x, y);
P[x].rs = merge(P[x].rs, y);
update(x);
return x;
}
pair<int, int> pop(int o) {
pushdown(o);
int x = o, y = merge(P[o].ls, P[o].rs);
P[x].ls = P[x].rs = P[x].dist = 0;
return make_pair(x, y);
}
};
LeftistManager<less<ll>> le;
LeftistManager<greater<ll>> ge;
const int N = 250010;
struct Convex {
int ls, rs, lc, k;
ll b;
void balance() {
while (k > lc && rs) {
int x;
tie(x, rs) = le.pop(rs);
ls = ge.merge(ls, x);
++lc;
}
while (k < lc) {
int x;
tie(x, ls) = ge.pop(ls);
rs = le.merge(rs, x);
--lc;
}
}
void correct() {
while (ls && rs && P[ls].key > P[rs].key) {
int x, y;
tie(x, ls) = ge.pop(ls);
tie(y, rs) = le.pop(rs);
ls = ge.merge(ls, y);
rs = le.merge(rs, x);
}
}
void inc() {
++k;
balance();
}
void ins() {
int o = create(0);
if (ls && P[ls].key >= 0) {
ls = ge.merge(ls, o);
++lc;
} else rs = le.merge(rs, o);
balance();
}
void val(ll x) {
b += x * k;
apply(ls, -x);
apply(rs, x);
}
};
int n;
vector<pair<int, int>> g[N];
bool vis[N];
int x[N], y[N];
Convex ds[N];
void dfs(int u) {
vis[u] = true;
for (const auto &pr : g[u])
if (!vis[pr.first]) {
int v, w;
tie(v, w) = pr;
dfs(v);
ds[v].val(w);
ds[u].ls = ge.merge(ds[u].ls, ds[v].ls);
ds[u].rs = le.merge(ds[u].rs, ds[v].rs);
ds[u].k += ds[v].k;
ds[u].lc += ds[v].lc;
ds[u].b += ds[v].b;
ds[u].balance();
ds[u].correct();
}
while (x[u] > y[u]) {
--x[u];
ds[u].ins();
}
while (y[u] > x[u]) {
--y[u];
ds[u].inc();
}
}
int main() {
#ifdef ELEGIA
freopen("test.in", "r", stdin);
int nol_cl = clock();
#endif
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int rep = 1; rep < n; ++rep) {
int u, v, w;
cin >> u >> v >> w;
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
for (int i = 1; i <= n; ++i) cin >> x[i] >> y[i];
dfs(1);
int o = ds[1].ls;
ll ans = ds[1].b;
while (o) {
ans += P[o].key;
o = ge.pop(o).second;
}
cout << ans << '\n';
#ifdef ELEGIA
LOG("Time: %dms\n", int ((clock()
-nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
memory: 13908kb
Test #2:
score: 0
Accepted
time: 3ms
memory: 13912kb
Test #3:
score: 0
Accepted
time: 3ms
memory: 13964kb
Test #4:
score: 0
Accepted
time: 0ms
memory: 13900kb
Test #5:
score: 0
Accepted
time: 0ms
memory: 13832kb
Test #6:
score: 0
Accepted
time: 0ms
memory: 13916kb
Test #7:
score: 0
Accepted
time: 0ms
memory: 13780kb
Test #8:
score: 0
Accepted
time: 0ms
memory: 13960kb
Test #9:
score: 0
Accepted
time: 8ms
memory: 29784kb
Test #10:
score: 0
Accepted
time: 9ms
memory: 22188kb
Test #11:
score: 0
Accepted
time: 24ms
memory: 40796kb
Test #12:
score: 0
Accepted
time: 3ms
memory: 13972kb
Test #13:
score: 0
Accepted
time: 0ms
memory: 14008kb
Test #14:
score: 0
Accepted
time: 0ms
memory: 14116kb
Test #15:
score: 0
Accepted
time: 0ms
memory: 14036kb
Test #16:
score: 0
Accepted
time: 0ms
memory: 13984kb
Test #17:
score: 0
Accepted
time: 2ms
memory: 11956kb
Test #18:
score: 0
Accepted
time: 0ms
memory: 16048kb
Test #19:
score: 0
Accepted
time: 8ms
memory: 20060kb
Test #20:
score: 0
Accepted
time: 31ms
memory: 41900kb
Test #21:
score: 0
Accepted
time: 157ms
memory: 25116kb
Test #22:
score: 0
Accepted
time: 152ms
memory: 25016kb
Test #23:
score: 0
Accepted
time: 162ms
memory: 27540kb
Test #24:
score: 0
Accepted
time: 67ms
memory: 22084kb
Test #25:
score: 0
Accepted
time: 92ms
memory: 23480kb
Test #26:
score: 0
Accepted
time: 169ms
memory: 28308kb
Test #27:
score: 0
Accepted
time: 142ms
memory: 45424kb
Test #28:
score: 0
Accepted
time: 169ms
memory: 33436kb
Test #29:
score: 0
Accepted
time: 213ms
memory: 59136kb
Test #30:
score: 0
Accepted
time: 215ms
memory: 61132kb
Test #31:
score: 0
Accepted
time: 185ms
memory: 59944kb
Test #32:
score: 0
Accepted
time: 179ms
memory: 60500kb
Test #33:
score: 0
Accepted
time: 149ms
memory: 59960kb
Test #34:
score: 0
Accepted
time: 260ms
memory: 60576kb
Test #35:
score: 0
Accepted
time: 182ms
memory: 61292kb
Test #36:
score: 0
Accepted
time: 167ms
memory: 60328kb
Test #37:
score: 0
Accepted
time: 349ms
memory: 60428kb
Test #38:
score: 0
Accepted
time: 289ms
memory: 99084kb
Test #39:
score: 0
Accepted
time: 234ms
memory: 85232kb
Test #40:
score: 0
Accepted
time: 266ms
memory: 59844kb
Test #41:
score: 0
Accepted
time: 226ms
memory: 64708kb
Test #42:
score: 0
Accepted
time: 202ms
memory: 66476kb
Test #43:
score: 0
Accepted
time: 155ms
memory: 67700kb
Test #44:
score: 0
Accepted
time: 153ms
memory: 75824kb
Test #45:
score: 0
Accepted
time: 183ms
memory: 73592kb
Test #46:
score: 0
Accepted
time: 3ms
memory: 13892kb
Test #47:
score: 0
Accepted
time: 12ms
memory: 44680kb
Test #48:
score: 0
Accepted
time: 0ms
memory: 13968kb
Test #49:
score: 0
Accepted
time: 185ms
memory: 59944kb
Test #50:
score: 0
Accepted
time: 123ms
memory: 27924kb
Test #51:
score: 0
Accepted
time: 0ms
memory: 13952kb
Test #52:
score: 0
Accepted
time: 2ms
memory: 13844kb
Test #53:
score: 0
Accepted
time: 2ms
memory: 13900kb
Test #54:
score: 0
Accepted
time: 2ms
memory: 13832kb
Test #55:
score: 0
Accepted
time: 0ms
memory: 13900kb
Test #56:
score: 0
Accepted
time: 2ms
memory: 13904kb
Test #57:
score: 0
Accepted
time: 2ms
memory: 13960kb
Test #58:
score: 0
Accepted
time: 3ms
memory: 14516kb
Test #59:
score: 0
Accepted
time: 48ms
memory: 26100kb
Test #60:
score: 0
Accepted
time: 129ms
memory: 40028kb
Test #61:
score: 0
Accepted
time: 256ms
memory: 54848kb
Test #62:
score: 0
Accepted
time: 475ms
memory: 58852kb
Test #63:
score: 0
Accepted
time: 118ms
memory: 59384kb
Test #64:
score: 0
Accepted
time: 110ms
memory: 58216kb
Test #65:
score: 0
Accepted
time: 126ms
memory: 58776kb
Test #66:
score: 0
Accepted
time: 492ms
memory: 59708kb
Test #67:
score: 0
Accepted
time: 172ms
memory: 85600kb
Test #68:
score: 0
Accepted
time: 203ms
memory: 98592kb
Test #69:
score: 0
Accepted
time: 152ms
memory: 102236kb
Test #70:
score: 0
Accepted
time: 220ms
memory: 99160kb