4418: [Shoi2013]扇形面积并|二分答案|树状数组

为何感觉SHOI的题好水。。。又是一道SB题
从左到右枚举每一个区间,遇到一个扇形的左区间就+1,遇到右区间就-1,然后再树状数组上2分答案,还是不会码 log 的。。SHOI2013似乎还有一道题发牌也是类似的维护方法。。

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define mod 999911659 //2 3 4679 35617
#define N 500001
using namespace std;
int sc()
{
    int i=0,f=1; char c=getchar();
    while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9')i=i*10+c-'0',c=getchar();
    return i*f;
}
struct W{int p,r,x;}a[N];
long long ans;
int n,tot,mx,k,m,tr[N];
bool cmp(W a,W b)
{
    return a.p<b.p;
}
void change(int x,int f)
{
    for(;x<=mx;x+=x&-x)tr[x]+=f;
}
ll ask(int x)
{
    ll ans=0;
    for(;x;x-=x&-x)ans+=tr[x];
    return ans;
}
int query()
{
    int l=1,r=mx,ans=0;
    while(l<=r)
    {
        int mid=l+r>>1;
        if(ask(mid)>=k)ans=mid,l=mid+1;
        else r=mid-1;
    }
    return ans;
}
int main()
{
    n=sc();m=sc(),k=sc();
    for(int i=1;i<=n;i++)
    {
        int x=sc(),l=sc(),r=sc();mx=max(mx,x);
        a[++tot].p=l,a[tot].r=x,a[tot].x=1;
        a[++tot].p=r,a[tot].r=x,a[tot].x=-1;
        if(l>r)
            a[++tot].p=-m,a[tot].r=x,a[tot].x=1;
    }
    sort(a+1,a+tot+1,cmp);
    int now=1;
    for(int i=-m;i<m;i++)
    {
        while(now<=tot&&a[now].p<=i)
            change(1,a[now].x),change(a[now].r+1,-a[now].x),now++;
        ll res=query(); 
        ans+=res*res;
    }
    cout<<ans;
    return 0;
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值