自学内容网 自学内容网

The 14th Jilin Provincial Collegiate Programming Contest

题目

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define fi first
#define se second
#define lson p << 1
#define rson p << 1 | 1
#define ll long long
#define pii pair<int, int>
#define ld long double
const int maxn = 1e6 + 5, inf = 1e18, maxm = 4e4 + 5, base = 37;
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
// const int mod = 998244353;
// const __int128 mod = 212370440130137957LL;
int n, m;

int t[maxn];//树状数组维护区间 所有 不同数字 的异或和
int lowbit(int x){
return x & -x;
}

void add(int x, int k){
for(int i = x; i <= n; i += lowbit(i)){
t[i] ^= k;
}
}
int ask(int x){
int res = 0;
for(int i = x; i >= 1; i -= lowbit(i)){
res ^= t[i];
}
return res;
}

//long long ? maxn ? n? m?
void solve(){
    ll res = 0;
    int k, q;
    cin >> n >> q;
vector<int> a(n + 1), lst(n + 1), ans(q + 1), pre(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
pre[i] = pre[i - 1] ^ a[i];
}
vector<vector<pii>> qry(n + 1);
for(int i = 1; i <= q; i++){
int l, r;
cin >> l >> r;
qry[r].pb({l, i});
}
map<int, int> mp;//数字最后出现的位置
for(int i = 1; i <= n; i++){
if(mp.count(a[i])){
add(mp[a[i]], a[i]);
}
add(i, a[i]);
mp[a[i]] = i;
for(auto [l, id] : qry[i]){
int r = i;
ans[id] = ask(r) ^ ask(l - 1) ^ pre[r] ^ pre[l - 1];
}
}
for(int i = 1; i <= q; i++){
cout << ans[i] << '\n';
}
}
    
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(9);
    
    int T = 1;
    // cin >> T;
    while (T--){
        solve();
    }
    return 0;
}


原文地址:https://blog.csdn.net/way_back__/article/details/142698554

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!