bzoj1878 树状数组 要一点思考
2015-06-18 19:02
435 查看
一开始快排敲错两次
View Code
Submit: 2154 Solved: 1062
[Submit][Status][Discuss]
1 2 3 4 3 5
3
1 2
3 5
2 6
2
4
对于40%的数据,N ≤ 3000,M ≤ 200000;
对于100%的数据,N ≤ 50000,M ≤ 200000。
[Submit][Status][Discuss]
program hehe; var t,n,m,i,j,k:longint; c:array[0..1000000] of longint; pre,next,a,x:array[0..50005] of longint; ans,p,z,y:array[0..200005] of longint; procedure sort(l,r:longint); var zuo,you,mid:longint; begin zuo:=l; you:=r; mid:=y[(l+r)>>1]; repeat while y[zuo]<mid do inc(zuo); while y[you]>mid do dec(you); if zuo<=you then begin t:=z[zuo]; z[zuo]:=z[you]; z[you]:=t; t:=y[zuo]; y[zuo]:=y[you]; y[you]:=t; t:=p[zuo]; p[zuo]:=p[you]; p[you]:=t; dec(you); inc(zuo); end; until zuo>you; if l<you then sort(l,you); if zuo<r then sort(zuo,r); end; function lowbit(a:longint):longint; begin exit(a and(-a)); end; procedure add(a,b:longint); begin while a<=n do begin x[a]:=x[a]+b; a:=a+lowbit(a); end; end; function find(a:longint):longint; var s:longint; begin s:=0; while a>0 do begin s:=s+x[a]; a:=a-lowbit(a); end; exit(s); end; begin readln(n); fillchar(a,sizeof(a),255); for i:=1 to n do begin read(a[i]); pre[i]:=c[a[i]]; c[a[i]]:=i; next[pre[i]]:=i; end; read(m); for i:=1 to m do read(z[i],y[i]); for i:=1 to m do p[i]:=i; sort(1,m); for i:=1 to m do begin while j<y[i] do begin inc(j); add(pre[j]+1,1); add(j+1,-1) end; ans[p[i]]:=find(z[i]); end; for i:=1 to m do writeln(ans[i]); end.
View Code
1878: [SDOI2009]HH的项链
Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2154 Solved: 1062
[Submit][Status][Discuss]
Description
HH有一串由各种漂亮的贝壳组成的项链。HH相信不同的贝壳会带来好运,所以每次散步 完后,他都会随意取出一段贝壳,思考它们所表达的含义。HH不断地收集新的贝壳,因此, 他的项链变得越来越长。有一天,他突然提出了一个问题:某一段贝壳中,包含了多少种不同 的贝壳?这个问题很难回答。。。因为项链实在是太长了。于是,他只好求助睿智的你,来解 决这个问题。Input
第一行:一个整数N,表示项链的长度。 第二行:N个整数,表示依次表示项链中贝壳的编号(编号为0到1000000之间的整数)。 第三行:一个整数M,表示HH询问的个数。 接下来M行:每行两个整数,L和R(1 ≤ L ≤ R ≤ N),表示询问的区间。Output
M行,每行一个整数,依次表示询问对应的答案。Sample Input
61 2 3 4 3 5
3
1 2
3 5
2 6
Sample Output
22
4
HINT
对于20%的数据,N ≤ 100,M ≤ 1000;对于40%的数据,N ≤ 3000,M ≤ 200000;
对于100%的数据,N ≤ 50000,M ≤ 200000。
Source
Day2[Submit][Status][Discuss]
相关文章推荐
- A Java int array example
- 自咏并呈友
- IOS开发中有用的第三方
- 解决sublime text输出decode error问题
- debian-svn配置多个版本库
- RadioGroup单选按钮排版
- 13-17章读后感
- 第十六章:网络IPC:套接字
- megacli创建RAID1
- Editplus调用TortoiseSVN进行版本控制(总结)
- 《一个程序猿的生命周期》的感悟
- UIAlertController的使用示例
- 简单定时器的实现
- Android对话框的两种创建方式
- OC基础:数组.字典.集
- iOS TableView实现QQ好友列表(二)
- OC基础:数组.字典.集 分类: ios学习 OC 2015-06-18 18:58 47人阅读 评论(0) 收藏
- Hibernate常用方法之_删除
- vs2015 支持Android arm neon Introducing Visual Studio’s Emulator for Android
- C# 快捷键