跳到正文

个人记录

AtCoder-abc250_e

Chthohepta约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年7月26日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 4 分钟

版本

  1. v1当前

题意

给定两个长度为 nn 的整数序列 aa 和 bb。

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

  • 如果 AA 的前 xix_i 项 (a1,…,axi)(a_1, \ldots, a_{x_i}) 所包含的值的集合与 BB 的前 yiy_i 项 (b1,…,byi)(b_1, \ldots, b_{y_i}) 所包含的值的集合相等,则输出 Yes,否则输出 No。

思路

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

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

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

代码

C++
#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;
}

原文