# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
544766 | rainboy | Queue (CEOI06_queue) | C11 | 75 ms | 5460 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <stdio.h>
#define N 50000
#define N_ (N * 3 + 1)
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int aa[N_];
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (aa[ii[j]] == aa[i_])
j++;
else if (aa[ii[j]] < aa[i_]) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int main() {
static int ii[N_], aa_[N_], ll[N_], rr[N_], pp[N_], qu[N_];
int n, n_, q, i, p, cnt;
scanf("%d", &n);
aa[0] = 1;
for (i = 0; i < n; i++) {
int a, b;
scanf("%d%d", &a, &b);
aa[i * 3 + 1] = a, aa[i * 3 + 2] = a + 1, aa[i * 3 + 3] = b;
}
for (i = 0; i <= n * 3; i++)
ii[i] = i;
sort(ii, 0, n * 3 + 1);
for (i = 0, n_ = 0; i <= n * 3; i++) {
int a = aa[ii[i]];
aa[ii[i]] = n_;
if (i + 1 == n * 3 || aa[ii[i + 1]] != a)
aa_[n_++] = a;
}
for (i = 0; i < n_; i++)
ll[i] = i - 1, rr[i] = i + 1;
for (i = 0; i < n; i++) {
int i_ = aa[i * 3 + 1], j_ = aa[i * 3 + 3], l = ll[i_], r = rr[i_];
if (l >= 0)
rr[l] = r;
if (r < n_)
ll[r] = l;
l = ll[j_];
rr[i_] = j_, ll[j_] = i_;
if (l >= 0)
rr[l] = i_;
ll[i_] = l;
}
i = 0;
while (ll[i] >= 0)
i++;
p = 1, cnt = 0;
while (i < n_) {
qu[cnt++] = i;
pp[i] = p;
p += aa_[i + 1] - aa_[i];
i = rr[i];
}
scanf("%d", &q);
while (q--) {
static char s[2];
int x, lower, upper;
scanf("%s%d", s, &x);
if (s[0] == 'P') {
lower = 0, upper = n_;
while (upper - lower > 1) {
i = (lower + upper) / 2;
if (aa_[i] <= x)
lower = i;
else
upper = i;
}
i = lower;
printf("%d\n", pp[i] + x - aa_[i]);
} else {
lower = 0, upper = n_;
while (upper - lower > 1) {
i = (lower + upper) / 2;
if (pp[qu[i]] <= x)
lower = i;
else
upper = i;
}
i = lower;
printf("%d\n", aa_[qu[i]] + x - pp[qu[i]]);
}
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |