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次,每次只阅 ...
随机推荐
- 解压版tomcat设置为系统服务
1.先关闭tomcat,在"开始 "→"运行 "中输入cmd 命令,进入MS-DOS界面 2.进入 到tomcat的bin目录下 (解压版tomcat存放目录, ...
- go build 时报错 cc1.exe: sorry, unimplemented: 64-bit mode not compiled in
最近在玩Go win下尝试编译Go的时候遇到了下面提示(可能是gorocksdb用到了gcc) gcc也需要64位的 最后找到了个帖子: https://github.com/mattn/go-sql ...
- Centos系统安装
Centos系统安装 安装系统前的注意事项 1) 硬件CPU必须支持虚拟化技术,在支持虚拟化的前提下我们还要去把虚拟 的功能打开否则在安装的时候会报错,开启虚拟化需要在BIOS中开启 2)创建虚拟机的 ...
- 负载均衡集群中的session解决方案
前言 在我们给Web站点使用负载均衡之后,必须面临的一个重要问题就是Session的处理办法,无论是PHP.Python.Ruby还是Java,只要使用服务器保存Session,在做负载均衡时都需要考 ...
- Linux运维工程师面试
一.Linux操作系统知识 1.常见的Linux发行版本都有什么?你最擅长哪一个?它的官网网站是什么?说明你擅长哪一块? 2.Linux开机启动流程详细步骤是什么?系统安装完,忘记密码如何破解? ...
- jQuery EasyUI tree中state属性慎用
EasyUI 1.4.4 tree控件中,如果是叶子节点,切忌把state设置为closed,否则该节点会加载整个tree,形成死循环 例如: json入下: [ { "checked&qu ...
- 在hyper安装openwrt
写了长长长一篇文章,结果把标签关了,这篇文章就不见了,草稿箱也没有!!! 只好直接copy原来作者的文章了 下载地址 openwrt image tulip driver 引用 Want to add ...
- 微信分享功能引入页面-控制分享时候调用的标题、图片、url和微信按钮隐藏显示控制
1.设置分享调用的标题.图片.url预览. 2.控制右上角三个点按钮的隐藏显示(和底部工具栏的显示隐藏--未测试). 3.判断网页是否在微信中被调用. <!doctype html> &l ...
- GitHub命令精简教程
Github其实也可以作为文件分享的地方,但是免费空间只有300M,所以不能存放大文件,否则可以成为一个分享资源的下载站,而且非常方便. 常用命令: git add . //添加所有的文件到索引 ...
- Delphi 我常用的几个下载源码的站点
盒子.Delphi园地就不说了,介绍几个其它的: 源码爱好者,特别喜欢. http://www.codefans.net/sort/list_10_1.shtml 新兴源码: http://www.n ...