2821: 作诗(Poetize)
2821: 作诗(Poetize)
Time Limit: 50 Sec Memory Limit: 128 MB
Submit: 1078 Solved: 348
[Submit][Status]
Description
神犇SJY虐完HEOI之后给傻×LYD出了一题:
SHY是T国的公主,平时的一大爱好是作诗。
由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅读其中连续的一段[l,r],从这一段中选出一些汉字构成诗。因为SHY喜欢对偶,所以SHY规定最后选出的每个汉字都必须在[l,r]里出现了正偶数次。而且SHY认为选出的汉字的种类数(两个一样的汉字称为同一种)越多越好(为了拿到更多的素材!)。于是SHY请LYD安排选法。
LYD这种傻×当然不会了,于是向你请教……
问题简述:N个数,M组询问,每次问[l,r]中有多少个数出现正偶数次。
Input
输入第一行三个整数n、c以及m。表示文章字数、汉字的种类数、要选择M次。
第二行有n个整数,每个数Ai在[1, c]间,代表一个编码为Ai的汉字。
接下来m行每行两个整数l和r,设上一个询问的答案为ans(第一个询问时ans=0),令L=(l+ans)mod n+1, R=(r+ans)mod n+1,若L>R,交换L和R,则本次询问为[L,R]。
Output
输出共m行,每行一个整数,第i个数表示SHY第i次能选出的汉字的最多种类数。
Sample Input
1 2 2 3 1
0 4
1 2
2 2
2 3
3 5
Sample Output
0
0
0
1
HINT
对于100%的数据,1<=n,c,m<=10^5
Source
挖个坑,以后来填。。。
本来用这题来练分块的,结果完全被二分坑到了QAQ。。。TLE...哪天有空A了这题
我的代码:
1.二分自己yy的
{$inline on}
const maxn=+;
type node=record
p,v:longint;
end;
var b:array[..maxn] of node;
a,belong,v,head,tail:array[..maxn] of longint;
mark:array[..maxn] of boolean;
f:array[..,..] of longint;
l,r:array[..] of longint;
i,j,n,m,tot,ans,ll,rr,block,cnt,x:longint;
function max(x,y:longint):longint;inline;
begin
if x>y then exit(x) else exit(y);
end;
procedure swap(var x,y:longint);inline;
var t:longint;
begin
t:=x;x:=y;y:=t;
end; function cmp(x,y:node):boolean;inline;
begin
if x.v=y.v then exit(x.p<y.p) else exit(x.v<y.v);
end;
procedure sort(l,r:longint);inline;
var i,j:longint;x,y:node;
begin
i:=l;j:=r;x:=b[(i+j)>>];
repeat
while cmp(b[i],x) do inc(i);
while cmp(x,b[j]) do dec(j);
if i<=j then
begin
y:=b[i];b[i]:=b[j];b[j]:=y;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
procedure init;
begin
readln(n,m,m);
for i:= to n do
begin
read(a[i]);
b[i].p:=i;b[i].v:=a[i];
end;
readln;
sort(,n);
block:=trunc(sqrt(n)*ln(n)/ln());
cnt:=(n-) div block+;
for i:= to n do belong[i]:=(i-) div block+;
for i:= to cnt do
begin
l[i]:=(i-)*block+;r[i]:=i*block;
end;
r[cnt]:=n;
for i:= to n do
begin
x:=b[i].v;
if head[x]= then head[x]:=i;
tail[x]:=i;
end;
end;
procedure prepare;
begin
for i:= to cnt do
begin
fillchar(v,sizeof(v),);
tot:=;
for j:=l[i] to r[cnt] do
begin
inc(v[a[j]]);
if (v[a[j]] and =) and (v[a[j]]<>) then dec(tot)
else if v[a[j]] and = then inc(tot);
f[i,belong[j]]:=tot;
end;
end;
end;
function findup(z,x:longint):longint;inline;
var l,r,mid:longint;
begin
if x>=b[tail[z]].p then exit(tail[z]);
l:=head[z];r:=tail[z];
while l<r do
begin
mid:=(l+r)>>;
if b[mid].p<x then l:=mid+
else if b[mid].p>x then r:=mid
else exit(mid);
end;
exit(l-);
end;
function finddown(z,x:longint):longint;inline;
var l,r,mid:longint;
begin
l:=head[z];r:=tail[z];
while l<r do
begin
mid:=(l+r)>>;
if b[mid].p<x then l:=mid+
else if b[mid].p>x then r:=mid
else exit(mid);
end;
exit(l);
end; function find(z,x,y:longint):longint;inline;
begin
find:=max(findup(z,y)-finddown(z,x)+,);
end;
procedure query(x,y:longint);inline;
var i,bx,by,t1,t2,z:longint;
begin
ans:=;
bx:=belong[x];by:=belong[y];
if by-bx<= then
begin
fillchar(v,sizeof(v),);
for i:=x to y do
begin
inc(v[a[i]]);
if (v[a[i]] and =) and (v[a[i]]<>) then dec(ans)
else if v[a[i]] and = then inc(ans);
end;
end
else
begin
fillchar(mark,sizeof(mark),false);
ans:=f[bx+,by-];
for i:=x to r[bx] do
begin
z:=a[i];if mark[z] then continue;mark[z]:=true;
t1:=find(z,x,y);t2:=find(z,l[bx+],r[by-]);
if (t1 and =) and (t2 and =) and (t2<>) then dec(ans)
else if (t1 and =) and ((t2 and =) or (t2=)) then inc(ans);
end;
for i:=l[by] to y do
begin
z:=a[i];if mark[z] then continue;mark[z]:=true;
t1:=find(z,x,y);t2:=find(z,l[bx+],r[by-]);
if (t1 and =) and (t2 and =) and (t2<>) then dec(ans)
else if (t1 and =) and ((t2 and =) or (t2=)) then inc(ans);
end;
end;
end;
procedure main;
begin
ans:=;
for i:= to m do
begin
readln(ll,rr);
ll:=(ll+ans) mod n+;rr:=(rr+ans) mod n+;
if ll>rr then swap(ll,rr);
query(ll,rr);
writeln(ans);
end;
end; begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
prepare;
main;
close(input);close(output);
end.
2.二分摘抄自hzwer
{$inline on}
const maxn=+;
type node=record
p,v:longint;
end;
var b:array[..maxn] of node;
a,belong,v,head,tail:array[..maxn] of longint;
mark:array[..maxn] of boolean;
f:array[..,..] of longint;
l,r:array[..] of longint;
i,j,n,m,tot,ans,ll,rr,block,cnt,x:longint;
function max(x,y:longint):longint;inline;
begin
if x>y then exit(x) else exit(y);
end;
procedure swap(var x,y:longint);inline;
var t:longint;
begin
t:=x;x:=y;y:=t;
end; function cmp(x,y:node):boolean;inline;
begin
if x.v=y.v then exit(x.p<y.p) else exit(x.v<y.v);
end;
procedure sort(l,r:longint);inline;
var i,j:longint;x,y:node;
begin
i:=l;j:=r;x:=b[(i+j)>>];
repeat
while cmp(b[i],x) do inc(i);
while cmp(x,b[j]) do dec(j);
if i<=j then
begin
y:=b[i];b[i]:=b[j];b[j]:=y;
inc(i);dec(j);
end;
until i>j;
if i<r then sort(i,r);
if j>l then sort(l,j);
end;
procedure init;
begin
readln(n,m,m);
for i:= to n do
begin
read(a[i]);
b[i].p:=i;b[i].v:=a[i];
end;
readln;
sort(,n);
block:=trunc(sqrt(n)*ln(n)/ln());
cnt:=(n-) div block+;
for i:= to n do belong[i]:=(i-) div block+;
for i:= to cnt do
begin
l[i]:=(i-)*block+;r[i]:=i*block;
end;
r[cnt]:=n;
for i:= to n do
begin
x:=b[i].v;
if head[x]= then head[x]:=i;
tail[x]:=i;
end;
end;
procedure prepare;
begin
for i:= to cnt do
begin
fillchar(v,sizeof(v),);
tot:=;
for j:=l[i] to r[cnt] do
begin
inc(v[a[j]]);
if (v[a[j]] and =) and (v[a[j]]<>) then dec(tot)
else if v[a[j]] and = then inc(tot);
f[i,belong[j]]:=tot;
end;
end;
end;
function findup(z,x:longint):longint;inline;
var l,r,mid,tmp:longint;
begin
l:=head[z];r:=tail[z];tmp:=;
while l<=r do
begin
mid:=(l+r)>>;
if b[mid].p>x then l:=mid+
else begin tmp:=mid;r:=mid-;end;
end;
exit(tmp);
end;
function finddown(z,x:longint):longint;inline;
var l,r,mid,tmp:longint;
begin
l:=head[z];r:=tail[z];tmp:=maxlongint;
while l<=r do
begin
mid:=(l+r)>>;
if b[mid].p<x then l:=mid+
else begin tmp:=mid;r:=mid-;end;
end;
exit(tmp);
end; function find(z,x,y:longint):longint;inline;
begin
find:=max(findup(z,y)-finddown(z,x)+,);
end;
procedure query(x,y:longint);inline;
var i,bx,by,t1,t2,z:longint;
begin
ans:=;
bx:=belong[x];by:=belong[y];
if by-bx<= then
begin
fillchar(v,sizeof(v),);
for i:=x to y do
begin
inc(v[a[i]]);
if (v[a[i]] and =) and (v[a[i]]<>) then dec(ans)
else if v[a[i]] and = then inc(ans);
end;
end
else
begin
fillchar(mark,sizeof(mark),false);
ans:=f[bx+,by-];
for i:=x to r[bx] do
begin
z:=a[i];if mark[z] then continue;mark[z]:=true;
t1:=find(z,x,y);t2:=find(z,l[bx+],r[by-]);
if (t1 and =) and (t2 and =) and (t2<>) then dec(ans)
else if (t1 and =) and ((t2 and =) or (t2=)) then inc(ans);
end;
for i:=l[by] to y do
begin
z:=a[i];if mark[z] then continue;mark[z]:=true;
t1:=find(z,x,y);t2:=find(z,l[bx+],r[by-]);
if (t1 and =) and (t2 and =) and (t2<>) then dec(ans)
else if (t1 and =) and ((t2 and =) or (t2=)) then inc(ans);
end;
end;
end;
procedure main;
begin
ans:=;
for i:= to m do
begin
readln(ll,rr);
ll:=(ll+ans) mod n+;rr:=(rr+ans) mod n+;
if ll>rr then swap(ll,rr);
query(ll,rr);
writeln(ans);
end;
end; begin
assign(input,'input.txt');assign(output,'output.txt');
reset(input);rewrite(output);
init;
prepare;
main;
close(input);close(output);
end.
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 ...
- BZOJ 2821作诗(Poetize) 分块
Description 有一个长度为n的序列,序列每个元素的范围[1,c],有m个询问x y,表示区间[x,y]中出现正偶数次的数的种类数. Solution 大力分块解决问题. 把序列分块,f[i] ...
- 【分块】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 ...
- BZOJ_2821_作诗(Poetize)_分块
BZOJ_2821_作诗(Poetize)_分块 Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要 ...
- 【BZOJ2821】作诗(Poetize) 分块
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...
- 巴蜀4384 -- 【模拟试题】作诗(Poetize)
Description 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次, ...
- 【bzoj2821】作诗(Poetize)
*题目描述: 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗 之后还要虐OI,于是SHY找来一篇长度为N的文章,阅读M次,每次只阅 ...
随机推荐
- 网站部署后,ie不能显示本地的图片
html:<div id="imgPreview" style='width:144px; height:80px;'> ...
- asp.net:验证控件中ValidationExpression的写法
手机号:"\d{11}"传真号:"\d{7,12}" EMAIL: ".{2,15}@.{2,15}\..{2,4}" 邮箱正则表达式:/^ ...
- Codevs 1904 最小路径覆盖问题
1904 最小路径覆盖问题 时间限制: 2 s 空间限制: 256000 KB 题目等级 : 大师 Master 传送门 题目描述 Description 给定有向图G=(V,E).设P 是G 的一个 ...
- mysql学习笔记2
drop database 数据库名称;————删除数据库 show columns from 数据表名[from 数据库名]:(或者 show columns from 数据库.数据表名:)———— ...
- MongoDB源码分析——mongod程序源码入口分析
Edit 说明:第一次写笔记,之前都是看别人写的,觉得很简单,开始写了之后才发现真的很难,不知道该怎么分析,这篇文章也参考了很多前辈对MongoDB源码的分析,也有一些自己的理解,后续将会继续分析其他 ...
- centOS tengine 安装后 不能访问的问题
1 安装方式跟在ubuntu下 安装一样.因为都是用源码 2 但安装好以后发现,局域网电脑访问不了!.原以为是安装错了.又装了一遍,还是不行,最终是iptables 没开放80端口... http: ...
- docker 中搭建tomcat
关于docker中tomcat的搭建,没有什么多说的,直接下载官方镜像 docker pull tomcat (可能会下载到好几个镜像,就是tag不同,有知道为什么的可以告诉我) 然后我看的 ht ...
- Linux 内核学习的经典书籍及途径
from:http://www.zhihu.com/question/19606660 知乎 Linux 内核学习的经典书籍及途径?修改 修改 写补充说明 举报 添加评论 分享 • 邀请回答 ...
- Debian vim没有颜色的解决办法
最近在研究Linux kali 3.12-kali1-amd64 Debian 3.12.6-2kali1 x86_64 GNU/Linux Debian的内核 发现vim竟然没有颜色,root或 ...
- 禁止选择文本和禁用右键 v1.0
var zhonghao={ //绑定事件 myAddEvent: function(obj, sEvent, fn){if(obj.attachEvent){obj.attachEvent('on' ...