AtCoder-abc250_e

· · 个人记录

题意

给定两个长度为 n 的整数序列 a 和 b。

对于 i = 1, \ldots, Q,请回答以下形式的查询:

思路

因为求的是两个集合是否相等,而集合对比复杂度太高,所以使用哈希。

把 a 和 b 中每一个数字随机化,分散数字范围,在前缀异或判断。

注意序列中每一个重复的数字中的第一个设赋随机值,其他赋 0 ,因为集合中不能有数字重复。

代码

#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll read(){ ll x = 0, f = 1; char ch = getchar(); while(ch < '0' || ch > '9'){ if(ch == '-'){ f = -1; } ch = getchar(); } while(ch >= '0' && ch <= '9'){ x = x * 10 + ch - '0', ch = getchar(); } return x * f; }
void write(ll x) { if(x < 0){ putchar('-'), x = -x; } if(x > 9){ write(x / 10); } putchar(x % 10 + '0'); }
string reads(){ string s = ""; char ch = getchar(); while(ch != ' ' && ch != '\n'){ s += ch; ch = getchar(); } return s; }
void writes(string s){ for(int i = 0; i < s.size(); i ++){ putchar(s[i]); } }


const int N = 2e5 + 2, mod = 998244353, mod2 = 1e9 + 7;
int n, q;
int a[N], b[N];
int f1[N], f2[N];
map <int, int> mp, mp2;
int x, y;

int main(){

    n = read();
    for(int i = 1; i <= n; i ++){
        a[i] = read();
    }
    for(int i = 1; i <= n; i ++){
        b[i] = read();
    }
    for(int i = 1; i <= n; i ++){
        if(!mp[a[i]]) mp[a[i]] = abs(rand()) % mod, a[i] = mp[a[i]];
        else a[i] = 0;
    }
    for(int i = 1; i <= n; i ++){
        if(!mp2[b[i]]){
            if(mp[b[i]]) mp2[b[i]] = mp[b[i]], b[i] = mp2[b[i]];
            else mp2[b[i]] = abs(rand()) % mod, b[i] = mp2[b[i]];
        }
        else b[i] = 0;
    }
    for(int i = 1; i <= n; i ++){
        f1[i] = f1[i - 1] ^ a[i];
        f2[i] = f2[i - 1] ^ b[i];
    }

    q = read();
    while(q --){
        x = read(), y = read();
        if(f1[x] == f2[y]) writes("Yes\n");
        else writes("No\n");
    }

    return 0;
}