刷银组刷得好开心= =

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

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. STM32 驱动1602液晶

    利用STM32f103c8t6单片机驱动1602A液晶进行显示功能 上图即为写入信息后的效果图 本人用的是STM32的核心系统,无任何外设 库函数:3.5版本的库函数 驱动模式:采用4线驱动模式 供电 ...

  2. foreach笔记

    结合泛型使用,不然就只能写成for(Object o : T). 缺点是没有下标,如下面代码 public class ForeachTest { public static void main(St ...

  3. T4模板使用

    本例使用的数据库是Northwind 1.新建tt文本模板customer.tt 2. 修改customer.tt内容为 <#@ template debug="false" ...

  4. Makefile — 基础

    参考: 跟我一起写 Makefile GNU make <GNU+Make项目管理(第三版)> 1.Makefile用途 使用GNU Make工具来管理程序是每个Linux工程师必须掌握的 ...

  5. 2781: [JSOI2007]文本生成器

    2781: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 8  Solved: 4[Submit][Status][We ...

  6. linux Cron 执行Django 任务计划

    用shell 脚本调用python 脚本如下 #!/bin/bash export FLAVOR=liveexport PYTHONPATH=$PYTHONPATH:/home/alex/Django ...

  7. 使用python爬取MedSci上的期刊信息

    使用python爬取medsci上的期刊信息,通过设定条件,然后获取相应的期刊的的影响因子排名,期刊名称,英文全称和影响因子.主要过程如下: 首先,通过分析网站http://www.medsci.cn ...

  8. php 常用代码段

    1.写文件 $fp = fopen("jsapi_ticket.json", "w+"); fwrite($fp, $str); fclose($fp); 2. ...

  9. 使用布局(Layout资源)

    实际上从我们学习第一个Android应用开始,已经开始接触Android的Layout资源了,因此此处不会详细介绍Android Layout资源的知识,会对Layout资源进行简单的归纳. Layo ...

  10. HTML 相同name 传递一个数组

    今天发现一个很厉害的东西 在input表单中,name名称可以是一个,后面[],里面跟名称,和数组一样,传递到PHP中也是一个数组 <html> <body> <form ...