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次,每次只阅 ...
随机推荐
- 【转】【SQL SERVER】怎样处理作业中的远程服务器错误(42000)
(SQL SERVER)怎样处理作业中的远程服务器错误(42000) 问: 1.我创建了一个链接服务器. 2.在两台服务器之间创建了新的SQL用户. 3.编写了访问链接服务器的SQL语句,执行成功. ...
- 产品原型设计5:移动App原型设计神器 - POP(Prototyping on Paper)
一般来说,苦逼的互联网产品经理们都知道 Axure 这个原型设计工具,一方面是因为它提供了足够简单的拖拽操作,易上手,且有很多模板方便复用:另一方是因为它可直接输出html,直接在浏览器里给团队成员和 ...
- spring定时器用Annotation兑现
spring定时器用Annotation实现 0人收藏此文章, 我要收藏发表于3个月前 , 已有46次阅读 共0个评论 1.ApplicationContext.xml配置 a).需要在xmlns里面 ...
- 飞锐GIS开发基础系列
3s知识库和GIS科研站,未经允许,禁止任何形式的复制.转载和传播.联系方式:276888624@qq.com 品GIS,品开发,品人生...... http://www.3sbase.com/3sb ...
- [翻译]ASP.NET Web API的路由
原文:Routing in ASP.NET Web API 在我们新建一个Web API项目时,会在App_Start文件夹下的WebApiConfig.cs中定义一个默认路由: config.Rou ...
- hibernate细节一
public void saveUser(){ Session session=JdbcUtil.getSession(); User user=new User(); ...
- javascript face ++
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- thinkphp+mysql+bootstrap
#thinkphp+mysql+bootstrapthinkphp3.2.3,bootstrap V3一个简易的企业cms网站系统,只要将clients的host改为对应的域名即可.thinkphp. ...
- IIS日志分析
发现一个强大的图形化IIS日志分析工具——Log Parser Studio,下面分享一个实际操作案例. 1. 安装Log Parser Studio a) 需要先安装Log Parser,下载地址: ...
- POJ 1836 Alignment 水DP
题目: http://poj.org/problem?id=1836 没读懂题,以为身高不能有相同的,没想到排中间的两个身高是可以相同的.. #include <stdio.h> #inc ...