题目链接: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

代码:

int n;
struct node{
ll x,y,id;
bool operator<(const node a) const {
return x+abs(y)<a.x-abs(a.y); //排序方法
}
};
bool dis(ll x1,ll y1,ll a,ll b)
{
return (x1-a)*(x1-a)+(y1-b)*(y1-b)<y1*y1;
}
set<node>s;
void run()
{
scanf("%d",&n);
ll t,a,b;
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&t,&a,&b);
if(t==1)
{
node Node;
Node.x=a;
Node.y=b;
Node.id=i;
s.insert(Node);
}
else{
node tmp;
bool flag=0;
tmp.x=a;
tmp.y=0;
set<node>::iterator it=s.lower_bound(tmp);
while(it!=s.end())
{
ll x1=it->x,y1=it->y; if(dis(x1,y1,a,b))
{
flag=true;
printf("%lld\n",it->id);
s.erase(it);
break;
}
if(it->x-a>abs(it->y)) break;//关键2;
it++;
}
if(!flag) puts("-1");
}
}
}
signed main()
{
// int t=rd();
// while(t--)
run();
return 0;
}

线段树的方法:

#include<iostream>
#include<deque>
#include<memory.h>
#include<stdio.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
#include<stack>
#include<queue>
#include<bitset>
#include<set>
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long int ll;
const int MAXN=200010; ll X[MAXN];
ll Y[MAXN];
int tot;
vector<int> V[MAXN*30];
int ls[MAXN*30];
int rs[MAXN*30];
int ans; bool check(ll x1,ll y1,ll x2,ll y2){
if((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)<y1*y1)
return 1;
return 0;
} void update(int L,int R,int C,int l,int r,int &rt){ if(!rt)
rt=++tot;
if(L<=l&&r<=R)
{
V[rt].push_back(C);
return;
}
int m=(l+r)>>1;
if(L<=m)
update(L,R,C,l,m,ls[rt]);
if(R>m)
update(L,R,C,m+1,r,rs[rt]);
} void update(int L,int R,int l,int r,int rt){
if(L<=l&&r<=R){
vector<int> tmp;
for(auto &t:V[rt]){
if(t!=ans){
tmp.push_back(t);
}
}
V[rt]=tmp;
return;
}
int m=(l+r)>>1;
if(L<=m)
update(L,R,l,m,ls[rt]);
if(R>m)
update(L,R,m+1,r,rs[rt]);
} void query(int L,int R,int l,int r,int rt){
if(!rt)
return;
for(auto &t:V[rt]){
if(check(X[t],Y[t],L,R)){
ans=t;
return;
}
}
if(l==r)
return;
int m=(l+r)>>1;
if(L<=m)
query(L,R,l,m,ls[rt]);
else
query(L,R,m+1,r,rs[rt]);
} int main(){ int N;
scanf("%d",&N);
int rt=0;
for(int i=1;i<=N;i++){ int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op==1){
X[i]=x;
Y[i]=y;
update(x-y,x+y,i,-INF,INF,rt);
}
else{
ans=-1;
query(x,y,-INF,INF,rt);
printf("%d\n",ans);
if(ans!=-1)
update(X[ans]-Y[ans],X[ans]+Y[ans],-INF,INF,rt);
}
} return 0;
}

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

    Ubuntu 16.04: 由于是PY交易, 实际上是安装wxPython: pip install -U \ -f https://extras.wxpython.org/wxPython4/ext ...

  2. 读写 LED 作业 台灯的 频闪研究 2 评测&对比!

    0. 读写 LED 作业 台灯的 频闪研究 2 评测&对比! 评测&对比图:  1. 日光:(中午12点) 2. Philips: (天猫 15元 5w E27白) 3. FSL: ( ...

  3. CVS、SVN、Git、GitHub :版本控制系统

    1 1 1 Git常用命令 1 1 1 1 1 1 https://www.codecademy.com/learn/learn-git Learn Git You have now been int ...

  4. 最新 Apple iPhone 12 价格 All In One

    最新 Apple iPhone 12 价格 All In One 美版价格 Apple iPhone 12 mini $699 Apple iPhone 12 $799 Apple iPhone 12 ...

  5. how to using Linux pipe command output another command's help content to a file

    how to using Linux pipe command output another command's help content to a file Linux tee > >& ...

  6. live chat for website UX

    live chat for website UX increase customer satisfaction using a live chat https://crisp.chat/en/live ...

  7. React Native hot reloading & Android & iOS

    React Native hot reloading & Android & iOS https://facebook.github.io/react-native/docs/debu ...

  8. CORS OPTIONS

    CORS OPTIONS A CORS preflight request is a CORS request that checks to see if the CORS protocol is u ...

  9. Nice!JavaScript基础语法知识都在这儿了

    好好学习,天天向上 本文已收录至我的Github仓库DayDayUP:github.com/RobodLee/DayDayUP,欢迎Star 转载请注明出处! 链接:https://blog.csdn ...

  10. 那些容易犯错的c++保留字

    本文首发 | 公众号:lunvey 目前正在学习vc++6.0开发,而这里面使用的是c++98标准. 保留字,也称关键字,是指在变量.函数.类中不得重新声明的名称. c++98中大致有48个保留字,这 ...