Yuna's confusion

Problem Description

After yuna studies the STL container,she finds the STL powerful for AC.For the k-th number, she is also very familiar with it. Now yuna meets a very similar problem, yuna wants to design a container, the container is to support the three operations,including add,del and query.
Although yuna is very intelligent, she can not think of how to do it, can you help her to solve this problem?

Input

Input contains multiple test cases.,Each test case the first number is an integer n (1 <= n <100000), means that the number of operation to do. The next m lines, each line will be an string at the beginning, s which has three values:
If s is “Add”, then there will be an integer i (0 <i <100000), means press element i into Container.
If s is “Del”, then there will be an integer i (0 <i <100000), indicated that delete the element i from the container
If s is “Query”, then there will be two integers x and k (0 <x <100000, 0 <k <10000),means the inquiries, the element is greater than x, and the k-th larger number.

Output

For each deletion, if you want to delete the element which does not exist, the output "No Elment!". For each query, output the suitable answers in line .if the number does not exist, the output "Not Find!".

Sample Input

5
Add 5
Del 2
Add 6
Query 3 2
Query 8 1
7
Add 2
Add 2
Add 4
Query 1 1
Query 1 2
Query 1 3
Query 1 4
5
Add 5
Add 5
Del 5
Del 5
Query 1 1

Sample Output

No Elment!
6
Not Find!
2
2
4
Not Find!
Not Find!

Author

Rexdf

Source

developing schools contest 5

传说中的水题,正解是用线段树做的,不过用简单的STL水过了.......

谁说vector不可以排序,只要用的好一样能排

 

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;

vector<int> v;

int main()
{
    int n;
    char job[20];
    int x;
    int y;
    while(scanf("%d%*c",&n)!=EOF)
    {
        v.clear();
        while(n--)
        {
            vector<int>::iterator it;
            scanf("%s",job);
            if(strcmp(job,"Add")==0)
            {
                scanf("%d%*c",&x);
                it=upper_bound(v.begin(),v.end(),x);//这个地方对vector进行排序,因为每次插入的时候是不会影响原来的顺序所以,插入之后还是有序的
                v.insert(it,x);
            }
            else if(strcmp(job,"Del")==0)
            {
                scanf("%d%*c",&x);
                it=find(v.begin(),v.end(),x);
                if(it==v.end())
                {
                    printf("No Elment!\n");
                }
                else
                {
                    v.erase(it);//这个地方删除的是一个元素,并不是所有的元素
                }
            }
            else
            {
                scanf("%d%d",&x,&y);
                it=upper_bound(v.begin(),v.end(),x);
                if(it==v.end())
                {
                    printf("Not Find!\n");
                }
                else
                {
                    it+=(y-1);
                    if(it>=v.end())
                    {
                        printf("Not Find!\n");
                    }
                    else
                        printf("%d\n",*it);
                }
            }
        }
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值