QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#871014#8618. Have You Seen This Subarray?ucup-team6275#WA 0ms3584kbC++203.4kb2025-01-25 19:07:562025-01-25 19:07:56

Judging History

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

  • [2025-01-25 19:07:56]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3584kb
  • [2025-01-25 19:07:56]
  • 提交

answer

#pragma GCC optimize("Ofast")

#include <iostream>
#include <sys/wait.h>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <deque>
#include <set>
#include <cassert>
#include <unordered_map>

#pragma GCC target("avx2,fma")

using namespace std;
const int MAXN = 1e5;

int a[MAXN];

void add_to_seg(int l, int r, int x) {
    for (int i = l; i <= r; ++i) {
        a[i] += x;
    }
}

int get_xor(int l, int r) {
    int ans = 0;
    for (int i = l; i <= r; ++i) {
        ans ^= a[i];
    }
    return ans;
}

vector <vector <int>> g;
vector <int> sz, tin, up_v, gl, pr;

void calc_sz(int v, int par) {
    sz[v] = 1;
    int opt = -1;
    for (int i = 0; i < g[v].size(); ++i) {
        int u = g[v][i];
        if (u == par) continue;
        calc_sz(u, v);
        sz[v] += sz[u];
        if (opt == -1 || sz[g[v][opt]] < sz[u]) opt = i;
    }
    if (opt != -1) swap(g[v][0], g[v][opt]);
}

int cur_time = 0;
void calc_tin(int v, int par, int up) {
    tin[v] = cur_time++;
    if (par != -1) gl[tin[v]] = gl[tin[par]] + 1;
    pr[tin[v]] = tin[par];
    up_v[tin[v]] = up;
    bool is_f = true;
    for (int i : g[v]) {
        if (i == par) continue;
        if (is_f) {
            calc_tin(i, v, up);
            is_f = false;
        } else {
            calc_tin(i, v, cur_time);
        }
    }
}

void add_path(int x, int y, int val) {
    while (true) {
        if (up_v[x] == up_v[y]) {
            add_to_seg(min(x, y), max(x, y), val);
            break;
        }
        if (gl[up_v[x]] > gl[up_v[y]]) {
            add_to_seg(up_v[x], x, val);
            x = pr[up_v[x]];
        } else {
            add_to_seg(up_v[y], y, val);
            y = pr[up_v[y]];
        }
    }
}

int path_xor(int x, int y) {
    int ans = 0;
    while (true) {
        if (up_v[x] == up_v[y]) {
            ans ^= get_xor(min(x, y), max(x, y));
            break;
        }
        if (gl[up_v[x]] > gl[up_v[y]]) {
            ans ^= get_xor(up_v[x], x);
            x = pr[up_v[x]];
        } else {
            ans ^= get_xor(up_v[y], y);
            y = pr[up_v[y]];
        }
    }
    return ans;
}

void solve() {
    int n, q;
    cin >> n >> q;
    g.assign(n, {});
    for (int i = 0; i < n - 1; ++i) {
        int x, y;
        cin >> x >> y; --x; --y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    sz.assign(n, 0);
    calc_sz(0, -1);
    tin.assign(n, 0);
    gl.assign(n, 0);
    pr.assign(n, 0);
    up_v.assign(n, 0);
    calc_tin(0, -1, 0);
    for (int i = 0; i < n; ++i) {
        a[tin[i]] = i + 1;
    }

    while (q--) {
        char oper;
        cin >> oper;
        if (oper == '+') {
            int u, v, x;
            cin >> u >> v >> x;
            u = tin[u - 1];
            v = tin[v - 1];
            add_path(u, v, x);
        } else {
            int u, v;
            cin >> u >> v;
            u = tin[u - 1];
            v = tin[v - 1];
            cout << path_xor(u, v) << "\n";
        }
    }
}

signed main() {
    if (1) {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
    }
    int t = 1;
//    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

/*
5 6
1 2
1 3
3 4
3 5
? 2 5
+ 1 4 1
? 2 5
+ 4 5 2
? 4 5
? 1 1
 */

详细

Test #1:

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

input:

6 3 5
1 5
3 4
1 6
2 4 1
3 3 1 5
3 3 4 5
4 5 2 4 3
2 6 2

output:

0
3
7

result:

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