zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    nsmutableset
    数组建立 不可变数组 排序 遍历
    字符串截取 拼接 转换 长度 查询 比较
    字典排序
    数字字典结合
    可变字典
    字典
    可变字符串
    oc block排序
    oc中文首字母排序
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7086778.html
Copyright © 2011-2022 走看看