原题链接
随机数另类做法
众所周知,异或可以两两相消
本题中,由于数据范围a<=1e6,可以利用随机数消除数字间异或相互影响的可能
离散每个数字,使异或时互不影响
附上哥哥代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <bits/stdc++.h>
using i64 = long long; using u64 = unsigned long long;
std::mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
constexpr int V = 1E6;
u64 val[V + 1];
void solve() { int n, q; std::cin >> n >> q; std::vector<u64> pre(n + 1); for (int i = 0; i < n; i++) { int a; std::cin >> a; pre[i + 1] = pre[i] ^ val[a]; } while (q--) { int l, r; std::cin >> l >> r; l--; if (pre[l] == pre[r]) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } } }
int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); for (int i = 1; i <= V; i++) { val[i] = rng(); } int t; std::cin >> t; while (t--) { solve(); } return 0; }
|
提取模板如下
1 2 3 4 5 6 7
| mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); const ll V = 1e6; ull val[V+1];
for(int i=1;i<=V;i++){ val[i]=rng(); }
|
by
Heshi
Copyright (c) 2019 CC-BY-NC-4.0 LICENSE