• [CQOI2018]异或序列


    嘟嘟嘟


    前缀和+莫队。


    先用前缀和预处理异或,于是问题变成了在([L - 1, R])中求两个数异或等于(k)的数对个数。


    然后就离线排序,按套路维护两个指针加加减减,并维护一个桶,每一次加(x),答案就加上(bac[x ^ k]),并且++(bac[x]),删除就减去贡献。

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define In inline
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e5 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, m, k, S, sum[maxn];
    struct Node
    {
      int L, R, id, b;
      In bool operator < (const Node& oth)const
      {
        return b < oth.b || (b == oth.b && R < oth.R);
      }
    }q[maxn];
    
    ll cnt = 0, bac[maxn], ans[maxn];
    In void add(int x)
    {
      cnt += bac[x ^ k], ++bac[x];
    }
    In void del(int x)
    {
      --bac[x], cnt -= bac[x ^ k];
    }
    
    int main()
    {
      n = read(), m = read(), k = read(); S = sqrt(n);
      for(int i = 1; i <= n; ++i) sum[i] = read(), sum[i] ^= sum[i - 1];
      for(int i = 1; i <= m; ++i)
        {
          int L = read() - 1, R = read();
          q[i] = (Node){L, R, i, (L - 1) / S + 1};
        }
      sort(q + 1, q + m + 1);
      for(int i = 1, l = 1, r = 0; i <= m; ++i)
        {
          while(l < q[i].L) del(sum[l++]);
          while(l > q[i].L) add(sum[--l]);
          while(r < q[i].R) add(sum[++r]);
          while(r > q[i].R) del(sum[r--]);
          ans[q[i].id] = cnt;
        }
      for(int i = 1; i <= m; ++i) write(ans[i]), enter;
      return 0;
    }
    
  • 相关阅读:
    shell读取或者修改ini文件
    Linux--查询文件的第几行到第几行命令
    python读写修改配置文件(ini)
    Django REST framework 的TokenAuth认证及外键Serializer基本实现
    vue2.0+webpack+vuerouter+vuex+axios构建项目基础
    Zabbix 监控Windows磁盘IO
    磁盘 I/O 性能监控指标和调优方法
    linux查看与修改交换内存配置(解决zabbix-agent启动报错)
    js中的Map对象的简单示例
    Idea 怎么远程debug
  • 原文地址:https://www.cnblogs.com/mrclr/p/10422883.html
Copyright © 2020-2023  润新知