BZOJ 2821作诗(Poetize) 分块
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
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <algorithm>
- #include <cmath>
- using namespace std;
- #define REP(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
- const int maxn = 1e5+;
- int n, c, m, a[maxn];
- int bel[maxn], l[maxn], r[maxn];
- int cnt[][maxn], s_block, t[maxn], f[][];
- int solve(int x, int y)
- {
- int L = bel[x], R = bel[y];
- if (L == R)
- {
- REP(i, x, y) t[a[i]] = ;
- int ret = ;
- REP(i, x, y) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
- return ret;
- }
- else
- {
- int ret = f[L+][R-];
- REP(i, x, r[L]) t[a[i]] = ;
- REP(i, l[R], y) t[a[i]] = ;
- REP(i, x, r[L]) if (!t[a[i]]) t[a[i]] = cnt[R-][a[i]]-cnt[L][a[i]];
- REP(i, l[R], y) if (!t[a[i]]) t[a[i]] = cnt[R-][a[i]]-cnt[L][a[i]];
- REP(i, x, r[L]) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
- REP(i, l[R], y) if (t[a[i]] ++) ret += (t[a[i]]&) ? - : ;
- return ret;
- }
- }
- int main()
- {
- scanf("%d %d %d", &n, &c, &m);
- REP(i, , n) scanf("%d", &a[i]);
- int block = int(sqrt(n));
- REP(i, , n)
- {
- bel[i] = i/block+, r[bel[i]] = i;
- if (i == || bel[i] != bel[i-]) l[bel[i]] = i;
- }
- s_block = n/block+;
- REP(i, , s_block)
- {
- REP(j, , c) t[j] = ;
- REP(j, i, s_block)
- {
- f[i][j] = (j == i) ? : f[i][j-];
- REP(k, l[j], r[j]) if (t[a[k]] ++) f[i][j] += (t[a[k]]&) ? - : ;
- }
- }
- REP(i, , s_block)
- {
- REP(j, , c) cnt[i][j] = cnt[i-][j];
- REP(j, l[i], r[i]) cnt[i][a[j]] ++;
- }
- int x, y, ans = ;
- while (m --)
- {
- scanf("%d %d", &x, &y);
- x = (x+ans)%n+, y = (y+ans)%n+;
- if (x > y) swap(x, y);
- printf("%d\n", ans = solve(x, y));
- }
- return ;
- }
BZOJ 2821作诗(Poetize) 分块的更多相关文章
- 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 ...
- [BZOJ 2821] 作诗(Poetize) 【分块】
题目链接:BZOJ - 2821 题目分析 因为强制在线了,所以无法用莫队..可以使用分块来做. 做法是,将 n 个数分成 n/x 个块,每个块大小为 x .先预处理出 f[i][j] ,表示从第 i ...
- 2821: 作诗(Poetize)
2821: 作诗(Poetize) Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 1078 Solved: 348[Submit][Status] ...
- 【BZOJ2821】作诗(Poetize) 分块
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...
- [BZOJ 2821] 作诗
Link: BZOJ 2821 传送门 Solution: 一道类似区间众数的经典分块 由于个数为偶数这样的条件不能支持快速合并 因此要先$O(n*sqrt(n))$预处理出$pre[i][j]$表示 ...
- bzoj 2821 作诗 分块
基本思路和蒲公英一样 还是预处理出每两个块间的答案 询问时暴力跑两边的贡献 #include<cstdio> #include<cstring> #include<ios ...
- BZOJ2821 作诗(Poetize) 分块
题意 算法 经验总结 代码 题意 不带修改,查询数列[1,n]中[l,r]内的出现正偶数次的数的个数, 数列中的数 <= 1e5, n <= 1e5, 强制在线 算法 查询的内容: 区 ...
- 【分块】BZOJ2821 作诗(Poetize)
2821: 作诗(Poetize) Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 3265 Solved: 951[Submit][Status][ ...
- BZOJ2821 作诗(Poetize) 【分块】
BZOJ2821 作诗(Poetize) Description 神犇SJY虐完HEOI之后给傻×LYD出了一题: SHY是T国的公主,平时的一大爱好是作诗. 由于时间紧迫,SHY作完诗之后还要虐OI ...
随机推荐
- [机器学习&数据挖掘]SVM---核函数
1.核函数概述: 核函数通俗的来说是通过一个函数将向量的低维空间映射到一个高维空间,从而将低维空间的非线性问题转换为高维空间的线性问题来求解,从而再利用之前说的一系列线性支持向量机,常用的核函数如下: ...
- 一个网页的对象抽象之路——po编程 (干货,Java自动化测试)
先来看一个在腾讯课堂首页搜索机构的操作步骤: 1:首先打开腾讯课堂的首页:https://ke.qq.com 2:点击课程或机构的下拉选择图标 3:选择机构 4:在搜索框输入要搜索的机构名称 5:点击 ...
- 针对Jigsaw勒索软件的解锁工具
针对Jigsaw勒索软件的解锁工具 据了解, 用户的计算机系统一旦感染了勒索软件Jigsaw,如果用户没有在一个小时之内支付赎金(0.4个比特币,价值约为150美金),那么恶意软件将会把系统中的上千份 ...
- Dream Spark ------spark on yarn ,yarn的配置
<?xml version="1.0"?> <!-- Licensed under the Apache License, Version 2.0 (the &q ...
- hibernate介绍及环境搭建
1.前言 hibernate与mybatis的位置一样,都是属于DAO层的框架,代替我们原来的JDBC操作数据库,属于ORM(object relationg mapping. 对象关系映射)框架.O ...
- 【干货】使用EnCase来分析windows 7文件系统------认识元数据记录$MFT,数据恢复
来源:Unit 6: Windows File Systems and Registry 6.1 Windows File Systems and Registry Windows NTFS File ...
- C#并行计算 Parallel.Foreach&Parallel.For
Parallel.For(int fromInclude, int toExclude, Action<int> body) 栗子: Parallel.For(0, 10, (i) =&g ...
- linux下usb转串口驱动分析【转】
转自:http://blog.csdn.net/txxm520/article/details/8934706 首先说一下linux的风格,个人理解 1. linux大小结构体其实是面向对象的方法,( ...
- APP的CPU,内存,耗电,流量测试工具
APP的CPU,内存,耗电,流量测试工具下载地址,后续文章会介绍如何使用Emmagee.itest.gt APP应用的CPU,内存,耗电,流量调查 可和同类产品比较,使用GT等工具:CPU靠syste ...
- windows安装 Microsoft Visual c++
第一种方法: 第二种方法: 参考链接 直接给一个2015版本的下载地址 https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/un ...