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为根节点的子 ...
随机推荐
- yun
# Author:zhang# -*- coding:utf-8 -*-"""https://workyun.com/ 云端工作"""imp ...
- Android 框架 Afinal使用
介绍android Afinal框架功能: Afinal是一个开源的android的orm和ioc应用开发框架.在android应用开发中,通过Afinal的ioc框架,诸如UI绑定,事件绑定,通过注 ...
- linux 基础知识(三)
抽空把Linux的一些基础的东西再补充一下,安全的东西真的很多都是要自己不断的学习,很多还是今天学习了一点时间过后不用就会忘记.所以学习的东西就是要不断地往复. 有时候感觉有时候快就是慢,慢就是快. ...
- cf14d 树的直径,枚举删边
#include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...
- CF1000G
蜜汁树形dp... 首先分析一下:他要求一条边至多只能经过两次,那么很容易会发现:从x到y这一条路径上的所有边都只会被经过一次.(如果过去再回来那么还要过去,这样就三次了,显然不合法) 那么其他能产生 ...
- 第四周学习总结-HTML
2018年8月5日 这是暑假第四周,这一周我在菜鸟教程网学到了许多HTML的知识.HTML编写网页不像C语言.Java语言那必须有主方法.主函数什么的,它基本上都是标签(元素),但是它可以与CSS(层 ...
- MySQL报错: Character set ‘utf8mb4‘ is not a compiled character set and is not specified in the ‘/usr/share/mysql/charsets/Index.xml‘ file
由于日常程序使用了字符集utf8mb4,为了避免每次更新时,set names utf8mb4,就把配置文件改了,如下: [root@~]# vim /etc/my.cnf #my.cnf [clie ...
- SSM文件下载
SSM框架文件下载比文件上传稍微麻烦一点,但这次还是写成最简朴的形式,哈哈~如下 参考:http://blog.csdn.net/lcx556224523/article/details/702076 ...
- 步步为营103-ZTree 二级联动
1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...
- 从零开始学C#——不再更新,直接进入高阶教程
从零开始学习C#不再更新,直接进入高阶教程. 入门教程,请自行谷歌.百度吧,有很多这样的教程. 编程是一件实践性很强的事情,那么接下来的文章将开始进行开发项目. 还在编程中迷茫的人们,先暂时放下一切的 ...