답안 #961006

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961006 2024-04-11T11:25:15 Z cpptowin Transport (COCI19_transport) C++17
130 / 130
330 ms 42836 KB
#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1 << j))
#define onbit(i, j) (i | (1 << j))
#define vi vector<int>
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
struct BIT
{
    int t[maxn];
    void up(int x, int val)
    {
        x++;
        for (; x; x -= lb(x))
            t[x] += val;
    }
    int get(int x)
    {
        int ans = 0;
        x++;
        for( ; x < maxn ; x += lb(x))
            ans += t[x];
        return ans;
    }
} t;
int n;
vii ke[maxn];
int a[maxn];
vi event;
int ans;
// centroid decomposition
bool is_remove[maxn];
int sz[maxn];
int dfs_size(int u, int parent = -1)
{
    sz[u] = 1;
    for (auto [v, w] : ke[u])
    {
        if (v == parent or is_remove[v])
            continue;
        sz[u] += dfs_size(v, u);
    }
    return sz[u];
}
int dfs_centroid(int u, int tree_size, int parent = 0)
{
    for (auto [v, w] : ke[u])
    {
        if (v == parent or is_remove[v])
            continue;
        if (2 * sz[v] > tree_size)
            return dfs_centroid(v, tree_size, u);
    }
    return u;
}
void up(int u, int par, int update, int gas, int minn)
{
    // cout << u << ' ' << gas << ' ' << minn << "\n";
    if (minn >= 0)
    {
        int lb = lower_bound(event.begin(), event.end(), gas) - event.begin() + 1;
        t.up(lb, update);
    }
    for (auto [v, w] : ke[u])
    {
        if (v == par or is_remove[v])
            continue;
        up(v, u, update, gas + a[v] - w, min(minn + a[v] - w, a[v] - w));
    }
}
void get(int u, int par, int gas, int minn)
{
    // cout << u << ' ' << gas << ' ' << minn << "\n";
    int lb = lower_bound(event.begin(), event.end(), -minn) - event.begin() + 1;
    ans += t.get(lb);
    for (auto [v, w] : ke[u])
    {
        if (v == par or is_remove[v])
            continue;
        get(v, u, gas + a[v] - w, min(minn, gas - w));
    }
}
void dfs_dist(int u, int par, int centroid,int gas, int minn)
{
    // cout << u << ' ' << gas << ' ' << minn << "xxx\n";
    if (minn >= 0)
        ans += (u != centroid),event.pb(gas);
    for (auto [v, w] : ke[u])
    {
        if (v == par or is_remove[v])
            continue;
        dfs_dist(v, u,centroid, gas - w + a[v], min(minn + a[v] - w, a[v] - w));
    }
}
void build_centroid(int u)
{
    u = dfs_centroid(u, dfs_size(u));
    is_remove[u] = 1;
    // cout << u;en;
    // do sth
    dfs_dist(u, u,u, a[u], a[u]);
    sort(event.begin(),event.end());
    event.erase(unique(event.begin(),event.end()),event.end());
    // if(u == 2)
    // for(int it : event) cout << it << "\n";
    up(u, u, 1, a[u], a[u]);
    for (auto [v, w] : ke[u])
    {
        if (is_remove[v])
            continue;
        up(v, u, -1, a[u] - w + a[v],min(a[u] - w + a[v],a[v] - w));
        get(v, u, a[v] - w, - w);
        up(v, u, 1, a[u] - w + a[v],min(a[u] - w + a[v],a[v] - w));
    }
    up(u, u, -1, a[u], a[u]);
    event.clear();
    // cout << u << ' ' << ans;en;
    for (auto [v, w] : ke[u])
    {
        if (is_remove[v])
            continue;
        build_centroid(v);
    }
}
main()
{
#define name "TASK"
    if (fopen(name ".inp", "r"))
    {
        freopen(name ".inp", "r", stdin);
        freopen(name ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    fo(i, 1, n) cin >> a[i];
    fo(i, 1, n - 1)
    {
        int u, v, w;
        cin >> u >> v >> w;
        ke[u].pb(v, w);
        ke[v].pb(u, w);
    }
    build_centroid(1);
    cout << ans;
}

Compilation message

transport.cpp:159:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  159 | main()
      | ^~~~
transport.cpp: In function 'int main()':
transport.cpp:164:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  164 |         freopen(name ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
transport.cpp:165:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  165 |         freopen(name ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 27412 KB Output is correct
2 Correct 11 ms 27484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 27740 KB Output is correct
2 Correct 12 ms 27740 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 35528 KB Output is correct
2 Correct 66 ms 34900 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 126 ms 37772 KB Output is correct
2 Correct 113 ms 38432 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 219 ms 40740 KB Output is correct
2 Correct 190 ms 42836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 99 ms 31576 KB Output is correct
2 Correct 38 ms 31068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 78 ms 33152 KB Output is correct
2 Correct 111 ms 32724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 176 ms 34544 KB Output is correct
2 Correct 128 ms 34392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 231 ms 35808 KB Output is correct
2 Correct 238 ms 35716 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 330 ms 37968 KB Output is correct
2 Correct 247 ms 36908 KB Output is correct