Sdut 3252 Lowest Unique Price【思维+二分+树状数组】

来源:互联网 发布:mysql 自定义变量 编辑:程序博客网 时间:2024/06/04 17:49

Lowest Unique Price

Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic Discuss

Problem Description

Recently my buddies and I came across an idea! We want to build a website to sell things in a new way.

For each product, everyone could bid at a price, or cancel his previous bid, finally we sale the product to the one who offered the "lowest unique price". The lowest unique price is defined to be the lowest price that was called only once.

So we need a program to find the "lowest unique price", We'd like to write a program to process the customers' bids and answer the query of what's the current lowest unique price.

All what we need now is merely a programmer. We will give you an "Accepted" as long as you help us to write the program.

Input

The first line of input contains an integer T, indicating the number of test cases (T ≤ 60).

Each test case begins with a integer N (1 ≤ N ≤ 200000) indicating the number of operations.

Next N lines each represents an operation.

There are three kinds of operations:

"b x": x (1 ≤ x ≤ 10^6) is an integer, this means a customer bids at price x.

"c x": a customer has canceled his bid at price x.

"q" : means "Query". You should print the current lowest unique price.

Our customers are honest, they won\'t cancel the price they didn't bid at.

Output

 Please print the current lowest unique price for every query ("q"). Print "none" (without quotes) if there is no lowest unique price.

Example Input

2 3 b 2 b 2 q 12 b 2 b 2 b 3 b 3 q b 4 q c 4 c 3 q c 2 q

Example Output

none none 4 3 2

Hint

Author

“浪潮杯”山东省第六届ACM大学生程序设计竞赛

题目大意:

有三种操作:

①b x,表示有一个出价是x.

②c x,表示取消一个出价x.

③q,询问之前的所有没有被取消的出价中,只出现了一次的价格中,最小的出价.


思路:


对于三种操作:

①如果vis【x】++之后,使得vis【x】==1.也就是说这种出价次数为1.那么将x入树:add(x,1).

注意,如果在vis【x】++之前,vis【x】==1.那么将x从树中去除:add(x,-1);

②同理,如果vis【x】--之后,使得vis【x】==1.也就是说这种出价次数为1.那么将x入树:add(x,1).

注意,如果vis【x】--之后,vis【x】==0,那么将x从树中去除:add(x,-1);

③那么对于查询操作,我们只需要二分查询一个位子pos,使得这个pos是最小的位子,使得getsum(pos)==1.

如果不存在,输出none即可。


Ac代码:


#include<stdio.h>#include<string.h>using namespace std;int vis[1000500];int tree[1004005];//树int lowbit(int x)//lowbit{    return x&(-x);}int sum(int x){    int sum=0;    while(x>0)    {        sum+=tree[x];        x-=lowbit(x);    }    return sum;}void add(int x,int c){    while(x<=1000000)    {        tree[x]+=c;        x+=lowbit(x);    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int q;        memset(tree,0,sizeof(tree));        memset(vis,0,sizeof(vis));        scanf("%d",&q);        while(q--)        {            char s[10];            scanf("%s",s);            if(s[0]=='b')            {                int x;scanf("%d",&x);                if(vis[x]==1)add(x,-1);                vis[x]++;                if(vis[x]==1)add(x,1);            }            else if(s[0]=='c')            {                int x;scanf("%d",&x);                vis[x]--;                if(vis[x]==1)add(x,1);                if(vis[x]==0)add(x,-1);            }            else            {                int pos=-1;                int l=1;                int r=1000000;                while(r-l>=0)                {                    int mid=(l+r)/2;                    if(sum(mid)>=1)                    {                        pos=mid;                        r=mid-1;                    }                    else                    {                        l=mid+1;                    }                }                if(pos==-1)printf("none\n");                else                printf("%d\n",pos);            }        }    }}






0 0
原创粉丝点击