zoukankan      html  css  js  c++  java
  • Codeforces Round #340 (Div. 2) E. XOR and Favorite Number (莫队)

    题面

    Bob has a favorite number k and ai of length n. Now he asks you to answer m queries. Each query is given by a pair li and ri and asks you to count the number of pairs of integers i and j, such that l ≤ i ≤ j ≤ r and the xor of the numbers ai, ai + 1, ..., aj is equal to k.

    Input
    The first line of the input contains integers n, m and k (1 ≤ n, m ≤ 100 000, 0 ≤ k ≤ 1 000 000) — the length of the array, the number of queries and Bob's favorite number respectively.

    The second line contains n integers ai (0 ≤ ai ≤ 1 000 000) — Bob's array.

    Then m lines follow. The i-th line contains integers li and ri (1 ≤ li ≤ ri ≤ n) — the parameters of the i-th query.

    Output
    Print m lines, answer the queries in the order they appear in the input.

    Examples
    inputCopy
    6 2 3
    1 2 1 1 0 3
    1 6
    3 5
    outputCopy
    7
    0
    inputCopy
    5 3 1
    1 1 1 1 1
    1 5
    2 4
    1 3
    outputCopy
    9
    4
    4

    思路

    没有修改只有询问,而且数据量是1e5,那么我们就可以考虑一下莫队了,对原数组做前缀和,然后离线询问,维护区间就行。

    代码实现

    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<set>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define rep(i,f_start,f_end) for (int i=f_start;i<=f_end;++i)
    #define per(i,n,a) for (int i=n;i>=a;i--)
    #define MT(x,i) memset(x,i,sizeof(x) )
    #define rev(i,start,end) for (int i=start;i<end;i++)
    #define inf 0x3f3f3f3f
    #define mp(x,y) make_pair(x,y)
    #define lowbit(x) (x&-x)
    #define MOD 1000000007
    #define exp 1e-8
    #define N 1000005 
    #define fi first 
    #define se second
    #define pb push_back
    typedef long long ll;
    const ll INF=0x3f3f3f3f3f3f3f3f;
    typedef vector <int> VI;
    typedef pair<int ,int> PII;
    typedef pair<int ,PII> PIII;
    ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
    ll lcm(ll a,ll b){return a/gcd(a,b)*b;};
    void check_max (int &a,int b) { a=max (a,b);}
    void check_min (int &a,int b) { a=min (a,b);}
    inline int read() {
        char ch=getchar(); int x=0, f=1;
        while(ch<'0'||ch>'9') {
            if(ch=='-') f=-1;
            ch=getchar();
        } while('0'<=ch&&ch<='9') {
            x=x*10+ch-'0';
            ch=getchar();
        } return x*f;
    }
    
    const int maxn=1<<20;
    struct node {int l,r,id;}q[maxn];
    ll flag[maxn];
    int lim;
    int a[maxn],i,l,r,k,n,m;
    ll ans[maxn];
    int pos[maxn];
    int L=1,R=0;
    ll now=0;
    bool cmp (node &a,node &b) {return pos[a.l]==pos[b.l]?a.r<b.r:pos[a.l]<pos[b.l]; }
    
    
    void add (int x) {
        now+=flag[a[x]^k];
        flag[a[x]]++;
    }
    
    void del (int x) {
        flag[a[x]]--;
        now-=flag[a[x]^k];
    }
    
    void solve () {
        scanf ("%d%d%d",&n,&m,&k);
        while (lim*lim<n) lim++;
        rep (i,1,n) pos[i]=(i-1)/lim+1;
        rep (i,1,n) {
            scanf ("%d",&a[i]);
            a[i]^=a[i-1];
        }
        rep (i,1,m) {
            scanf ("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort (q+1,q+1+m,cmp);
        flag[0]=1;
        for (i=l=1,r=0;i<=m;i++) {
            int L=q[i].l,R=q[i].r;
            if (r<R) {for (r++;r<=R;r++) add (r); r--;}
            if (r>R) {for (;r>R;r--) del (r);}
            if (l<L) for (;l<L;l++) del (l-1);
            if (l>L) {for (l--;l>=L;l--) add (l-1);l++;}
            ans[q[i].id]=now; 
        }
        rep (i,1,m) printf ("%lld
    ",ans[i]);
    }
    
    int main () {
        int t=1;
        while (t--) {
            solve ();
        }
    
        return 0;
    }
    
  • 相关阅读:
    Python List+Tuple+Dict+Set小结
    小命要紧~风热风寒感冒
    IDEA忽略编译报错
    IDEA新建一个Spring Boot项目
    ip2region.jar实现ip转地址
    IDEA控制台中文乱码问题
    IDEA报错Plugin "XXX" was not loaded: required plugin "Java EE: EJB, JPA, Servlets" is disabled.
    IDEA Error:java: Compilation failed: internal java compiler error
    Failed to start component [StandardEngine[Catalina].StandardHost[localhost]
    浏览网页隐藏服务器IP
  • 原文地址:https://www.cnblogs.com/hhlya/p/13838916.html
Copyright © 2011-2022 走看看