QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#805033 | #1458. Binary Search Algorithm | Rong7 | TL | 0ms | 0kb | C++14 | 3.5kb | 2024-12-08 12:05:18 | 2024-12-08 12:05:19 |
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 = lson (u) ^ rson (u) ^ mins[u];
}
for (u = x;u >> 1;){
Q.push (u >> 1); Q.push (u ^ lson (u >> 1) ^ rson (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); swap (rp[bel[x]], rp[tot]);
-- tot; upd (bel[x]);
}
io::write (tot ? rp[1] : - 1), putchar ('\n');
}
TIMENOW
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 add 1 1
output:
1 1