제출 #823609

#제출 시각아이디문제언어결과실행 시간메모리
823609danitroPutovanje (COCI20_putovanje)C++14
110 / 110
980 ms77940 KiB
#include <bits/stdc++.h> using namespace std; #define MAX 410000 long long n, par[25][MAX], first[MAX], last[MAX], tym, br[25][MAX], tree[MAX * 2], lazy[MAX * 2]; map<pair<int, int>, long long > mp; long long ans; vector <int> gr[MAX]; bool used[MAX]; struct edge { long long 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("%lld", &n); for(int i = 0; i < n - 1; i++) { scanf("%lld%lld%lld%lld", &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("%lld", &n);
      |  ~~~~~^~~~~~~~~~~~
putovanje.cpp:158:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  158 |   scanf("%lld%lld%lld%lld", &e[i].a, &e[i].b, &e[i].c, &e[i].d);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...