zoukankan      html  css  js  c++  java
  • Codeforces Round #539 (Div. 2)

    Problem   Codeforces Round #539 (Div. 2) - C. Sasha and a Bit of Relax

    Time Limit: 2000 mSec

    Problem Description

    Input

    The first line contains one integer n (2n310^5) — the size of the array.

    The second line contains n integers a1,a2,,an (0ai<2^20) — array itself.

    Output

    Print one integer — the number of funny pairs. You should consider only pairs where rl+1is even number.

    Sample Input

    5
    1 2 3 4 5

    Sample Output

    1

    题解:这个题只要知道异或满足

      if A ^ B == C then A == B ^ C 这个性质

    就会很简单,首先处理出异或前缀和是很自然的,之后对于满足条件的pair,必定有f(r) == f(l - 1),f即异或前缀和,这是必要条件,同时也是充分的,充分性同样利用这个性质

      若有a[l] ^ a[l+1] ^ ... ^ a[k] = a[k+1] ^ a[k + 2] ^ ... ^ a[r], 那么就可以利用上述性质使得k == mid,因为如果k > mid,那么,等式两边同时异或a[k]即可将a[k]从等式左边变到右边,继续进行这种操作知道k == mid,对于k < mid,同理,因此现在就是找满足异或前缀和相等的pair,并且要使得r - l + 1是偶数,也就是r 和 l - 1同奇偶,这个问题很好解决,统计每种异或前缀和的个数(对每个值按下标奇偶性分类)即可计算出最终结果,由a数组数据的范围可知异或前缀和 < 2^21,复杂度是完全可以接受的,别忘了开 long long。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 #define REP(i, n) for (int i = 1; i <= (n); i++)
     6 #define sqr(x) ((x) * (x))
     7 
     8 const int maxn = 2000000 + 100;
     9 const int maxm = 200000 + 100;
    10 const int maxs = 10000 + 10;
    11 
    12 typedef long long LL;
    13 typedef pair<int, int> pii;
    14 typedef pair<double, double> pdd;
    15 
    16 const LL unit = 1LL;
    17 const int INF = 0x3f3f3f3f;
    18 const double eps = 1e-14;
    19 const double inf = 1e15;
    20 const double pi = acos(-1.0);
    21 const int SIZE = 100 + 5;
    22 const LL MOD = 1000000007;
    23 
    24 LL cnt[maxn][2];
    25 int n;
    26 
    27 int main()
    28 {
    29     ios::sync_with_stdio(false);
    30     cin.tie(0);
    31     //freopen("input.txt", "r", stdin);
    32     //freopen("output.txt", "w", stdout);
    33     cin >> n;
    34     int pre = 0, x;
    35     for (int i = 1; i <= n; i++)
    36     {
    37         cin >> x;
    38         pre ^= x;
    39         //cout << pre << endl;
    40         cnt[pre][i % 2]++;
    41     }
    42     LL ans = 0;
    43     cnt[0][0]++;
    44     for (int i = 0; i < maxn; i++)
    45     {
    46         ans += cnt[i][0] * (cnt[i][0] - 1) / 2;
    47         ans += cnt[i][1] * (cnt[i][1] - 1) / 2;
    48     }
    49     cout << ans << endl;
    50     return 0;
    51 }
  • 相关阅读:
    [剑指Offer]判断一棵树为平衡二叉树(递归)
    [HDOJ]Coin Change(DP)
    01背包、完全背包
    [CodeForces_618C]Constellation
    [Codeforces_713A]Sonya and Queries
    C++位运算符
    [codeforces_597B] Restaurant(贪心)
    [LeetCode_98]Validate Binary Search Tree
    [LeetCode_96] Unique Binary Search Trees
    [LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal
  • 原文地址:https://www.cnblogs.com/npugen/p/10791788.html
Copyright © 2011-2022 走看看