社区讨论

求问J组T3

学术版参与者 2已保存回复 2

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
2 条
当前快照
1 份
快照标识符
@mhiydi5q
此快照首次捕获于
2025/11/03 17:43
4 个月前
此快照最后确认于
2025/11/03 17:43
4 个月前
查看原帖
这个做法能过吗,复杂度正确吗
CPP
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN = 5e5 + 10;
const int MAXV = 1 << 20; // 根据数据范围调整,2^20足够大

int n, k;
ll ans;
int a[MAXN];
int q[MAXN];
vector<int> pos[MAXV]; // 存储每个前缀异或值出现的位置

inline int read() {
    int x = 0; bool f = 1;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = 0;
        c = getchar();
    } 
    while(c >= '0' && c <= '9') {
        x = (x << 3) + (x << 1) + (c ^ 48);
        c = getchar();
    }
    return f ? x : -x;
}

int main() {
    n = read(), k = read();
    pos[0].push_back(0);
    int it=0;
    for(int i = 1; i <= n; i++) {
        a[i] = read();
        q[i] = a[i] ^ q[i - 1];
        int target = q[i] ^ k;
        int idx = lower_bound(pos[target].begin(), pos[target].end(), it)-pos[target].begin();
        if(idx <= (int)pos[target].size()-1) {
           // cout<<pos[target].size()<<endl;
            ans++;
            it=i;
           // cout<<i<<endl;
        }
        pos[q[i]].push_back(i);
    }
    
    printf("%lld", ans);
    return 0;
}

回复

2 条回复,欢迎继续交流。

正在加载回复...