Description

有一个长度为n的序列,序列每个元素的范围[1,c],有m个询问x y,表示区间[x,y]中出现正偶数次的数的种类数。

Solution

大力分块解决问题。

把序列分块,f[i][j]表示第i块到第j块的答案,并记录块的前缀数的出现次数。

f[i][j]直接暴力算,块的前缀数的出现次数也可以直接算,都是nsqrt(n)。

遇到询问x y,中间答案的块可以直接统计,然后再暴力统计左右两边零碎的贡献,也是nsqrt(n)。

Code

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7.  
  8. using namespace std;
  9.  
  10. #define REP(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
  11. const int maxn = 1e5+;
  12. int n, c, m, a[maxn];
  13. int bel[maxn], l[maxn], r[maxn];
  14. int cnt[][maxn], s_block, t[maxn], f[][];
  15.  
  16. int solve(int x, int y)
  17. {
  18. int L = bel[x], R = bel[y];
  19. if (L == R)
  20. {
  21. REP(i, x, y) t[a[i]] = ;
  22. int ret = ;
  23. REP(i, x, y) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
  24. return ret;
  25. }
  26. else
  27. {
  28. int ret = f[L+][R-];
  29. REP(i, x, r[L]) t[a[i]] = ;
  30. REP(i, l[R], y) t[a[i]] = ;
  31. REP(i, x, r[L]) if (!t[a[i]]) t[a[i]] = cnt[R-][a[i]]-cnt[L][a[i]];
  32. REP(i, l[R], y) if (!t[a[i]]) t[a[i]] = cnt[R-][a[i]]-cnt[L][a[i]];
  33. REP(i, x, r[L]) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
  34. REP(i, l[R], y) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
  35. return ret;
  36. }
  37. }
  38.  
  39. int main()
  40. {
  41. scanf("%d %d %d", &n, &c, &m);
  42. REP(i, , n) scanf("%d", &a[i]);
  43. int block = int(sqrt(n));
  44. REP(i, , n)
  45. {
  46. bel[i] = i/block+, r[bel[i]] = i;
  47. if (i == || bel[i] != bel[i-]) l[bel[i]] = i;
  48. }
  49. s_block = n/block+;
  50. REP(i, , s_block)
  51. {
  52. REP(j, , c) t[j] = ;
  53. REP(j, i, s_block)
  54. {
  55. f[i][j] = (j == i) ? : f[i][j-];
  56. REP(k, l[j], r[j]) if (t[a[k]] ++) f[i][j] += (t[a[k]]&) ? - : ;
  57. }
  58. }
  59. REP(i, , s_block)
  60. {
  61. REP(j, , c) cnt[i][j] = cnt[i-][j];
  62. REP(j, l[i], r[i]) cnt[i][a[j]] ++;
  63. }
  64. int x, y, ans = ;
  65. while (m --)
  66. {
  67. scanf("%d %d", &x, &y);
  68. x = (x+ans)%n+, y = (y+ans)%n+;
  69. if (x > y) swap(x, y);
  70. printf("%d\n", ans = solve(x, y));
  71. }
  72. return ;
  73. }

BZOJ 2821作诗(Poetize) 分块的更多相关文章

  1. BZOJ 2821: 作诗(Poetize)( 分块 )

    分块,分成N^0.5块.O(N^1.5)预处理出sm[i][j]表示前i块中j的出现次数, ans[i][j]表示第i~j块的答案. 然后就可以O(N^0.5)回答询问了.总复杂度O((N+Q)N^0 ...

  2. [BZOJ 2821] 作诗(Poetize) 【分块】

    题目链接:BZOJ - 2821 题目分析 因为强制在线了,所以无法用莫队..可以使用分块来做. 做法是,将 n 个数分成 n/x 个块,每个块大小为 x .先预处理出 f[i][j] ,表示从第 i ...

  3. 2821: 作诗(Poetize)

    2821: 作诗(Poetize) Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 1078  Solved: 348[Submit][Status] ...

  4. 【BZOJ2821】作诗(Poetize) 分块

    Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...

  5. [BZOJ 2821] 作诗

    Link: BZOJ 2821 传送门 Solution: 一道类似区间众数的经典分块 由于个数为偶数这样的条件不能支持快速合并 因此要先$O(n*sqrt(n))$预处理出$pre[i][j]$表示 ...

  6. bzoj 2821 作诗 分块

    基本思路和蒲公英一样 还是预处理出每两个块间的答案 询问时暴力跑两边的贡献 #include<cstdio> #include<cstring> #include<ios ...

  7. BZOJ2821 作诗(Poetize) 分块

    题意 算法 经验总结 代码 题意 不带修改,查询数列[1,n]中[l,r]内的出现正偶数次的数的个数, 数列中的数 <= 1e5, n <= 1e5, 强制在线 算法 ​ 查询的内容: 区 ...

  8. 【分块】BZOJ2821 作诗(Poetize)

    2821: 作诗(Poetize) Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 3265  Solved: 951[Submit][Status][ ...

  9. BZOJ2821 作诗(Poetize) 【分块】

    BZOJ2821 作诗(Poetize) Description 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好是作诗. 由于时间紧迫,SHY作完诗之后还要虐OI ...

随机推荐

  1. [机器学习&数据挖掘]SVM---核函数

    1.核函数概述: 核函数通俗的来说是通过一个函数将向量的低维空间映射到一个高维空间,从而将低维空间的非线性问题转换为高维空间的线性问题来求解,从而再利用之前说的一系列线性支持向量机,常用的核函数如下: ...

  2. 一个网页的对象抽象之路——po编程 (干货,Java自动化测试)

    先来看一个在腾讯课堂首页搜索机构的操作步骤: 1:首先打开腾讯课堂的首页:https://ke.qq.com 2:点击课程或机构的下拉选择图标 3:选择机构 4:在搜索框输入要搜索的机构名称 5:点击 ...

  3. 针对Jigsaw勒索软件的解锁工具

    针对Jigsaw勒索软件的解锁工具 据了解, 用户的计算机系统一旦感染了勒索软件Jigsaw,如果用户没有在一个小时之内支付赎金(0.4个比特币,价值约为150美金),那么恶意软件将会把系统中的上千份 ...

  4. Dream Spark ------spark on yarn ,yarn的配置

    <?xml version="1.0"?> <!-- Licensed under the Apache License, Version 2.0 (the &q ...

  5. hibernate介绍及环境搭建

    1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...

  6. 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复

    来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...

  7. C#并行计算 Parallel.Foreach&Parallel.For

    Parallel.For(int fromInclude, int toExclude, Action<int> body) 栗子: Parallel.For(0, 10, (i) =&g ...

  8. linux下usb转串口驱动分析【转】

    转自:http://blog.csdn.net/txxm520/article/details/8934706 首先说一下linux的风格,个人理解 1. linux大小结构体其实是面向对象的方法,( ...

  9. APP的CPU,内存,耗电,流量测试工具

    APP的CPU,内存,耗电,流量测试工具下载地址,后续文章会介绍如何使用Emmagee.itest.gt APP应用的CPU,内存,耗电,流量调查 可和同类产品比较,使用GT等工具:CPU靠syste ...

  10. windows安装 Microsoft Visual c++

    第一种方法: 第二种方法: 参考链接 直接给一个2015版本的下载地址 https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/un ...