http://www.codeforces.com/contest/703/problem/D D. Mishka and Interesting sum (莫队的TLE)

/*莫队算法的常数优化 实战演练 虽然是TLE代码*/

#include<bits/stdc++.h>
using namespace std;

const int maxn = 1000000 + 100;
int block;
struct query{
    int l, r, id;
    bool operator < (const query &rhs)const{
        return (l/block == rhs.l /block) ? r < rhs.r : l/block < rhs.l/block;
    }
}q[maxn];

int a[maxn];
int cnt[maxn];
int ans[maxn];
int pre[maxn];

int main(){
    int n;
    scanf("%d", &n);
    memset(cnt, 0, sizeof(cnt));
    pre[0] = 0;
    for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), pre[i] = pre[i-1]^a[i];
    int m;
    scanf("%d", &m);
    block = ceil(sqrt(m*1.0));
    for(int i = 0; i < m; ++i){
        scanf("%d%d", &q[i].l, &q[i].r);
        q[i].id = i + 1;
    }
    sort(q, q + m);
    int curl, curr;
    curl = curr = 0;
    int res = 0;
    for(int i = 0; i < m; ++i){
        if(i){
            if(q[i].r < q[i-1].r){
                while(curl <= curr) cnt[a[curl++]] = 0;
                res = 0;
                curl = q[i].l;
                curr = q[i].r;
                //++cnt[a[curl]];
                int k = curl;
                while(k <= curr){
                    ++cnt[a[k]];
                    if(cnt[a[k]] == 1) res ^= a[k];
                    ++k;
                }
                ans[q[i].id] = res^pre[q[i].r]^pre[q[i].l-1];
                continue;
            }
        }

        if(curl < q[i].l){
            while(curl < q[i].l){
                --cnt[a[curl]];
                if(cnt[a[curl]] == 0)
                    res ^= a[curl];
                ++curl;
            }
        }
        if(curl > q[i].l){
            while(curl > q[i].l){
                --curl;
                ++cnt[a[curl]];
                if(cnt[a[curl]] == 1)
                     res ^= a[curl];
            }
        }
        if(curr < q[i].r){
            while(curr < q[i].r){
                ++curr;
                ++cnt[a[curr]];
                 if(cnt[a[curr]] == 1)
                    res ^= a[curr];
            }
        }
        if(curr > q[i].r){
            while(curr > q[i].r){
                --cnt[a[curr]];
               if(cnt[a[curr]] == 0)
                    res ^= a[curr];
                --curr;
            }
        }
        ans[q[i].id] = res^pre[q[i].r]^pre[q[i].l-1];
    }
    for(int i = 1; i <= m; ++i) printf("%d\n", ans[i]);
}
时间: 2024-12-30 03:43:11

http://www.codeforces.com/contest/703/problem/D D. Mishka and Interesting sum (莫队的TLE)的相关文章

Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的性质,在一个区间中,我们如果把所有数字都异或的话,可以发现最后偶数次的数字异或后都变成了0,只剩下了奇数次的数字异或. 举个例子,{1,2,3,2,3,5} 异或和是1^2^3^2^3^5=1^5 因为最后要计算偶数次数字的异或和,那么最后我们只需要再异或上该区间内所有不同数字即可. 那么我们可以先

Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和Xor[i],表示1~i的xor和.因为num^num=0,所以Xor[r] ^ Xor[l - 1]求的是l~r之间出现奇数次的数字xor和. 那怎么求偶数次的呢,那我们可以先求l到r之间不重复出现数字的xor(比如1 1 2 求的是1 ^ 2),然后再xor以上求出的Xor[r] ^ Xor[l

http://codeforces.com/contest/575/problem/B

题目链接: http://codeforces.com/contest/575/problem/B 题解: 代码: #include<cstdio> #include<vector> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn = 1e5 + 10; const int DEG = 22; const in

http://codeforces.com/contest/741/problem/B B. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

题意: 给出上限体重W 然后还给出每个人的体重wi 和 魅力值 bi 互为伙伴的对(xi, yi) 可以凑成group 思路: 并查集找出所有的group 暴力背包 对于每一个group 要选出这一组内选一个人时的最优结果, 如果所有人的体重和小于等于W,还得考虑选所有人的情况 #include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include &l

Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 树状数组

D. Mishka and Interesting sum 链接: http://codeforces.com/problemset/problem/703/D 题意: 给一个序列 每次询问一个区间 求区间中出现次数为偶数次的数的异或和 代码: 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<map> 5 using namespace std; 6 7 str

Codeforces Round #340 (Div. 2) E. XOR and Favorite Number 【莫队算法 + 异或和前缀和的巧妙】

任意门:http://codeforces.com/problemset/problem/617/E E. XOR and Favorite Number time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Bob has a favorite number k and ai of length n. Now he asks yo

http://codeforces.com/contest/776/problem/G

题意: 讲一个16进制的数字num的每一位拆分下来 也就是sum = $\sigma(2 ^ a_i)$ 每一个a_i 都是不同的 举个栗子: $1014_16$ 就是 $2^1 + 2 ^ 0 + 2 ^ 4$ 求得的sum是个十进制的数字 然后将sum和num都化为二进制进行异或,如果异或后的值小于num那么++ans 现在题目是给出一个区间[l, r] 问这个区间内有多少个满足条件的数,也就是区间内ans的大小 思路: 贪心+数位dp, 定义三个状态 dp[mask1][mask2][le

http://codeforces.com/contest/535/problem/C

C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Karafs is some kind of vegetable in shape of an 1?×?h rectangle. Tavaspolis people love Karafs and they use Karafs in al

codeforces 703D D. Mishka and Interesting sum(树状数组)

题目链接: D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes input standard input output standard output Little Mishka enjoys programming. Since her birthday has just passed, her friends decided to present h