제출 #1351348

#제출 시각아이디문제언어결과실행 시간메모리
1351348top1svtinJanjetina (COCI21_janjetina)C++17
110 / 110
201 ms17476 KiB
#include <bits/stdc++.h>

using namespace std;

#define kien long long
#define int long long
#define pb push_back
#define FOR(i, a, b) for (int i = a;i <= b; i++)
#define FORD(i, a, b) for (int i = a;i >= b; i--)
#define pii pair<int, int>
#define dembit(a) __builtin_popcountll(a)
#define task "kien"
#define fin(x) freopen(x".inp","r",stdin)
#define fou(x) freopen(x".out","w",stdout)
#define debug(x) cout << x << " ";
#define down cout << "\n"
const kien MOD = 1e9 + 7;
const int NTEST = 100;
const int Million = 1e6 + 5;
const int MXN = 1e5 + 5;
mt19937 rd(chrono::high_resolution_clock::now().time_since_epoch().count());
kien rand(kien l, kien r) {
    assert(l <= r);
    return uniform_int_distribution<kien>(l, r)(rd);
}

kien n, k, m, dem, sz[MXN + 5], u, v, a[1000000];
kien maxx, minn, vtr, l, r, ans;
bool vis[MXN];
vector <pii> dp[MXN];
vector <pii> vec;

struct FenwickTree {
    int N;
    kien bit[MXN];

    void init(int n) {
        N = n;
        for (int i = 1; i <= N; i++) bit[i] = 0;
    }

    void update(int pos, int val) {
        for (; pos <= N; pos += pos & -pos) bit[pos] += val;
    }

    kien get(int pos) {
        kien s = 0;
        for (; pos > 0; pos -= pos & -pos) s += bit[pos];
        return s;
    }

} BIT;

void calcsz (int u, int pa) {
    sz[u] = 1;

    for (auto v : dp[u]) {
        if (v.first != pa and !vis[v.first]) {
            calcsz(v.first, u);
            sz[u] += sz[v.first];
        }
    }
}

int centroid (int u, int pa, int N) {
    for (auto v : dp[u]) {
        if (v.first != pa and !vis[v.first] and sz[v.first] > N / 2) {
            return centroid(v.first , u, N);
        }
    }

    return u;
}

void dfs (int u, int len ,int maxx, int pa) {
    vec.pb({maxx, len});

    for (auto v : dp[u]) {
        if (v.first == pa or vis[v.first]) continue;
        dfs(v.first, len + 1, max(maxx, v.second), u);
    }
}

void build (int u) {
    calcsz(u, 0);
    int cen = centroid(u, 0, sz[u]);
    vis[cen] = 1;

    vector<pii>().swap(vec); /// giải phóng bộ nhớ
    for (auto v : dp[cen]) {
        if (!vis[v.first]) dfs (v.first, 1, v.second, cen);
    }
    sort (vec.begin(), vec.end());

    for (auto v : vec) {
        if (v.first - v.second - k >= 0) {
            ans += BIT.get(v.first - v.second - k);
            ans++;
        }
        BIT.update(v.second, 1);
    }

    for (auto v : vec) {
        BIT.update(v.second, -1);
    }

    for (auto u : dp[cen]) {
        if (!vis[u.first]) {

            vector<pii>().swap(vec);
            dfs (u.first, 1, u.second, cen);
            sort (vec.begin(), vec.end());

            for (auto v : vec) {
                if (v.first - v.second - k >= 0) ans -= BIT.get(v.first - v.second - k);
                BIT.update(v.second, 1);
            }

            for (auto v : vec) {
                BIT.update(v.second, -1);
            }
        }
    }

    for (auto v : dp[cen]) {
        if (!vis[v.first]) build(v.first);
    }
}

void solve() {
    cin >> n >> k;
    kien w;

    FOR (i, 1, n - 1) {
        cin >> u >> v >> w;
        dp[u].pb({v, w}); dp[v].pb({u, w});
    }

    BIT.init(100000);
    build (1);
    cout << ans * 2;
}

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen(task".inp", "r")) {
        fin(task); fou(task);
    }
    int t = 1;  //cin >> t;
    while(t--) solve();

    cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC << "s ";
}

컴파일 시 표준 에러 (stderr) 메시지

Main.cpp:144:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
  144 | main() {
      | ^~~~
Main.cpp: In function 'int main()':
Main.cpp:13:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 | #define fin(x) freopen(x".inp","r",stdin)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~
Main.cpp:148:9: note: in expansion of macro 'fin'
  148 |         fin(task); fou(task);
      |         ^~~
Main.cpp:14:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   14 | #define fou(x) freopen(x".out","w",stdout)
      |                ~~~~~~~^~~~~~~~~~~~~~~~~~~~
Main.cpp:148:20: note: in expansion of macro 'fou'
  148 |         fin(task); fou(task);
      |                    ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...