刷银组刷得好开心= =

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

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. CLR VIA C#: 基元类型、 引用类型 和 值类型

    一.基元类型 . 引用类型 和 值类型的区别: 1.基元类型(primitive type):编译器直接支持的数据类型: 基元类型 直接映射到 FCL 中存在的类型. C# 小写是基元类型,例如:st ...

  2. 安装arm-linux-gcc交叉编译器

    1.开发平台 虚拟机:VMware 12 操作系统:Ubuntu 14.04 2.准备交叉编译工具包(arm-linux-gcc-4.5.1) 编译uboot和linux kernel都需要gnu交叉 ...

  3. easyui 翻译

    1,DataGrid checkOnSelect: 设置成true:用户点击一行的时候,复选框被选中或者是取消选中 设置成false:只有当用户点击复选框的时候,复选框才能被选中或者是取消选中 sel ...

  4. UVa 10667 - Largest Block

    题目大意:这个也是和UVa 836 - Largest Submatrix差不多,修改一下数据就可以套用代码的. #include <cstdio> #include <cstrin ...

  5. JAVA短信验证登录

    短信验证登陆 1.点击触发,以电话号码为参数调用发送验证登录短信方法 2.默认模板为验证模板 生成6位验证码 3.将生成的验证码和手机号码放入缓存,(已经设置好缓存存放时间) 4.调用发送模板短信方法 ...

  6. error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches

    error: WatchKit App doesn't contain any WatchKit Extensions whose WKAppBundleIdentifier matches &quo ...

  7. 【spoj LCS2】 Longest Common Substring II

    http://www.spoj.com/problems/LCS2/ (题目链接) 题意 求多个串的最长公共子串 Solution 对其中一个串构造后缀自动机,然后其它串在上面跑匹配.对于每个串都可以 ...

  8. Apache自带压力测试工具ab用法简介

    ab命令原理 ab命令会创建很多的并发访问线程,模拟多个访问者同时对某一URL进行访问.它的测试目标是基于URL的,因此,既可以用来测试Apache的负载压力,也可以测试nginx.lighthttp ...

  9. 超炫的时间轴jquery插件Timeline Portfolio

    Timeline Portfolio是一款按时间顺序专业显示事件的jquery时间轴插件,可以根据时间的先后嵌入各种媒体包括微博,视频和地图等.这个展现的模式非常适合设计师的作品集和个人简历的展示.T ...

  10. 部署Replica Sets及查看相关配置

    MongoDB 支持在多个机器中通过异步复制达到故障转移和实现冗余.多机器中同一时刻只有一台是用于写操作.正是由于这个情况,为MongoDB 提供了数据一致性的保障.担当Primary 角色的机器能把 ...