小奇画画

题:红莲清泪两行欲吐半点却无
如初是你杳然若绯雾还在水榭畔画楼处
是谁衣白衫如初谁红裳如故
——《忆红莲》
小奇想画几朵红莲,可惜它刚开始学画画,只能从画圆开始。小奇画了n个圆,它们的圆心都在x轴上,且两两不相交(可以相切)。现在小奇想知道,它画的圆把画纸分割成了多少块?(假设画纸无限大)

思路:做法五花八门,我补一发线段树+离散化的做法,如果有数据可以hack掉,欢迎交流学习

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=6e5+6;
const int N=8e5+5;
struct node{
    int x,y;
}a[maxn];
int c[maxn];
int rc[maxn];
bool cnp(node a,node b) {
    return (a.y-a.x)<(b.y-b.x);
}
int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}
struct nod{
    LL sum;
    LL mark;
}t[N<<2];
void build(int id,int l,int r){
    t[id].sum=0;
    t[id].mark=0;
    if(l==r){
        t[id].sum=0;
        return ;
    }
    int m=(l+r)>>1;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
    t[id].sum=t[id*2].sum+t[id*2+1].sum;
}
void update(int id,int l,int r,int val,int L,int R){
    if(l<=L&&R<=r){
        if(!t[id].mark){
            t[id].mark=val;
            t[id].sum=val*(R-L+1);
        }
        return ;
    }
    if(l>r)return ;
    int mid=(L+R)>>1;
    if(mid>=r){
        update(id*2,l,r,val,L,mid);
    }
    else if(mid<l){
        update(id*2+1,l,r,val,mid+1,R);
    }
    else{
        update(id*2,l,mid,val,L,mid);
        update(id*2+1,mid+1,r,val,mid+1,R);
    }
    t[id].sum=t[id<<1].sum+t[id<<1|1].sum;
}
LL query(int id,int l,int r,int L,int R){
    if(l<=L&&R<=r){
        return t[id].sum/*+t[id].mark*(LL)(r-l+1)*/;
    }
    if(l>r)return 0;
    if(t[id].mark!=0){
        t[id*2].mark+=t[id].mark;
        t[id*2+1].mark+=t[id].mark;
        t[id].sum+=(LL)(R-L+1)*t[id].mark;
        t[id].mark=0;
    }
    int mid=(L+R)>>1;
    LL ans=0;
    if(mid>=r){
        ans=query(id*2,l,r,L,mid);
    }
    else if(mid<l){
        ans=query(id*2+1,l,r,mid+1,R);
    }
    else{
        ans=query(id*2,l,mid,L,mid)+query(id*2+1,mid+1,r,mid+1,R);
    }
    return ans;
}

int main() {
    int n;
    n=read();
    map<int,int> ma;
    int cnt=0;
    for(int i=1;i<=n;i++) {
        int aa,bb;
        aa=read();
        bb=read();
        a[i].x=aa-bb;
        a[i].y=aa+bb;
    }
    sort(a+1,a+1+n,cnp);
    int nnum=0;
    for(int i=1;i<=n;i++) {
        c[++nnum]=a[i].x;
        c[++nnum]=a[i].y;
    }
    sort(c+1,c+1+nnum);
    int m=unique(c+1,c+nnum+1)-c;
    int tt=m;
    int ans=1;
    build(1,1,m);
    for(int i=1;i<=n;i++) {
        int y=lower_bound(c+1,c+m+1,a[i].y)-c;
        int x=lower_bound(c+1,c+m+1,a[i].x)-c;
        int test=query(1,x,y-1,1,m);
        if(y-x==test) ans+=2;
        else ans+=1;
        update(1,x,y-1,1,1,m);
        //cout<<y-x<<" "<<test<<endl;
    }
    printf("%d\n",ans);
}
/*
5
1 1
3 1
2 2
4 4
6 2


4
7 5
-9 11 11 9
0 20
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值