<题目链接>

题目大意:

进行两种操作:1.给定一个数的出现时间、价值、消失时间;

       2.进行一次询问,问你当前时间,第K大的数的价值。

解题分析:

采用离线集中处理,将每个数的出现时间和它的消失时间分组存下,并且存下所有的询问。然后就是依次进行所有询问,将这些询问时间点之前的点插入,将已经消失的点删除。因为算了一下复杂度感觉能过,所以就没有用离散化,线段树的代表区间大小就是1~1e6,线段树的节点维护的信息就是这个节点所对应区间的有效节点个数。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e5+ , MAX = 1e6+;
  5.  
  6. #define lson rt<<1,l,mid
  7. #define rson rt<<1|1,mid+1,r
  8. struct Node{
  9. int time,val;
  10. Node (int _time=,int _val=):time(_time),val(_val){}
  11. bool operator < (const Node & tmp) const { return time<tmp.time; }
  12. }node1[N],node2[N],d[N];
  13.  
  14. int tr[MAX<<];
  15.  
  16. template<typename T>
  17. inline void read(T&x){
  18. x=;int f=;char ch=getchar();
  19. while(ch<'' ||ch>''){ if(ch=='-')f=-; ch=getchar(); }
  20. while(ch>='' && ch<=''){ x=x*+ch-''; ch=getchar(); }
  21. x*=f;
  22. }
  23.  
  24. inline void Pushup(int rt){ tr[rt]=tr[rt<<]+tr[rt<<|]; }
  25. void update(int rt,int l,int r,int loc,int val){
  26. if(l==r){ tr[rt]+=val;return; }
  27. int mid=l+r>>;
  28. if(loc<=mid)update(lson,loc,val);
  29. if(loc>mid)update(rson,loc,val);
  30. Pushup(rt);
  31. }
  32. int query(int rt,int l,int r,int k){
  33. if(l==r)return l;
  34. int mid=l+r>>;
  35. if(tr[rt<<]>=k)return query(lson,k);
  36. else if(tr[rt<<]<k)return query(rson,k-tr[rt<<]); //左子树不够k个,则第k个一定在右子树
  37. }
  38. int main(){
  39. int T,ncase=;scanf("%d",&T);
  40. while(T--){
  41. printf("Case %d:\n",++ncase);
  42. int q;scanf("%d",&q);
  43. int cnt1=,cnt2=,cnt3=;
  44. while(q--){
  45. int op;read(op);
  46. if(op==){
  47. int s,e,w;read(s);read(w);read(e);
  48. cnt1++,cnt2++;
  49. node1[cnt1]=Node(s,w); //node1记录开始时间
  50. node2[cnt2]=Node(e,w); //node2记录结束时间
  51. }else {
  52. int now,k;read(now),read(k);
  53. cnt3++;
  54. d[cnt3].time=now,d[cnt3].val=k; //记录下询问数组
  55. }
  56. }
  57. sort(node1+,node1++cnt1);sort(node2+,node2++cnt2); sort(d+,d++cnt3);
  58. int p1=,p2=; //遍历‘建立’和‘删除’这两个数组的指针
  59. memset(tr,,sizeof(tr)); //建树
  60. for(int i=;i<=cnt3;i++){
  61. int now=d[i].time,k=d[i].val;
  62. while(p1<=cnt1 && node1[p1].time<=now){ //将当前时间前存活的的点插入线段树
  63. update(,,MAX,node1[p1].val,);
  64. p1++;
  65. }
  66. while(p2<=cnt2 && node2[p2].time<now){ //将当前时间前死亡的点删除线段树
  67. update(,,MAX,node2[p2].val,-);
  68. p2++;
  69. }
  70. if(tr[]<k){ puts("-1");continue; } //如果不足k个存活,直接输出-1
  71. printf("%d\n",query(,,MAX,k));
  72. }
  73. }
  74. }

Gym 102091K The Stream of Corning 2【线段树】的更多相关文章

  1. Gym 100507C Zhenya moves from parents (线段树)

    Zhenya moves from parents 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/C Description Z ...

  2. The Stream of Corning 2( 权值线段树/(树状数组+二分) )

    题意: 有两种操作:1.在[l,r]上插入一条值为val的线段 2.问p位置上值第k小的线段的值(是否存在) 特别的,询问的时候l和p合起来是一个递增序列 1<=l,r<=1e9:1< ...

  3. 组队训练 K K - The Stream of Corning 2

    K - The Stream of Corning 2 这个题目不是很难,因为给你的这个S是单调递增的,所以就用优先队列+权值线段树就可以很快的解决了. 这个+读入挂可以优化,不过不用也没关系. #i ...

  4. Codeforces Gym 100803G Flipping Parentheses 线段树+二分

    Flipping Parentheses 题目连接: http://codeforces.com/gym/100803/attachments Description A string consist ...

  5. Codeforces Gym 100231B Intervals 线段树+二分+贪心

    Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...

  6. Codeforces Gym 100513F F. Ilya Muromets 线段树

    F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...

  7. Codeforces GYM 100114 D. Selection 线段树维护DP

    D. Selection Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Descriptio ...

  8. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  9. K. Random Numbers(Gym 101466K + 线段树 + dfs序 + 快速幂 + 唯一分解)

    题目链接:http://codeforces.com/gym/101466/problem/K 题目: 题意: 给你一棵有n个节点的树,根节点始终为0,有两种操作: 1.RAND:查询以u为根节点的子 ...

随机推荐

  1. yun

    # Author:zhang# -*- coding:utf-8 -*-"""https://workyun.com/ 云端工作"""imp ...

  2. Android 框架 Afinal使用

    介绍android Afinal框架功能: Afinal是一个开源的android的orm和ioc应用开发框架.在android应用开发中,通过Afinal的ioc框架,诸如UI绑定,事件绑定,通过注 ...

  3. linux 基础知识(三)

    抽空把Linux的一些基础的东西再补充一下,安全的东西真的很多都是要自己不断的学习,很多还是今天学习了一点时间过后不用就会忘记.所以学习的东西就是要不断地往复. 有时候感觉有时候快就是慢,慢就是快. ...

  4. cf14d 树的直径,枚举删边

    #include<bits/stdc++.h> using namespace std; #define maxn 300 ]; int n,head[maxn],tot,a,b,dis[ ...

  5. CF1000G

    蜜汁树形dp... 首先分析一下:他要求一条边至多只能经过两次,那么很容易会发现:从x到y这一条路径上的所有边都只会被经过一次.(如果过去再回来那么还要过去,这样就三次了,显然不合法) 那么其他能产生 ...

  6. 第四周学习总结-HTML

    2018年8月5日 这是暑假第四周,这一周我在菜鸟教程网学到了许多HTML的知识.HTML编写网页不像C语言.Java语言那必须有主方法.主函数什么的,它基本上都是标签(元素),但是它可以与CSS(层 ...

  7. 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 ...

  8. SSM文件下载

    SSM框架文件下载比文件上传稍微麻烦一点,但这次还是写成最简朴的形式,哈哈~如下 参考:http://blog.csdn.net/lcx556224523/article/details/702076 ...

  9. 步步为营103-ZTree 二级联动

    1:添加引用 <%--流程类别多选--引用js和css文件--开始--%> <link rel="stylesheet" href="../css/zT ...

  10. 从零开始学C#——不再更新,直接进入高阶教程

    从零开始学习C#不再更新,直接进入高阶教程. 入门教程,请自行谷歌.百度吧,有很多这样的教程. 编程是一件实践性很强的事情,那么接下来的文章将开始进行开发项目. 还在编程中迷茫的人们,先暂时放下一切的 ...