Bubble Cup 11 - Finals [Online Mirror, Div. 2] A. AI robots

地址:http://codeforces.com/contest/1046/problem/A

转载:https://blog.csdn.net/Lee_w_j__/article/details/82823266

来考虑如何知道两个机器人是否能互相看见。
我们可以先按从大到小对机器人进行排序,然后用一个线段树记录第 i 个节点是否有机器人,这样我们就可以通过线段树的区间查询知道,对于第 i 个机器人,它可以看到多少个机器人。
由于我们是动态加入机器人的,同时之前加入的机器人的 r 是要大于等于当前机器人的 r 的,所以当前机器人所能看到的机器人肯定也能看到当前的机器人。
接下来我们再来考虑智商的约束条件,常规做法是线段树再套一个线段树,仍旧是通过区间查询查出合适的机器人,但由于本题的内存只有256MB,树套树的空间接受不了,所以无法用树套树解决。
解决本题有一个关键,就是k<=20,这样每次我们只需要暴力遍历40次即可,由于空间的限制原因,所以我们可以通过线段树动态开点来维护智商为 qi 的机器人所在的位置,由于总的只有1e5个机器人,所以所耗费的空间会比树套树小很多。这样就可以解决这个题了。
相当于每个iq建立一个线段树,用动态开点来压缩空间

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int N = 1e9;
const int MAXN = 1e5 + 7;

int root[MAXN * 40],ls[MAXN * 40],lt[MAXN * 40];
LL sum[MAXN * 40];
int n,k,cnt;
vector<int>ve;

typedef struct Node{
    int x,r,q;
    friend bool operator < (const Node &a,const Node &b)
    {
        return a.r > b.r;
    }
}Node;
Node node[MAXN];

int getid(int x)
{
    return lower_bound(ve.begin(),ve.end(),x) - ve.begin() + 1;
}

void update(int p,int l,int r,int &rt)
{
    if(!rt) rt = ++cnt;
    if(l == r){
        sum[rt] += 1;
        return ;
    }
    int mid = (l + r) / 2;
    if(p <= mid) update(p,l,mid,ls[rt]);
    else update(p,mid + 1,r,lt[rt]);
    sum[rt] += 1;
}

LL query(int L,int R,int l,int r,int rt)
{
    if(!rt) return 0;
    if(L <= l && R >= r) return sum[rt];
    LL ans = 0;
    int mid = (l + r) / 2;
    if(L <= mid) ans += query(L,R,l,mid,ls[rt]);
    if(R > mid) ans += query(L,R,mid + 1,r,lt[rt]);
    return ans;
}

int main()
{
    while(~scanf("%d %d",&n,&k))
    {
        ve.clear();
        memset(root,0,sizeof(root));
        memset(ls,0,sizeof(ls));
        memset(lt,0,sizeof(lt));
        memset(sum,0,sizeof(sum));
        cnt = 0;
        for(int i = 0;i < n;++i)
        {
            scanf("%d %d %d",&node[i].x,&node[i].r,&node[i].q);
            ve.push_back(node[i].q);
            ve.push_back(node[i].q - k);
            ve.push_back(node[i].q + k);
        }
        sort(node,node + n);
        sort(ve.begin(),ve.end());
        ve.erase(unique(ve.begin(),ve.end()),ve.end());
        LL ans = 0;
        for(int i = 0;i < n;++i)
        {
            int st = getid(node[i].q - k),sk = getid(node[i].q + k);
            for(int j = st;j <= sk;++j){
                if(!root[j]) continue;
                int l = max(0,node[i].x - node[i].r);
                int r = min(N,node[i].x + node[i].r);
                ans += query(l,r,0,N,root[j]);
            }
            int id = getid(node[i].q);
            update(node[i].x,0,N,root[id]);
        }
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值