# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
223201 | dwsc | Putovanje (COCI20_putovanje) | C++14 | 676 ms | 27128 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef pair<ii,ii> iv;
vector<int> adj[200005];
int heavy[200005],depth[200005],head[200005], p[200005], pos[200005], sz[200005];
int cnt = 0; ///set to 1 if you're using fenwick tree
void dfs(int u){
sz[u] = 1;
int maxChild = 0;
for(int v : adj[u]){
if(sz[v] == 0){
depth[v] = depth[u] + 1;
dfs(v);
sz[u] += sz[v];
p[v] = u;
if(sz[v] > maxChild){
maxChild = sz[v];
heavy[u] = v;
}
}
}
}
void decompose(int u, int h){
head[u] = h;
pos[u] = cnt;
cnt++;
if(heavy[u] != 0) decompose(heavy[u], h);
for(int v : adj[u]){
if(sz[v] < sz[u] && v != heavy[u]){
decompose(v,v);
}
}
}
struct node {
node *l, *r;
int val, s, m, e, lazyadd;
node(int _s, int _e): s(_s), e(_e), m((_s+_e)/2), val(0), lazyadd(0), l(NULL), r(NULL) {
if (s != e) l = new node(s, m), r = new node(m+1, e);
}
int value() { //returns the value of the current node after lazy propagating
if (s == e) return val + lazyadd;
val += lazyadd;
l->lazyadd += lazyadd, r->lazyadd += lazyadd;
lazyadd = 0;
return val;
}
void add(int x, int y, int v) {
if (x > y) return;
if (s == x && e == y) lazyadd += v;
else {
if (x > m) r->add(x, y, v);
else if (y <= m) l->add(x, y, v);
else l->add(x, m, v), r->add(m+1, y, v);
val = min(l->value(), r->value()); //Change here to max
}
}
int query(int x, int y) {
value();
if (s == x && e == y) return value();
if (x > m) return r->query(x, y);
if (y <= m) return l->query(x, y);
return min(l->query(x, m),r->query(m+1, y)); //Change here to max
}
}*root;
void update(int a,int b){
//cout << a << " " << b << "\n";
if(depth[a] > depth[b]) swap(a,b);
for(;head[a] != head[b];b = p[head[b]]){
if(depth[head[a]] > depth[head[b]]) swap(a,b);
root->add(pos[head[b]],pos[b],1);
///any update and query affects pos[head[b]] inclusive to pos[b] inclusive
}
//cout << a << " " << b << "hi\n";
//cout << pos[a] << " " << pos[b] << "\n";
if(depth[a] > depth[b]) swap(a,b);
root->add(pos[a]+1,pos[b],1);
}
int main(){
int n;
cin >> n;
iv edge[n];
root = new node(0,n-1);
for (int i = 0; i < n-1; i++){
int a,b,x,y;
cin >> a >> b >> x >> y;
a--;
b--;
edge[i] = iv(ii(a,b),ii(x,y));
adj[a].push_back(b);
adj[b].push_back(a);
}
dfs(0);
decompose(0,0);
for (int i = 0; i < n-1; i++) update(i,i+1);
long long ans = 0;
for (int i = 0; i < n-1; i++){
int a = edge[i].first.first,b = edge[i].first.second;
long long x = edge[i].second.first,y = edge[i].second.second;
if (depth[a] < depth[b]) swap(a,b);
int val = root->query(pos[a],pos[a]);
//cout << a << " " << val << " " << x << " " << y << "\n";
if (val != 0) ans += min(y,x*val);
}
cout << ans;
///any update and query affects pos[a]+1 inclusive to pos[b] inclusive
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |