[BZOJ2821]作诗(Poetize)
[BZOJ2821]作诗(Poetize)
试题描述
输入
输出
输入示例
5 3 5 1 2 2 3 1 0 4 1 2 2 2 2 3 3 5
输出示例
2 0 0 0 1
数据规模及约定
对于100%的数据,1<=n,c,m<=10^5
题解
分块,统计出 f[i][j] 表示第 i 个块到第 j 个块中出现正偶次的数有多少个。对于一个询问 [ql, qr],我们搞出那一段连续整块的答案,然后对于首尾零散的部分我们依次把所有出现的颜色统计一下它在整块那一段连续整块中出现次数的奇偶性和它在 [ql, qr] 中出现次数的奇偶性加加减减调整答案就好了。
这题做法其实和区间众数很类似。
这题块的大小要开小点(sqrt(n / log2(n) )),否则会 T 得很惨。。。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {if(Head == Tail) {int l = fread(buffer, 1, BufferSize, stdin);Tail = (Head = buffer) + l;}return *Head++;
}
int read() {int x = 0, f = 1; char c = Getchar();while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }return x * f;
}#define maxn 100010
#define maxbl 2010int n, A[maxn], tot[maxn];int cb, st[maxbl], en[maxbl], even[maxbl][maxbl], blid[maxn];
vector <int> pos[maxn];int calc(int l, int r, int col) {if(l > r) return 0;return upper_bound(pos[col].begin(), pos[col].end(), r) - lower_bound(pos[col].begin(), pos[col].end(), l);
}
int appcol[maxn], capp, clo[maxn];
int query(int ql, int qr, int tag) {if(blid[ql] == blid[qr]) {int tmp = 0;for(int i = ql; i <= qr; i++) {if(clo[A[i]] != tag) clo[A[i]] = tag, tot[A[i]] = 0;tot[A[i]]++;if((tot[A[i]] & 1) && tot[A[i]] > 1) tmp--;if(!(tot[A[i]] & 1)) tmp++;}return tmp;}int bl = blid[ql], br = blid[qr], tmp = even[bl+1][br-1];capp = 0;for(int i = ql; i <= en[bl]; i++) {if(clo[A[i]] != tag) clo[A[i]] = tag, tot[A[i]] = 0;if(!tot[A[i]]) appcol[++capp] = A[i];tot[A[i]] = 1;}for(int i = st[br]; i <= qr; i++) {if(clo[A[i]] != tag) clo[A[i]] = tag, tot[A[i]] = 0;if(!tot[A[i]]) appcol[++capp] = A[i];tot[A[i]] = 1;}for(int i = 1; i <= capp; i++) {int appt = calc(ql, qr, appcol[i]), appt_bl = calc(st[bl+1], en[br-1], appcol[i]);if(!(appt & 1) && ((appt_bl & 1) || !appt_bl)) tmp++;if((appt & 1) && (!(appt_bl & 1) && appt_bl)) tmp--;}return tmp;
}int main() {n = read(); int C = read(), q = read();for(int i = 1; i <= n; i++) A[i] = read();int m = 50;for(int i = 1; i <= n; i++) {int bl = (i - 1) / m + 1; cb = max(cb, bl);blid[i] = bl;if(!st[bl]) st[bl] = i; en[bl] = i;pos[A[i]].push_back(i);}for(int i = 1; i <= cb; i++) {memset(tot, 0, sizeof(tot));int tmp = 0;for(int j = st[i]; j <= n; j++) {tot[A[j]]++;if((tot[A[j]] & 1) && tot[A[j]] > 1) tmp--;if(!(tot[A[j]] & 1)) tmp++;if(j == n || blid[j] != blid[j+1]) even[i][blid[j]] = tmp;}}int lstans = 0;while(q--) {int l = (read() + lstans) % n + 1, r = (read() + lstans) % n + 1;if(l > r) swap(l, r);printf("%d\n", lstans = query(l, r, q + 1));}return 0;
}
转载于:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6789208.html
[BZOJ2821]作诗(Poetize)相关推荐
- BZOJ2821: 作诗(Poetize)
2821: 作诗(Poetize) Time Limit: 50 Sec Memory Limit: 128 MB Submit: 3300 Solved: 965 [Submit][Status ...
- bzoj2821 作诗(Poetize)分块+二分
预处理的d[i][j]为第i块到第j点的答案 块外的暴力 #include<iostream> #include<cstdio> #include<cstring> ...
- 2821: 作诗(Poetize)
2821: 作诗(Poetize) Time Limit: 50 Sec Memory Limit: 128 MB Submit: 2835 Solved: 814 [Submit][Status ...
- BZOJ 2821: 作诗(Poetize) [分块]
题意: N个数,Q组询问,每次问[l,r]中有多少个数出现正偶数次. 和上题一样呀呀呀 稍微改一改交上就行了 写错好多地方...要认真啊... #include <iostream> #i ...
- 腾讯国风AI虚拟人学会作诗书法,背靠开源模型SongNet
5月21日,腾讯AI虚拟人艾灵再秀出新技能,首次展示AI作诗.AI书法等国风才艺,并与青年歌手白举纲跨次元合作,共同演唱国风新歌<百川千仞>. AI"艾灵"诞生于腾讯A ...
- 罗念梓(帮别人名字作诗)
罗念梓(帮别人名字作诗) --代腾飞 2008年8月9日 于成都 罗郎羽扇伴纶巾 念诵诗文唱古今 梓桑莫问今何在 名满天下传乡音
- 月英爱耀承(帮别人名字作诗)
月英爱耀承(帮别人名字作诗) --代腾飞 2008年4月14日 于成都 月色皎皎爱如霜 英光脉脉两凝望 耀燿一生相扶过 承旨幸福万年长
- 杨英(帮别人名字作诗)
杨英(帮别人名字作诗) --代腾飞 2008年6月18日 于成都 杨门女将胜须眉 英姿飒爽捍国威 好评如流传千古 名留青史镌丰碑
- 吴敏霞(为奥运冠军名字作诗)
吴敏霞(为奥运冠军名字作诗) --代腾飞 2008年8月18日 于成都 吴女聪颖无不能 敏捷身姿为水生 霞姿轻逸落水去 问鼎皇冠功自成
最新文章
- linux mysql 编码修改,Linux下修改MySQL编码的方法
- linux php curl 安装包下载,linux中php如何安装CURL扩展方法
- java数组转换成字符串,附小技巧
- 网工协议基础(2) TCP/IP四层模型
- 【每日一包0015】gradient-string
- Github链接地址
- matlab中的神经网络训练,MATLAB中的神经网络训练
- mastercam加工报表生成_听说最厉害的工程师才敢这样玩五轴加工?
- 一个域名值百万, 现在不注册,未来价更高
- 微信公众号开发之创建菜单栏代码示例(php)
- Swift语言中的#ifdef替换
- 全排列——深度优先搜索
- 一句话总结行测资料分析八大速算技巧
- 微信小程序引入组件以及catchtouchmove实现拖动效果
- leaflet地图生成图片下载
- 【电影】小萝莉的猴神大叔
- 软件测试人员分工【管理层必看】
- C语言中 *x++ (*x)++ ++*x *++x的区别
- 单元测试整理(一)——单元测试是什么,有什么好处
- Apple下载Xcode安装包