이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define MAX 410000
int n, par[25][MAX], first[MAX], last[MAX], tym, br[25][MAX], tree[MAX * 2], lazy[MAX * 2];
map<pair<int, int>, int > mp;
long long ans;
vector <int> gr[MAX];
bool used[MAX];
struct edge
{
int a, b, c, d;
} e[MAX];
void dfs(int v, int pr)
{
used[v] = true;
first[v] = ++tym;
par[0][v] = pr;
for(int i = 1; i < 25; i++)
{
par[i][v] = par[i - 1][par[i - 1][v]];
}
for(int i = 0; i < gr[v].size(); i++)
{
if(!used[gr[v][i]])
{
dfs(gr[v][i], v);
}
}
last[v] = ++tym;
return;
}
bool ancestor(int a, int b)
{
return (first[a] <= first[b] && last[b] <= last[a]);
}
void lca(int a, int b)
{
int tmp = a;
for(int i = 24; i >= 0; i--)
{
if(!ancestor(par[i][tmp], b))
{
br[i][tmp]++;
tmp = par[i][tmp];
}
}
if(!ancestor(tmp, b))br[0][tmp]++;
tmp = b;
for(int i = 24; i >= 0; i--)
{
if(!ancestor(par[i][tmp], a))
{
br[i][tmp]++;
tmp = par[i][tmp];
}
}
if(!ancestor(tmp, a))br[0][tmp]++;
return ;
}
void update(int v, int l, int r, int ql, int qr, int val)
{
if(lazy[v] != 0)
{
if(v * 2 + 1 < 2 * MAX)
{
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
}
tree[v] += (r - l + 1) * lazy[v];
lazy[v] = 0;
}
if(ql <= l && r <= qr)
{
if(v * 2 + 1 < 2 * MAX)
{
lazy[v * 2] += val;
lazy[v * 2 + 1] += val;
}
tree[v] += (r - l + 1) * val;
return;
}
if(l > qr || r < ql)return ;
int mid = (l + r) / 2;
update(v * 2, l, mid, ql, qr, val);
update(v * 2 + 1, mid + 1, r, ql, qr, val);
tree[v] = tree[v * 2] + tree[v * 2 + 1];
return;
}
int query(int v, int l, int r, int ql, int qr)
{
if(lazy[v] != 0)
{
if(v * 2 + 1 < 2 * MAX)
{
lazy[v * 2] += lazy[v];
lazy[v * 2 + 1] += lazy[v];
}
tree[v] += (r - l + 1) * lazy[v];
lazy[v] = 0;
}
if(ql <= l && r <= qr)
{
return tree[v];
}
if(l > qr || r < ql)return 0;
int mid = (l + r) / 2;
return query(v * 2, l, mid, ql, qr) +
query(v * 2 + 1, mid + 1, r, ql, qr);
}
void dfs_comp(int v, int depth, int par)
{
used[v] = 1;
int q = query(1, 1, n, depth, depth);
update(1, 1, n, depth, depth, -q);
for(int i = 0; i < gr[v].size(); i++)
{
if(!used[gr[v][i]])
{
dfs_comp(gr[v][i], depth + 1, v);
}
}
for(int i = 0; i < 25; i++)
{
update(1, 1, n, max(1, depth - (1 << i) + 1), depth, br[i][v]);
}
int num = query(1, 1, n, depth, depth);
mp[{par, v}] = mp[{v, par}] = num;
return ;
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n - 1; i++)
{
scanf("%d%d%d%d", &e[i].a, &e[i].b, &e[i].c, &e[i].d);
gr[e[i].a].push_back(e[i].b);
gr[e[i].b].push_back(e[i].a);
}
dfs(1, 0);
last[0] = INT_MAX;
for(int i = 1; i <= n - 1; i++)
{
lca(i, i + 1);
}
fill(used, used + n + 2, 0);
dfs_comp(1, 1, 0);
for(int i = 0; i < n - 1; i++)
{
int tmp = mp[{e[i].a, e[i].b}];
if(tmp * e[i].c > e[i].d)ans += e[i].d;
else ans += tmp * e[i].c;
}
printf("%lld\n", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
putovanje.cpp: In function 'void dfs(int, int)':
putovanje.cpp:33:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i = 0; i < gr[v].size(); i++)
| ~~^~~~~~~~~~~~~~
putovanje.cpp: In function 'void dfs_comp(int, int, int)':
putovanje.cpp:135:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
135 | for(int i = 0; i < gr[v].size(); i++)
| ~~^~~~~~~~~~~~~~
putovanje.cpp: In function 'int main()':
putovanje.cpp:155:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
155 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
putovanje.cpp:158:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
158 | scanf("%d%d%d%d", &e[i].a, &e[i].b, &e[i].c, &e[i].d);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |