FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
competitive-programming/stack_queue/stack.cpp at master · harrypotter0/competitive-programming · GitHub
harrypotter0
competitive-programming
Repository navigation
Code
Issues
1
(1)
Pull requests
2
(2)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
competitive-programming
/
stack_queue
/
stack.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
239 lines (210 loc) · 7.16 KB
Breadcrumbs
competitive-programming
/
stack_queue
/
stack.cpp
Copy path
File metadata and controls
239 lines (210 loc) · 7.16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#
pragma
GCC optimize("O3", "unroll-loops")
//
God Help me !!
#
include
<
bits/stdc++.h
>
using
namespace
std
;
#
include
<
ext/pb_ds/assoc_container.hpp
>
#
include
<
ext/pb_ds/tree_policy.hpp
>
#
define
FILES
freopen
(
"
input.txt
"
,
"
r
"
, stdin); freopen(
"
output.txt
"
,
"
w
"
, stdout)
#
define
FAST
ios_base::sync_with_stdio
(
0
); cin.tie(
0
); cout.tie(
0
)
#
define
FIXED
cout << fixed << setprecision(
20
)
#
define
RANDOM
srand
(time(
nullptr
))
#
define
sz
(
a
) (
int
)a.size()
#
define
int
long
long
#
define
rep
(
i,a,b
)
for
(
int
i=(
int
)a;i<=(
int
)b;i++)
#
define
sep
(
i,a,b
)
for
(
int
i=(
int
)a;i>=(
int
)b;i--)
#
define
inf
0x3f3f3f3f
#
define
mp
make_pair
#
define
pb
push_back
#
define
eb
emplace_back
#
define
pf
push_front
#
define
popb
pop_back
#
define
popf
pop_front
#
define
lb
lower_bound
#
define
ub
upper_bound
#
define
all
(
a
) a.begin(),a.end()
#
define
ff
first
#
define
ss
second
#
define
endl
"
\n
"
#
define
n6
3000005
#
define
n3
3005
#
define
n5
300005
#
define
pi
pair<
int
,
int
>
#
define
pii
pair<
int
,pi>
template
<
typename
T>
using
ordered_set = __gnu_pbds::tree<T, __gnu_pbds::null_type, less<T>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;
//
find_by_order(), order_of_key()
/*
*****************************************************************************
*/
/*
*****************************************************************************
*/
#
define
dbg1
(
x
) cout << #x <<
"
:
"
<< x << endl;
#
define
dbg2
(
x, y
) cout << #x <<
"
:
"
<< x <<
"
|
"
<< #y <<
"
:
"
<< y << endl;
#
define
dbg3
(
x, y, z
) cout << #x <<
"
:
"
<< x <<
"
|
"
<< #y <<
"
:
"
<< y <<
"
|
"
<< #z <<
"
:
"
<< z << endl;
#
define
dbg4
(
a, b, c, d
) cout << #a <<
"
:
"
<< a <<
"
|
"
<< #b <<
"
:
"
<< b <<
"
|
"
<< #c <<
"
:
"
<< c <<
"
|
"
<< #d <<
"
:
"
<< d << endl;
#
define
dbg5
(
a, b, c, d, e
) cout << #a <<
"
:
"
<< a <<
"
|
"
<< #b <<
"
:
"
<< b <<
"
|
"
<< #c <<
"
:
"
<< c <<
"
|
"
<< #d <<
"
:
"
<< d <<
"
|
"
<< #e <<
"
:
"
<< e << endl;
#
define
dbg6
(
a, b, c, d, e, f
) cout << #a <<
"
:
"
<< a <<
"
|
"
<< #b <<
"
:
"
<< b <<
"
|
"
<< #c <<
"
:
"
<< c <<
"
|
"
<< #d <<
"
:
"
<< d <<
"
|
"
<< #e <<
"
:
"
<< e <<
"
|
"
<< #f <<
"
:
"
<< f << endl;
#
define
dbg
(...) fprintf(stderr, __VA_ARGS__)
#
define
dbgv
(
x
) cout << #x <<
"
=
"
<< x << endl
#
define
dbga
(
arr, len
) {cout << #arr <<
"
=
"
;
for
(
int
_ =
0
; _ < len; _++)cout << arr[_] <<
"
"
; cout << endl;}
#
define
dbgi
(
it
) {cout << #it <<
"
=
"
;
for
(
const
auto
& _ : it)cout << _ <<
"
"
; cout << endl;}
/*
*****************************************************************************
*/
/*
*****************************************************************************
*/
template
<
typename
T>
inline
void
ckmax
(T &x, T y) {
if
(y > x) x = y; }
template
<
typename
T>
inline
void
ckmin
(T &x, T y) {
if
(y < x) x = y; }
#
define
error
(
args...
) { string _s = #args;
replace
(_s.
begin
(), _s.
end
(),
'
,
'
,
'
'
); stringstream
_ss
(_s); istream_iterator<string>
_it
(_ss);
err
(_it, args); }
void
err
(istream_iterator<string> it) {}
template
<
typename
T,
typename
... Args>
void
err
(istream_iterator<string> it, T a, Args... args) {
cout << *it <<
"
=:
"
<< a << endl;
err
(++it, args...);
}
template
<
typename
T1
,
typename
T2
>
inline
std::ostream&
operator
<< (std::ostream& os,
const
std::pair<
T1
,
T2
>& buf) {
return
os <<
"
(
"
<< buf.
first
<<
"
:
"
<< buf.
second
<<
"
)
"
;
}
template
<
typename
T>
inline
std::ostream &
operator
<< (std::ostream & os,
const
std::vector<T>& v) {
bool
first =
true
;
os <<
"
[
"
;
for
(
unsigned
int
i =
0
; i < v.
size
(); i++) {
if
(!first) os <<
"
,
"
;
os << v[i];
first =
false
;
}
return
os <<
"
]
"
;
}
template
<
typename
T>
inline
std::ostream &
operator
<< (std::ostream & os,
const
std::set<T>& v) {
bool
first =
true
;
os <<
"
{
"
;
for
(
typename
std::set<T>::const_iterator ii = v.
begin
(); ii != v.
end
(); ++ii) {
if
(!first) os <<
"
,
"
;
os << *ii;
first =
false
;
}
return
os <<
"
}
"
;
}
template
<
typename
T1
,
typename
T2
>
inline
std::ostream &
operator
<< (std::ostream & os,
const
std::map<
T1
,
T2
>& v) {
bool
first =
true
;
os <<
"
{
"
;
for
(
typename
std::map<
T1
,
T2
>::const_iterator ii = v.
begin
(); ii != v.
end
(); ++ii) {
if
(!first) os <<
"
,
"
;
os << *ii ;
first =
false
;
}
return
os <<
"
}
"
;
}
template
<
typename
T>
inline
std::ostream &
operator
<< (std::ostream & os,
const
vector<vector<T>>& v) {
os <<
'
\n
'
;
for
(
auto
&u: v) os << u <<
'
\n
'
;
return
os;
}
const
int
MOD
=
1e9
+
7
;
const
long
long
INF
=
1e18
;
const
double
EPS
=
1e-6
;
/*
*****************************************************************************
*/
/*
*****************************************************************************
*/
inline
int
add
(
int
a,
int
b) {
a += b;
if
(a >=
MOD
) a -=
MOD
;
return
a;
}
inline
int
sub
(
int
a,
int
b) {
a -= b;
if
(a <
0
) a +=
MOD
;
return
a;
}
inline
int
mul
(
int
a,
int
b) {
return
(
long
long
) a * b %
MOD
;
}
inline
int
power
(
int
a,
long
long
b) {
int
res =
1
;
while
(b >
0
) {
if
(b &
1
) {
res =
mul
(res, a);
}
a =
mul
(a, a);
b >>=
1
;
}
return
res;
}
/*
*****************************************************************************
*/
/*
*****************************************************************************
*/
void
showdq
(deque <
int
> g)
{
for
(
auto
it = g.
begin
(); it != g.
end
(); ++it)
cout << *it <<
'
'
;
cout <<
'
\n
'
;
}
void
showstack
(stack <
int
> s)
{
while
(!s.
empty
()){
cout << s.
top
() <<
'
'
;
s.
pop
();
}
cout <<
'
\n
'
;
}
void
showqueue
(queue<
int
> q)
{
while
(!q.
empty
()){
cout << q.
front
() <<
'
'
;
q.
pop
();
}
cout <<
'
\n
'
;
}
int
solve_util
(deque<
int
> &q){
}
void
nextGreater
(
int
arr[],
int
n,
int
next[],
char
order)
{
stack<
int
> S;
for
(
int
i=n-
1
; i>=
0
; i--)
{
//
Keep removing top element from S while the top
//
element is smaller then or equal to arr[i] (if Key is G)
//
element is greater then or equal to arr[i] (if order is S)
while
(!S.
empty
() &&
((order==
'
G
'
)? arr[S.
top
()] <= arr[i]:
arr[S.
top
()] >= arr[i]))
S.
pop
();
//
store the next greater element of current element
if
(!S.
empty
())
next[i] = S.
top
();
//
If all elements in S were smaller than arr[i]
else
next[i] = -
1
;
//
Push this element
S.
push
(i);
}
}
void
nextSmallerOfNextGreater
(
int
arr[],
int
n)
{
int
NG
[n];
//
stores indexes of next greater elements
int
RS
[n];
//
stores indexes of right smaller elements
nextGreater
(arr, n,
NG
,
'
G
'
);
nextGreater
(arr, n,
RS
,
'
S
'
);
for
(
int
i=
0
; i< n; i++)
{
if
(
NG
[i] != -
1
&&
RS
[
NG
[i]] != -
1
)
cout << arr[
RS
[
NG
[i]]] <<
"
"
;
else
cout<<
"
-1
"
<<
"
"
;
}
}
int
l[n6], r[n6], arr[n6];
//
stack<int>st;
int
n;
void
solve
(){
cin >> n ;
rep
(i,
0
,n-
1
){
cin>>arr[i];
}
nextSmallerOfNextGreater
(arr, n);
}
signed
main
()
{
FAST
;
FIXED
;
RANDOM
;
int
t=
1
;
//
cin>>t;
//
time_t time_t1, time_t2;
//
time_t1 = clock();
while
(t--)
solve
();
//
time_t2 = clock();
//
cout << "time taken :" << time_t2 - time_t1 << endl;
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL