QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#302245#7245. Frank Sinatrajasper166WA 0ms28228kbC++203.5kb2024-01-10 18:00:562024-01-10 18:00:56

Judging History

你现在查看的是最新测评结果

  • [2024-01-10 18:00:56]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:28228kb
  • [2024-01-10 18:00:56]
  • 提交

answer

#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;

#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif

const int N = 210000;
const int K = 18;
const int BLOCK = 400;

int n, q;
vector <pair <int, int>> adj[N];
int tin[N], tout[N];
pair <int, int> tr[N << 1];
int timer = 0;

int dep[N], up[K][N];
int c[N];
void dfs(int u, int x, int p) {
    tin[u] = ++timer;
    tr[timer] = {u, x};
    for (auto [v, y] : adj[u]) {
        if (v ^ p) {
            up[0][v] = u;
            dep[v] = dep[u] + 1;
            for (int i = 1; i < K; ++i)
                up[i][v] = up[i - 1][up[i - 1][v]];
            dfs(v, y, u);
            c[v] = y;
        }
    }
    tout[u] = ++timer;
    tr[timer] = {u, x};
}

int lca(int u, int v) {
    if (dep[u] != dep[v]) {
        if (dep[u] < dep[v]) swap(u, v);
        int k = dep[u] - dep[v];
        for (int i = K - 1; i >= 0; --i)
            if (k & (1 << i))
                u = up[i][u];
    }
    if (u == v) return u;
    for (int i = K - 1; i >= 0; --i) {
        if (up[i][u] != up[i][v]) {
            u = up[i][u];
            v = up[i][v];
        }
    }
    return up[0][u];
}

struct Queries {
    int l, r, id;
    bool operator < (const Queries &ot) const {
        if (l / BLOCK == ot.l / BLOCK)
            return (l / BLOCK & 1)? r < ot.r : r > ot.r;
        else 
            return (r < ot.r); 
    }
} qs[N];

bool vis[N];
int ans[N], mex = 0;
int has[N], f[N];

void add(int i) {
    int u = tr[i].first;
    int x = c[u];
    if (x > n) return;
    vis[u] ^= 1;
    if (vis[u]) {
        has[x]++;
        if (has[x] == 1) f[x / BLOCK]++;
    }
    else {
        has[x]--;
        if (has[x] == 0) f[x / BLOCK]--;
    }
}
void del(int i) {
    int u = tr[i].first;
    int x = c[u];
    vis[u] ^= 1;
    if (vis[u]) {
        has[x]++;
        if (has[x] == 1) f[x / BLOCK]++;
    }
    else {
        has[x]--;
        if (has[x] == 0) f[x / BLOCK]--;
    }
}
int get() {
    int mex = 0;
    for (int x = 0; ; x++) {
        if (f[x] != BLOCK) {
            mex = x * BLOCK;
            while (has[mex]) ++mex;
            break;
        }
    }
    return mex;
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    
    #ifdef JASPER
        freopen("in1", "r", stdin);
    #endif

    cin >> n >> q;
    for (int i = 1; i < n; ++i) {
        int u, v, x;
        cin >> u >> v >> x;
        // debug(u, v, x);
        adj[u].emplace_back(v, x);
        adj[v].emplace_back(u, x);
    }

    dfs(1, 1e9, 0);

    // debug(lca(1, 3))
    for (int i = 1; i <= q; ++i) {
        int u, v; 
        cin >> u >> v;
        // if (dep[u] > dep[v]) swap(u, v);
        // int p = lca(u, v);
        // int L, R;
        // if (p == u) {
        //     L = tin[u] + 1;
        //     R = tin[v];
        // }
        // else {
        //     L = tout[u];
        //     R = tin[v];
        // }
        // // debug(L, R, p);
        // qs[i] = {L, R, i};
        if (tin[u] > tin[v]) swap(u, v);
        qs[i] = {tin[u], tin[v], i};
    }
    sort(qs + 1, qs + 1 + q);
    int l = 1, r = 0;
    for (int i = 1; i <= q; ++i) {
        auto [L, R, id] = qs[i];

        while (l > L) add(--l);
        while (r < R) add(++r);
        while (l < L) del(l++);
        while (r > R) del(r--);

        ans[id] = get();
    }
    // MEX only in range [0, n + 1];
    for (int i = 1; i <= q; ++i)
        cout << ans[i] << "\n";
}


詳細信息

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 28228kb

input:

7 6
2 1 1
3 1 2
1 4 0
4 5 1
5 6 3
5 7 4
1 3
4 1
2 4
2 5
3 5
3 7

output:

1
1
1
2
2
2

result:

wrong answer 1st numbers differ - expected: '0', found: '1'