刷银组刷得好开心= =

离线按权值排序,从大到小插入二叉树,查找树中比这个数大的

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 仰望(离线+平衡树)的更多相关文章

  1. BZOJ 3401: [Usaco2009 Mar]Look Up 仰望( 单调栈 )

    n <= 105 , 其实是10 ^ 5 ....坑...我一开始写了个模拟结果就 RE 了.. 发现这个后写了个单调栈就 A 了... ---------------------------- ...

  2. bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】

    用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...

  3. 3401: [Usaco2009 Mar]Look Up 仰望

    3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 136  Solved: 81[Submi ...

  4. 【BZOJ】3401: [Usaco2009 Mar]Look Up 仰望(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3401 还能更裸一些吗.. 维护一个递减的单调栈 #include <cstdio> #i ...

  5. BZOJ3401: [Usaco2009 Mar]Look Up 仰望

    3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 87  Solved: 58[Submit ...

  6. DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 419  Solve ...

  7. bzoj 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...

  8. BZOJ 3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 动态规划

    3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=34 ...

  9. bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害

    1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着 ...

随机推荐

  1. java学习(四) excel读取

    private static void readExcel() { String filePath = "C:/Standardzid.xls"; File file = new ...

  2. ios数据存储——数据库:SQlite3以及第三方库FMDB

    [reference]http://blog.csdn.net/mad1989/article/details/9322307 原生数据库:SQlite3 一.必备条件 在ios项目中使用sqlite ...

  3. thinkPHP的学习

    1.版本,以3.1为主,因为手册是基于这个的,最新的版本,还没有对应的手册 2.发现一个问题,echo 中文时,出现乱码,而调用模版则正常. 3.写url的注意大小写.index和Index是不同的 ...

  4. Linux - tomcat -jndi数据源配置

    Linux - tomcat -jndi数据源配置 tomcat/conf/context .xml 文件中修改如下 <Resource name="/jdbc/--" au ...

  5. IOS Cell 重影

    效果:重影 原因: 多次创建控件元素 解决:在initWithStyle中进行初始化元素

  6. linx建立用戶&組

    groupadd  test                  创建test用户组 useradd  user1                  创建user1用户 passwd   user1   ...

  7. 2.13.1. 对结果排序(Core Data 应用程序实践指南)

    传递NSSortDescriptor给NSFetchRequest进行排序.示例如下,修改demo方法: NSSortDescriptor *sort = [NSSortDescriptor sort ...

  8. 浅谈JavaWEB入门必备知识之Servlet入门案例详解

    工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...

  9. LVS 负载均衡解决方案 (windows IIS)

    LVS 负载均衡解决方案 因为我们的产品运行的主流平台是WINDOWS+IIS+SQLSERVER(2000以上版本),而LVS+KEEPALIVED是LINUX下的四层负载均衡软件.其有如下特点: ...

  10. cvc-complex-type.2.4.c: The matching wildcard...

    在家里的电脑好好的,在单位的就不行,需要把web app libraties提到 最前面,然后clean一下项目