-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1511C.cpp
More file actions
97 lines (80 loc) · 1.62 KB
/
1511C.cpp
File metadata and controls
97 lines (80 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Node
{
int data;
Node *next;
Node *prev;
Node(int d) : data(d), next(nullptr), prev(nullptr) {}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n);
vector<int> queries(q);
Node *head = nullptr;
Node *tail = nullptr;
for (int i = 0; i < n; i++)
{
int key;
cin >> key;
Node *node = new Node(key);
if (!head)
{
head = tail = node;
}
else
{
tail->next = node;
node->prev = tail;
tail = node;
}
}
for (int i = 0; i < q; i++)
{
cin >> queries[i];
}
for (int i = 0; i < q; i++)
{
int key = queries[i];
Node *temp = head;
int ind = 1;
while (temp && temp->data != key)
{
temp = temp->next;
ind++;
}
if (!temp)
continue;
queries[i] = ind;
if (temp == head)
continue;
if (temp->prev)
temp->prev->next = temp->next;
if (temp->next)
temp->next->prev = temp->prev;
if (temp == tail)
tail = temp->prev;
temp->prev = nullptr;
temp->next = head;
if (head)
head->prev = temp;
head = temp;
}
for (int i = 0; i < q; i++)
{
cout << queries[i] << ' ';
}
cout << '\n';
while (head)
{
Node *next = head->next;
delete head;
head = next;
}
return 0;
}