-
Notifications
You must be signed in to change notification settings - Fork 4
/
847D (Treap).cpp
163 lines (139 loc) · 3.42 KB
/
847D (Treap).cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// http://codeforces.com/contest/847/problem/D
// Time complexity: O(n * log(n))
// Space complexity: O(n)
#include <cstdio>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef struct Node *pnode;
struct Node {
int key, prior, sub, freq; // sub = subtree, freq = count of curr element
pnode l, r;
Node(int key) : key(key), prior(rand()), sub(1), freq(1), l(nullptr), r(nullptr) {}
};
struct Treap {
private:
pnode root;
void clear(pnode t) {
if (t) {
clear(t->l);
clear(t->r);
delete t;
}
}
int sub(pnode t) {
return t ? t->sub : 0;
}
void upd(pnode t) {
if (t) {
t->sub = t->freq + sub(t->l) + sub(t->r);
}
}
void split(pnode t, int key, pnode &l, pnode &r) {
if (!t) {
l = r = nullptr;
} else if (key < t->key) {
split(t->l, key, l, t->l), r = t;
} else {
split(t->r, key, t->r, r), l = t;
}
upd(t);
}
void merge(pnode &t, pnode l, pnode r) {
if (!l || !r) {
t = l ? l : r;
} else if (l->prior > r->prior) {
merge(l->r, l->r, r), t = l;
} else {
merge(r->l, l, r->l), t = r;
}
upd(t);
}
void insert(pnode &t, pnode it) {
if (!t) {
t = it;
} else if (t->prior > it->prior) {
insert(t->key <= it->key ? t->r : t->l, it);
} else {
split(t, it->key, it->l, it->r), t = it;
}
upd(t);
}
pnode find(pnode t, int key) {
if (!t) {
return nullptr;
}
if (t->key == key) {
return t;
}
if (key < t->key) {
return find(t->l, key);
} else {
return find(t->r, key);
}
}
void updPath(pnode t, int key) {
if (!t) {
return;
}
if (t->key == key) {}
else if (key < t->key) {
updPath(t->l, key);
} else {
updPath(t->r, key);
}
upd(t);
}
int less(pnode t, int key) {
int le = 0;
while (t) {
if (t->key < key) {
le += t->freq + sub(t->l);
t = t->r;
} else if (t->key == key) {
return le + sub(t->l);
} else {
t = t->l;
}
}
return le;
}
public:
Treap() : root(nullptr) {
srand(time(nullptr));
}
~Treap() {
clear(this->root);
}
void smartInsert(int key) {
pnode found = find(this->root, key);
if (!found) {
insert(this->root, new Node(key));
} else {
++found->freq;
updPath(this->root, key);
}
}
int less(int key) {
return less(this->root, key);
}
};
int main() {
int n, T;
scanf("%d %d", &n, &T);
Treap t;
int b, res = 0; // b = bowlsOfFood
for (int i = 1; i <= n; ++i) {
scanf("%d", &b);
// we enter the time that the dog has to wait when it reaches the bowl of food
if (T <= i) {
// an alternative way to manage the never reachable bowls is
// to insert max(b-i , 0) in the Treap
break;
}
t.smartInsert(b - i);
int le = t.less(T - i);
res = le > res ? le : res;
}
return printf("%d\n", res), 0;
}