题目链接:https://vjudge.net/problem/Gym-101630A

题意: n个事件,t=1 代表增加一个圆心为(x,y),半径为 abs(y)的靶子,t=2,代表射击的坐标为(x,y)并且询问是否在已出现的靶子上,如果在则输出第几个事件的编号,否则输出-1。

该题有个坑点:(一开始没看到)一个靶子只能被射击一次。

思路:可以用set存每个靶子的位置,但是需要对靶子按照前后空隙大小进行排序,这样之后我们就可以通过射击点的横坐标对set二分查找出最靠近其的靶子,然后就可以往后遍历是否存在一个靶子使得子弹在上面,但是光这样做在P5 就T了,仔细想了想发现,我们之前排序的作用,一旦我们的 x<x1-abs(y) 那么后面的靶子也不用再遍历了,直接退出循环,这样就会减少很多不必要的操作。

然后还有简单的题解是通过线段树维护暴搜就好了:https://blog.csdn.net/lzc504603913/article/details/83958171

代码:

  1. int n;
  2. struct node{
  3. ll x,y,id;
  4. bool operator<(const node a) const {
  5. return x+abs(y)<a.x-abs(a.y); //排序方法
  6. }
  7. };
  8. bool dis(ll x1,ll y1,ll a,ll b)
  9. {
  10. return (x1-a)*(x1-a)+(y1-b)*(y1-b)<y1*y1;
  11. }
  12. set<node>s;
  13. void run()
  14. {
  15. scanf("%d",&n);
  16. ll t,a,b;
  17. for(int i=1;i<=n;i++)
  18. {
  19. scanf("%lld%lld%lld",&t,&a,&b);
  20. if(t==1)
  21. {
  22. node Node;
  23. Node.x=a;
  24. Node.y=b;
  25. Node.id=i;
  26. s.insert(Node);
  27. }
  28. else{
  29. node tmp;
  30. bool flag=0;
  31. tmp.x=a;
  32. tmp.y=0;
  33. set<node>::iterator it=s.lower_bound(tmp);
  34. while(it!=s.end())
  35. {
  36. ll x1=it->x,y1=it->y;
  37.  
  38. if(dis(x1,y1,a,b))
  39. {
  40. flag=true;
  41. printf("%lld\n",it->id);
  42. s.erase(it);
  43. break;
  44. }
  45. if(it->x-a>abs(it->y)) break;//关键2;
  46. it++;
  47. }
  48. if(!flag) puts("-1");
  49. }
  50. }
  51. }
  52. signed main()
  53. {
  54. // int t=rd();
  55. // while(t--)
  56. run();
  57. return 0;
  58. }

线段树的方法:

  1. #include<iostream>
  2. #include<deque>
  3. #include<memory.h>
  4. #include<stdio.h>
  5. #include<map>
  6. #include<string>
  7. #include<algorithm>
  8. #include<vector>
  9. #include<math.h>
  10. #include<stack>
  11. #include<queue>
  12. #include<bitset>
  13. #include<set>
  14. #define INF (0x3f3f3f3f)
  15. using namespace std;
  16. typedef long long int ll;
  17. const int MAXN=200010;
  18.  
  19. ll X[MAXN];
  20. ll Y[MAXN];
  21. int tot;
  22. vector<int> V[MAXN*30];
  23. int ls[MAXN*30];
  24. int rs[MAXN*30];
  25. int ans;
  26.  
  27. bool check(ll x1,ll y1,ll x2,ll y2){
  28. if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<y1*y1)
  29. return 1;
  30. return 0;
  31. }
  32.  
  33. void update(int L,int R,int C,int l,int r,int &rt){
  34.  
  35. if(!rt)
  36. rt=++tot;
  37. if(L<=l&&r<=R)
  38. {
  39. V[rt].push_back(C);
  40. return;
  41. }
  42. int m=(l+r)>>1;
  43. if(L<=m)
  44. update(L,R,C,l,m,ls[rt]);
  45. if(R>m)
  46. update(L,R,C,m+1,r,rs[rt]);
  47. }
  48.  
  49. void update(int L,int R,int l,int r,int rt){
  50. if(L<=l&&r<=R){
  51. vector<int> tmp;
  52. for(auto &t:V[rt]){
  53. if(t!=ans){
  54. tmp.push_back(t);
  55. }
  56. }
  57. V[rt]=tmp;
  58. return;
  59. }
  60. int m=(l+r)>>1;
  61. if(L<=m)
  62. update(L,R,l,m,ls[rt]);
  63. if(R>m)
  64. update(L,R,m+1,r,rs[rt]);
  65. }
  66.  
  67. void query(int L,int R,int l,int r,int rt){
  68. if(!rt)
  69. return;
  70. for(auto &t:V[rt]){
  71. if(check(X[t],Y[t],L,R)){
  72. ans=t;
  73. return;
  74. }
  75. }
  76. if(l==r)
  77. return;
  78. int m=(l+r)>>1;
  79. if(L<=m)
  80. query(L,R,l,m,ls[rt]);
  81. else
  82. query(L,R,m+1,r,rs[rt]);
  83. }
  84.  
  85. int main(){
  86.  
  87. int N;
  88. scanf("%d",&N);
  89. int rt=0;
  90. for(int i=1;i<=N;i++){
  91.  
  92. int op,x,y;
  93. scanf("%d%d%d",&op,&x,&y);
  94. if(op==1){
  95. X[i]=x;
  96. Y[i]=y;
  97. update(x-y,x+y,i,-INF,INF,rt);
  98. }
  99. else{
  100. ans=-1;
  101. query(x,y,-INF,INF,rt);
  102. printf("%d\n",ans);
  103. if(ans!=-1)
  104. update(X[ans]-Y[ans],X[ans]+Y[ans],-INF,INF,rt);
  105. }
  106. }
  107.  
  108. return 0;
  109. }

2017-2018 ACM-ICPC Northern Eurasia(A.Archery Tournament)的更多相关文章

  1. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  2. 牛客网暑期ACM多校训练营(第五场):F - take

    链接:牛客网暑期ACM多校训练营(第五场):F - take 题意: Kanade有n个盒子,第i个盒子有p [i]概率有一个d [i]大小的钻石. 起初,Kanade有一颗0号钻石.她将从第1到第n ...

  3. 牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献)

    牛客网暑期ACM多校训练营(第三场)H Diff-prime Pairs (贡献) 链接:https://ac.nowcoder.com/acm/contest/141/H来源:牛客网 Eddy ha ...

  4. 2018牛客网暑期ACM多校训练营(第三场) A - PACM Team - [四维01背包][四约束01背包]

    题目链接:https://www.nowcoder.com/acm/contest/141/A 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  5. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  6. Trace 2018徐州icpc网络赛 (二分)(树状数组)

    Trace There's a beach in the first quadrant. And from time to time, there are sea waves. A wave ( xx ...

  7. 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]

    题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...

  8. 2018牛客网暑期ACM多校训练营(第十场)A Rikka with Lowbit (树状数组)

    链接:https://ac.nowcoder.com/acm/contest/148/A 来源:牛客网 Rikka with Lowbit 时间限制:C/C++ 5秒,其他语言10秒 空间限制:C/C ...

  9. 2018 ACM ICPC 南京赛区 酱油记

    Day 1: 早上6点起床打车去车站,似乎好久没有这么早起床过了,困到不行,在火车上睡啊睡就睡到了南京.南航离南京南站很近,地铁一站就到了,在学校里看到了体验坐直升机的活动,感觉很强.报道完之后去吃了 ...

随机推荐

  1. online QRcode generator , QRcode=== (Quick Response Code) , 二维条码,二维码,彩色二维码,图片二维码,

    online QRcode generator ,  QRcode=== (Quick Response Code)    , 二维条码,二维码,彩色二维码,图片二维码, 1 http://cli.i ...

  2. TypeScript 1.7 & TypeScript 1.8

    TypeScript 1.7 & TypeScript 1.8 1 1 https://zh.wikipedia.org/wiki/TypeScript TypeScript是一种由微软开发的 ...

  3. js 快速排序 All In One

    js 快速排序 All In One 快速排序 / Quick Sort "use strict"; /** * * @author xgqfrms * @license MIT ...

  4. Full Stack Web Development

    Full Stack Web Development Web Stacks MEAN (Mongo, Express, Angular and Node) LAMP (Linux, Apache, M ...

  5. GitHub user language statistics

    GitHub user language statistics 2020 https://madnight.github.io/githut/#/pull_requests/2020/2 2011 ~ ...

  6. 官网GitLab CI/CD英文文档翻译

    在查阅GitLab官网的CI/CD功能说明时,全是英文看起来不方便,通过翻译软件自动翻译后"内容失真",看起来很变扭.查阅了百度上的资料发现很多翻译很老旧,有些甚至是挂羊头卖狗肉. ...

  7. react性能提升

    1.把.bind(this)提升到constructor里面 2.在生命周期函数里面shouldComponentupdate里面做父组件改变重新渲染以致于子组件重新渲染的禁止 3.在setstate ...

  8. net里面using的使用

    起初using就明白一个作用  那就是引用命名空间.当面试官听到我回答这个问题的时候,马上就还问我,还有什么作用?我就只能摇头了,今天在网上看了下using的作用. 1.using指令.using + ...

  9. Java之HTTP网络编程(一):TCP/SSL网页下载

    目录 一.简介:HTTP程序设计 1.HTTP系统设计 2.HTTP客户端工作过程 3.HTTP服务端工作过程 二.基于TCP Socket的HTTP网页下载 三.基于SSL Socket的HTTPS ...

  10. 一次 MySQL 线上死锁分析实战

    关键词:MySQL Index Merge 前言 MySQL 的锁机制相信大家在学习 MySQL 的时候都有简单的了解过,那既然有锁就必定绕不开死锁这个问题.其实 MySQL 在大部分场景下是不会存在 ...