BZOJ 3401: [Usaco2009 Mar]Look Up 仰望(离线+平衡树)
刷银组刷得好开心= =
离线按权值排序,从大到小插入二叉树,查找树中比这个数大的
CODE:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100010
struct node{
int ch[2],x,r;
}t[maxn];
int cmp(int x,int y){
if (t[x].x>y) return 0;
if (t[x].x==y) return -1;
return 1;
}
int rotate(int &x,int d){
int u=t[x].ch[d];
t[x].ch[d]=t[u].ch[d^1];
t[u].ch[d^1]=x;
x=u;
return 0;
}
int l;
int insert(int &x,int y){
if (x==0) {x=++l;t[x]=(node){{0,0},y,rand()};return 0;}
int d=cmp(x,y);
insert(t[x].ch[d],y);
if (t[t[x].ch[d]].r>t[x].r) rotate(x,d);
return 0;
}
int an;
int maxnum(int x,int y){
if (x==0) return 0;
int d=cmp(x,y);
if (d==0) an=t[x].x;
maxnum(t[x].ch[d],y);
return 0;
}
int id[maxn],ans[maxn],a[maxn];
bool cmp1(int x,int y){
if (a[x]==a[y]) return x<y;
return a[x]>a[y];
}
int main(){
int n;
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",a+i);
for (int i=1;i<=n;i++) id[i]=i;
sort(id+1,id+1+n,cmp1);
int root=0;
for (int i=1;i<=n;i++) {
an=0;
maxnum(root,id[i]);
ans[id[i]]=an;
insert(root,id[i]);
}
for (int i=1;i<=n;i++) printf("%d\n",ans[i]);
return 0;
}
BZOJ 3401: [Usaco2009 Mar]Look Up 仰望(离线+平衡树)的更多相关文章
- BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )
n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了... ---------------------------- ...
- bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】
用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...
- 3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 136 Solved: 81[Submi ...
- 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=3401 还能更裸一些吗.. 维护一个递减的单调栈 #include <cstdio> #i ...
- BZOJ3401: [Usaco2009 Mar]Look Up 仰望
3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 87 Solved: 58[Submit ...
- DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 419 Solve ...
- bzoj 3399: [Usaco2009 Mar]Sand Castle城堡
3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...
- BZOJ 3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 动态规划
3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=34 ...
- bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着 ...
随机推荐
- 每个Javascript开发者都应当知道的那些事
每个Javascript开发者都应当知道的那些事 2015-06-07 前端大全 (点击上方蓝字,可快速关注我们) Javascript是一种日益增长的语言,特别是现在ECMAScript规范按照每年 ...
- 使用python爬虫抓站的一些技巧总结:进阶篇
Reference:http://python.jobbole.com/82000/ 一.gzip/deflate支持 现在的网页普遍支持gzip压缩,这往往可以解决大量传输时间,以VeryCD的主页 ...
- 八 Appium常用方法介绍
由于appium是扩展了Webdriver协议,所以可以使用webdriver提供的方法,比如在处理webview页面,完全可以使用webdriver中的方法.当然在原生应用中,也可以使用. 1.元素 ...
- PAT1013 数素数
思路: 打印素数表 然后找出对应区间[m,n]中的素数 #include <iostream> #include <vector> #include <cmath> ...
- Bootstrap每天必学之导航条
http://www.jb51.net/article/75534.htm Bootstrap每天必学之导航条,本文向大家讲解了多种多样的导航条,以及导航条中元素的实现方法,感兴趣的小伙伴们可以参考一 ...
- 推荐系统中的Graph Model
转自:http://www.cnblogs.com/wentingtu/archive/2012/05/28/2521166.html 推荐中对graph model的研究主要有两个方面,一个是如何构 ...
- PKU-1704-Georgia and Bob
题目链接 http://poj.org/problem?id=1704 这个题目是个好题,没有两下子是做不出的,其中考到,要你排序,如何把题目化成我们熟知的东西, 在这个题中我开始用选择法排序,他给我 ...
- 查找子字符串----KMP算法深入剖析
假设主串:a b a b c a b c a c b a b 子串:a b c a c 1.一般匹配算法 逐个字符的比较,匹配过程如下: 第一趟匹配 a b a b c a b c a c ...
- spring mvc在Controller中获取ApplicationContext
spring mvc在Controller中获取ApplicationContext web.xml中进行正常的beans.xml和spring-mvc.xml的配置: 需要在beans.xml中进行 ...
- WinForm TextBox 实现自动索引功能
源代码如下: http://pan.baidu.com/s/1bnOz4hp