Gym 102091K The Stream of Corning 2【线段树】
<题目链接>
题目大意:
进行两种操作:1.给定一个数的出现时间、价值、消失时间;
2.进行一次询问,问你当前时间,第K大的数的价值。
解题分析:
采用离线集中处理,将每个数的出现时间和它的消失时间分组存下,并且存下所有的询问。然后就是依次进行所有询问,将这些询问时间点之前的点插入,将已经消失的点删除。因为算了一下复杂度感觉能过,所以就没有用离散化,线段树的代表区间大小就是1~1e6,线段树的节点维护的信息就是这个节点所对应区间的有效节点个数。
#include <bits/stdc++.h>
using namespace std; const int N = 1e5+ , MAX = 1e6+; #define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
struct Node{
int time,val;
Node (int _time=,int _val=):time(_time),val(_val){}
bool operator < (const Node & tmp) const { return time<tmp.time; }
}node1[N],node2[N],d[N]; int tr[MAX<<]; template<typename T>
inline void read(T&x){
x=;int f=;char ch=getchar();
while(ch<'' ||ch>''){ if(ch=='-')f=-; ch=getchar(); }
while(ch>='' && ch<=''){ x=x*+ch-''; ch=getchar(); }
x*=f;
} inline void Pushup(int rt){ tr[rt]=tr[rt<<]+tr[rt<<|]; }
void update(int rt,int l,int r,int loc,int val){
if(l==r){ tr[rt]+=val;return; }
int mid=l+r>>;
if(loc<=mid)update(lson,loc,val);
if(loc>mid)update(rson,loc,val);
Pushup(rt);
}
int query(int rt,int l,int r,int k){
if(l==r)return l;
int mid=l+r>>;
if(tr[rt<<]>=k)return query(lson,k);
else if(tr[rt<<]<k)return query(rson,k-tr[rt<<]); //左子树不够k个,则第k个一定在右子树
}
int main(){
int T,ncase=;scanf("%d",&T);
while(T--){
printf("Case %d:\n",++ncase);
int q;scanf("%d",&q);
int cnt1=,cnt2=,cnt3=;
while(q--){
int op;read(op);
if(op==){
int s,e,w;read(s);read(w);read(e);
cnt1++,cnt2++;
node1[cnt1]=Node(s,w); //node1记录开始时间
node2[cnt2]=Node(e,w); //node2记录结束时间
}else {
int now,k;read(now),read(k);
cnt3++;
d[cnt3].time=now,d[cnt3].val=k; //记录下询问数组
}
}
sort(node1+,node1++cnt1);sort(node2+,node2++cnt2); sort(d+,d++cnt3);
int p1=,p2=; //遍历‘建立’和‘删除’这两个数组的指针
memset(tr,,sizeof(tr)); //建树
for(int i=;i<=cnt3;i++){
int now=d[i].time,k=d[i].val;
while(p1<=cnt1 && node1[p1].time<=now){ //将当前时间前存活的的点插入线段树
update(,,MAX,node1[p1].val,);
p1++;
}
while(p2<=cnt2 && node2[p2].time<now){ //将当前时间前死亡的点删除线段树
update(,,MAX,node2[p2].val,-);
p2++;
}
if(tr[]<k){ puts("-1");continue; } //如果不足k个存活,直接输出-1
printf("%d\n",query(,,MAX,k));
}
}
}
Gym 102091K The Stream of Corning 2【线段树】的更多相关文章
- Gym 100507C Zhenya moves from parents (线段树)
Zhenya moves from parents 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/C Description Z ...
- The Stream of Corning 2( 权值线段树/(树状数组+二分) )
题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...
- 组队训练 K K - The Stream of Corning 2
K - The Stream of Corning 2 这个题目不是很难,因为给你的这个S是单调递增的,所以就用优先队列+权值线段树就可以很快的解决了. 这个+读入挂可以优化,不过不用也没关系. #i ...
- Codeforces Gym 100803G Flipping Parentheses 线段树+二分
Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- Codeforces GYM 100114 D. Selection 线段树维护DP
D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...
- 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)
题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...
随机推荐
- ios 本地存储文件夹的使用注意
文件夹 tmp 属于临时文件夹,不需要自己删除,系统会在应用退出后清空 文件夹 Library 下面的子文件 Caches 也是用来存储的,,但是Library 基本上不会被清除,但是在内存不足的 ...
- python 排序 sort和sorted
当我们从数据库中获取一写数据后,一般对于列表的排序是经常会遇到的问题,今天总结一下python对于列表list排序的常用方法: 第一种:内建方法sort() 可以直接对列表进行排序 用法: list. ...
- Net 4.5 WebSocket 在 Windows 7, Windows 8 and Server 2012上的比较
.Net 4.5 WebSocket Server Running on Windows 7? Net 4.5 WebSocket Server 可以运行在 Windows 7,但是Net 4.5的 ...
- 修改 sql 提示符信息:
Last login: Thu Dec 8 19:18:08 2016 from 192.168.242.1 [root@localhost ~]# su - oracle [oracle@local ...
- Confluence 6 用户宏示例 - Hello World
下面示例显示了如何创建一个用户宏,在这个用户宏中显示文本 'Hello World!' 和任何用户在宏内容中输入的内容. Macro name helloworld Visibility Visibl ...
- LeetCode(85):最大矩形
Hard! 题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0",&quo ...
- vue-cli3.0 使用postcss-plugin-px2rem(推荐)和 postcss-pxtorem(postcss-px2rem)自动转换px为rem 的配置方法;
如何在vue-cli3.0中使用postcss-plugin-px2rem 插件 插件的作用是 自动将vue项目中的px转换为rem . 为什么这三个中要推荐 postcss-plugin-px2r ...
- JMeter 中对于Json数据的处理方法
JMeter中对于Json数据的处理方法 http://eclipsesource.com/blogs/2014/06/12/parsing-json-responses-with-jmeter/ J ...
- 常见的爬虫分析库(4)-爬虫之PyQuery
PyQuery 是 Python 仿照 jQuery 的严格实现.语法与 jQuery 几乎完全相同. 官方文档:http://pyquery.readthedocs.io/ 安装 1 pip ins ...
- JAVA 代码中使用中文的办法
在编译代码中插入 -encoding UTF-8 示例: javac -encoding UTF-8 *.java