SPOJ ADALIST Ada and List(双端队列)

Ada the Ladybug has a TODO-list (containing only numbers - for simplicity). She is still doing something, so she sometimes erases kth number, sometimes she inserts something on kth position and sometime she asks for kth number.

Sadly, she is now searching for a work at position k so she doesn't have time to do this herself. Can you help her?

Input

The first line will contain 0 < N ≤ 105,0 < Q < 5*105, the number of elements in TODO-list and number of queries.

Then a line with N numbers follows. Each number 0 ≤ Ak ≤ 109 means kth number in her TODO-list.

Afterward, Q lines follow, each beginning with number 1 ≤ a ≤ 3

k x means that you will add number x to position k

k means that you will erase number from position k

k means that you will print number from position k

For all queries, it is true that 1 ≤ k ≤ #SizeOfList0 ≤ x ≤ 109 (for query1, it can be also put to position #SizeOfList + 1)

You will never get query of type or if the list is empty

Output

For each query of type 3, print kth numbers

Example Input

6 10
1 2 4 8 16 32
3 4
1 1 7
3 2
2 2
2 2
3 2
1 6 666
3 6
2 1
3 1

Example Output

8
1
4
666
4

Queries explanations

1 2 4 8 16 32
7 1 2 4 8 16 32
7 1 2 4 8 16 32
7 2 4 8 16 32
7 4 8 16 32
7 4 8 16 32
7 4 8 16 32 666
7 4 8 16 32 666
4 8 16 32 666
4 8 16 32 666
之前对STL里的东西都不是很懂,然后昨天做题的时候又重新看了一下好像似乎懂了很多。首先这道题要实现插入删除输出,并且插入删除输出的位置不确定所以这时候就应该想到双端队列。双端队列可以实现位置的输入输出还有位置的查询。。
 Deque成员函数

函数
描述
c.assign(beg,end)
c.assign(n,elem)
将[beg; end)区间中的数据赋值给c。
将n个elem的拷贝赋值给c。
c.at(idx)
传回索引idx所指的数据,如果idx越界,抛出out_of_range。
c.back()
传回最后一个数据,不检查这个数据是否存在。
c.begin()
传回迭代器重的可一个数据。
c.clear()
移除容器中所有数据。
deque<Elem> c
deque<Elem> c1(c2)
Deque<Elem> c(n)
Deque<Elem> c(n, elem)
Deque<Elem> c(beg,end)
c.~deque<Elem>()
创建一个空的deque。
复制一个deque。
创建一个deque,含有n个数据,数据均已缺省构造产生。
创建一个含有n个elem拷贝的deque。
创建一个以[beg;end)区间的deque。
销毁所有数据,释放内存。
c.empty()
判断容器是否为空。
c.end()
指向迭代器中的最后一个数据地址。
c.erase(pos)
c.erase(beg,end)
删除pos位置的数据,传回下一个数据的位置。
删除[beg,end)区间的数据,传回下一个数据的位置。
c.front()
传回地一个数据。
get_allocator
使用构造函数返回一个拷贝。
c.insert(pos,elem)
c.insert(pos,n,elem)
c.insert(pos,beg,end)
在pos位置插入一个elem拷贝,传回新数据位置。
在pos位置插入>n个elem数据。无返回值。
在pos位置插入在[beg,end)区间的数据。无返回值。
c.max_size()
返回容器中最大数据的数量。
c.pop_back()
删除最后一个数据。
c.pop_front()
删除头部数据。
c.push_back(elem)
在尾部加入一个数据。
c.push_front(elem)
在头部插入一个数据。
c.rbegin()
传回一个逆向队列的第一个数据。
c.rend()
传回一个逆向队列的最后一个数据的下一个位置。
c.resize(num)
重新指定队列的长度。
c.size()
返回容器中实际数据的个数。
C1.swap(c2)
Swap(c1,c2)
将c1和c2元素互换。
同上操作。

  Deque操作

函数
描述
operator[]
返回容器中指定位置的一个引用。
其次deque和vector相似。
所以代码实现:
#include <iostream>
#include<stdio.h>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<map>
#include<vector>
#include<deque>
#define ll long long
#define inf 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
char a[1001];
std::deque<int>p;
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0; i<n; i++)
    {
        int x;
        scanf("%d",&x);
        p.push_back(x);
    }
    while(m--)
    {
        int y,k,xx;
        scanf("%d%d",&y,&k);
        if(y==1)
        {
            scanf("%d",&xx);
            p.insert(p.begin()+k-1,xx);
        }
        if(y==2)
        {

            p.erase(p.begin()+k-1);
        }
        if(y==3)
        {

            printf("%d\n",p.at(k-1));
        }
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值