1.题目描述:
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
1 k x means that you will add number x to position k
2 k means that you will erase number from position k
3 k means that you will print number from position k
For all queries, it is true that 1 ≤ k ≤ #SizeOfList, 0 ≤ x ≤ 109 (for query 1, it can be also put to position #SizeOfList + 1)
You will never get query of type 2 or 3 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
最开始给你n个数,接下来有q次操作
1 k x 意味着把元素x插入到第k个位置
2 k 意味着删除第k个位置的数
3 k 意味着输出第k个位置的数
3.解题思路:
当时想的就是vector模拟,结果暴力20000+ms过了。。。赛后有大佬表示是个平衡树!!
还有一种优化的做法是用双端队列deque验证延时确实降到了10000ms
4.AC代码:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
vector<int> v;
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
int n, q;
while (~scanf("%d%d", &n, &q))
{
v.clear();
for (int i = 0; i < n; i++)
{
int x;
scanf("%d", &x);
v.push_back(x);
}
while (q--)
{
int dir, k, x;
scanf("%d%d", &dir, &k);
if (dir == 1)
{
scanf("%d", &x);
k--;
v.insert(v.begin() + k, x);
}
else if (dir == 2)
v.erase(v.begin() + --k);
else if (dir == 3)
printf("%d\n", v[--k]);
}
}
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return 0;
}