codeforces 91B Queue 线段树,简单查询
来源:互联网 发布:js单例和实际化 编辑:程序博客网 时间:2024/06/05 08:13
题目链接:http://codeforces.com/problemset/problem/91/B
思路:线段树解决,每个节点存储该段最小的数。。。多简单的二分查找就能解决的问题楞是被我这逗比用线段树做了
这两天在做线段树的题,所以百度了一下codeforces上哪道题是线段树,一个博客说这个是,就做了。看到题后就想着怎么用线段树做,很顺畅的通过。(第一次RE因为数组开小了)
结果做完看到CF上这道题的tags,说是二分查找。。。瞬间觉得自己白痴了,这个题二分查找不是更简单快速吗。。。
泪流满面的贴上线段树的代码:
///2014.6.9///codeforecs 91B//Time:124MS#include <cstdio>#define lson l , m , rt<<1#define rson m+1 , r , rt<<1|1const int maxn = 110000;int n;int num[maxn];int segT[maxn<<2];void PushUp(int rt){ segT[rt] = segT[rt<<1]<segT[rt<<1|1] ? segT[rt<<1]:segT[rt<<1|1];}void build(int l,int r,int rt){ if( l == r ){ scanf("%d",&segT[rt]); num[l] = segT[rt]; return; } int m = (l+r)>>1; build(lson); build(rson); PushUp(rt);}int query(int L,int R,int x,int l,int r,int rt){ if( segT[rt]>=x ) return -1; if( l==r ){ if( segT[rt]<x ) return l; } else{ int m = (l+r)>>1; if( m<R && segT[rt<<1|1] < x ) return query(L,R,x,rson); else if( L<=m ) return query(L,R,x,lson); } return -1;}int main(){ // freopen("in","r",stdin); // freopen("out","w",stdout); scanf("%d",&n); build(0,n-1,1); for(int i=0 ; i<n-1 ; i++){ int r = query(i+1,n-1,num[i],0,n-1,1); if( r==-1 ) printf("-1 "); else printf("%d ",(r-i-1) ); } printf("-1\n"); return 0;}
0 0
- codeforces 91B Queue 线段树,简单查询
- CodeForces 91B Queue【线段树】
- CodeForces 91B Queue 线段树 单点更新 成段查询
- CodeForces 91B Queue (线段树单点操作)
- Codeforces 85B. Embassy Queue【线段树、贪心】
- Codeforces 91B-Queue
- codeforces 91b Queue
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
- 【Codeforces 768 B Code For 1】+ 简单线段树
- Codeforces 482B 线段树
- 解题报告 之 CodeForces 91B Queue
- CodeForces 91B-K - Queue 二分
- 单调队列 CodeForces 91B Queue
- CodeForces 490B Queue
- 【CODEFORCES】 B. Queue
- CodeForces - 91B 单调队列 或 线段树
- codeforces 581B Luxurious Houses(线段树点更新,区间查询)
- Codeforces 525B. Pasha and String【线段树 区间更新 单点查询】
- 最长最短单词
- Apache+Tomcat通过ajp_proxy配置php和jsp环境
- 点与指向符操作
- 放的地方方法
- cakephp发送post请求
- codeforces 91B Queue 线段树,简单查询
- leetcode——Divide Two Integers 不用乘除取余操作求除法(AC)
- 二叉树前序Preorder遍历和后序Postorder遍历的非递归实现
- 回调函数
- 关于Google Guava
- OracleService服务不见了|OracleServiceXE服务没有了
- method 与 function的区别
- 【推荐】.NET使用NPOI组件将数据导出Excel
- 设计模式之单例模式