QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#805046 | #1458. Binary Search Algorithm | Rong7 | RE | 1ms | 4044kb | C++14 | 3.8kb | 2024-12-08 12:18:27 | 2024-12-08 12:18:27 |
Judging History
answer
// Go in my style.
// Not afraid to dark.
#include <bits/stdc++.h>
using namespace std;
clock_t sttime;
#define STCLOCK sttime = clock ();
#define TIMENOW fprintf (stderr, "\nNOW TIME COSSEMED: %0.4lf\n", 1.0 * (clock () - sttime) / CLOCKS_PER_SEC);
#define inline __inline__ __attribute__ ((always_inline))
#define FS fflush (stdout)
namespace io {
int read_pos, read_dt; char read_char;
inline int read (int &p = read_pos){
p = 0, read_dt = 1; read_char = getchar ();
while (! isdigit (read_char)){
if (read_char == '-')
read_dt = - 1;
read_char = getchar ();
}
while (isdigit (read_char)){
p = (p << 1) + (p << 3) + read_char - 48;
read_char = getchar ();
}
return p = p * read_dt;
}
int write_sta[65], write_top;
inline void write (int x){
if (x < 0)
putchar ('-'), x = - x;
write_top = 0;
do
write_sta[write_top ++] = x % 10, x /= 10;
while (x);
while (write_top)
putchar (write_sta[-- write_top] + 48);
}
inline char next_blank (){
static char c; c = getchar ();
while (c < 'a' || c > 'z') c = getchar ();
return c;
}
}
const int N = 8e3;
int n;
namespace HEAP {
#define lson(p) ((p) << 1)
#define rson(p) ((p) << 1 | 1)
const int P = N * 4;
int tot, rp[P], mins[P], bel[P];
struct que {
vector < int > q, pos; unordered_map < int , int > to;
inline void push (int x){ x <= tot ? (q.push_back (rp[x]), pos.push_back (x)) : void (); }
inline void query (){
sort (q.begin (), q.end ());
q.erase (unique (q.begin (), q.end ()), q.end ());
io::write ((int) q.size ());
for (int x : q) putchar (' '), io::write (x);
putchar ('\n'); FS;
for (int i = 0;i < (int) q.size ();++ i)
io::read (q[i]), to[q[i]] = i;
}
inline void clear (){ q.clear (), to.clear (), pos.clear (); }
que (){ clear (); }
};
inline void upd (int x){
if (! tot) return puts ("0"), FS, void ();
static que Q; Q.clear ();
static int u;
Q.push (x);
for (u = x;u <= tot;){
Q.push (lson (u)); Q.push (rson (u));
u = mins[u];
}
for (u = x;u >> 1;){
Q.push (u >> 1); Q.push (u ^ 1);
u >>= 1;
}
Q.query (); vector < int > l;
for (u = x;mins[u] <= tot && Q.to[rp[mins[u]]] < Q.to[rp[u]];u = mins[u])
swap (rp[mins[u]], rp[u]), l.push_back (u);
for (u = x;(u >> 1) && Q.to[rp[u >> 1]] > Q.to[rp[u]];u >>= 1)
swap (rp[u >> 1], rp[u]), l.push_back (u);
for (int u : l)
if (rson (u) > tot) mins[u] = lson (u);
else mins[u] = Q.to[rp[lson (u)]] < Q.to[rp[rson (u)]] ? lson (u) : rson (u);
for (int u : Q.pos) bel[rp[u]] = u;
}
} using namespace HEAP;
signed main (){
STCLOCK
io::read (n);
for (int T = n << 1, x;T --;){
if (io::next_blank () == 'a'){
rp[++ tot] = io::read (x); bel[x] = tot;
mins[tot] = lson (tot); upd (tot);
} else {
io::read (x);
// printf ("%d %d %d\n", rp[bel[x]], bel[x], x);
if (rp[bel[x]] != x) exit (1);
swap (rp[bel[x]], rp[tot]);
if ((tot & 1) && (tot >> 1)) mins[tot >> 1] = tot ^ 1;
-- tot; upd (bel[x]);
}
io::write (tot ? rp[1] : - 1), putchar ('\n'); FS;
// printf ("%d %d %d\n", rp[1], rp[2], tot);
}
TIMENOW
return 0;
}
/*
3
add 1
1
add 3
3 1
delete 1
3
add 2
3 2
delete 3
2
delete 2
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 4044kb
input:
3 add 1 1 add 3 3 1 delete 1 3 add 2 3 2 delete 3 2 delete 2
output:
1 1 1 2 1 3 3 1 3 3 2 2 3 3 1 2 2 0 -1
result:
ok n=3, OK
Test #2:
score: -100
Runtime Error
input:
10 add 9 9 add 4 9 4 add 6 9 4 6 delete 9 4 6 add 7 7 4 6 delete 4 7 6 add 5 7 5 6 add 8 7 5 8 6 add 10 7 10 5 8 6 delete 8 7 10 5 6 add 3 3 7 5 8 6 add 2 3 7 5 2 delete 6 3 7 5 8 2 delete 10
output:
1 9 9 2 4 9 9 3 4 6 9 9 2 4 6 4 3 4 6 7 7 2 6 7 7 3 5 6 7 7 4 5 6 7 8 7 5 5 6 7 8 10 7 4 5 6 7 10 7 5 3 5 6 7 8 3 4 2 3 5 7 3 5 2 3 5 7 8 3