• hdu 2852 树状数组


    思路:加一个数e就用update(e,1)。删除元素e就用update(e,-1)。找比a大的第k大的元素就用二分查找。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define Maxn 120010
    #define lowbit(x) (x&(-x))
    using namespace std;
    int C[Maxn];
    int Sum(int pos)
    {
        int sum=0;
        while(pos)
        {
            sum+=C[pos];
            pos-=lowbit(pos);
        }
        return sum;
    }
    void update(int pos,int val)
    {
        while(pos<=120010)
        {
            C[pos]+=val;
            pos+=lowbit(pos);
        }
    }
    int main()
    {
        int m,i,j,p,k,e,num,a;
        while(scanf("%d",&m)!=EOF)
        {
            num=0;
            memset(C,0,sizeof(C));
            while(m--)
            {
                scanf("%d",&p);
                if(p==0)
                {
                    scanf("%d",&e);
                    update(e,1);
                    num++;
                }
                if(p==1)
                {
                    scanf("%d",&e);
                    if(Sum(e)-Sum(e-1))
                    {
                        update(e,-1);
                        num--;
                    }
                    else
                        printf("No Elment!
    ");
                }
                if(p==2)
                {
                    scanf("%d%d",&a,&k);
                    int l,r,mid,temp;
                    int mins;
                    mins=Sum(a);
                    l=a,r=120000;
                    if(!num||num-mins<k)
                    {
                        printf("Not Find!
    ");
                        continue;
                    }
                    while(l<r)
                    {
                        mid=(l+r)>>1;
                        temp=Sum(mid)-mins;
                        if(temp>=k)
                            r=mid;
                        else
                            l=mid+1;
                    }
                    if(r>a)
                        printf("%d
    ",r);
                    else
                        printf("Not Find!
    ");
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    LeetCode124 二叉树中的最大路径和
    LeetCode100 相同的树
    LeetCode206 反转链表
    LeetCode460 LFU缓存
    LeetCode876 链表的中间结点
    hdu2767 强连通分量
    hdu1827 强连通分量
    模板 tarjan算法
    hdu2227 树状数组优化dp
    割点和桥
  • 原文地址:https://www.cnblogs.com/wangfang20/p/3227022.html
Copyright © 2020-2023  润新知