程序设计实验七

//万湖之国的形成
#include <iostream>
#include <algorithm>
using namespace std;
struct hole{
    double x;
    double y;
    double r;
};

hole all[100005];
int pre[100005];

bool cmp(hole a,hole b)//排序一下可以大大提升计算速度
{
    return a.x-a.r<b.x-b.r;
}

bool check(hole a,hole b)//检查两个坑是否相交
{
    double distance=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
    if(distance<(a.r+b.r)*(a.r+b.r))
        return true;
    return false;
}

int find(int x)     				//查找结点 x的根结点
{
    if(pre[x] == x) return x;		//递归出口:x的上级为 x本身,即 x为根结点
    return pre[x] = find(pre[x]);	//此代码相当于先找到根结点 rootx,然后pre[x]=rootx
}

int main()
{
    int n;
    scanf("%d",&n);
    int res=n;

    for(int i=1;i<=n;i++)
    {
        scanf("%lf%lf%lf",&all[i].x,&all[i].y,&all[i].r);
        pre[i]=i;
    }

    sort(all+1,all+1+n,cmp);//标记v数组没必要改 因为还是一个一个点

    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            if(all[j].x-all[j].r>=all[i].x+all[i].r)
                break;
            if(check(all[i],all[j]))
            {
                int pi=find(i);
                int pj=find(j);
                if(pi!=pj)
                {
                    res--;
                    pre[pi]=pj;
                }
            }
        }
    }
    printf("%d\n",res);
    return 0;
}

//繁忙的公路
#include<iostream>
#define lowbit(x) (x&(-x))
typedef long long ll;
using namespace std;
int c[2000006];
int n,m;
ll ans;

void add_dandian(int x,int k)
{
    for(int i=x;i<=n;i+=lowbit(i))
        c[i]+=k;
}

void search(int begin,int end)
{
    for(int i=end;i;i-=lowbit(i))
        ans+=c[i];
    for(int i=begin-1;i;i-=lowbit(i))
        ans-=c[i];
}

int main()
{
    scanf("%d\n%d\n",&n,&m);
    while(m--)
    {
        char choice;
        int x,y;
        scanf("%c %d %d\n",&choice,&x,&y);
        //printf("%c %d %d\n",choice,x,y);
        if(choice=='H')
        add_dandian(x,y);
        else if(choice=='Q')
        {
            ans=0;
            search(x,y);
            printf("%lld\n",ans);
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zero_019

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值