QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#121786 | #2559. Endless Road | zhaohaikun | TL | 0ms | 0kb | C++14 | 7.7kb | 2023-07-08 20:42:54 | 2023-07-08 20:42:56 |
Judging History
answer
#include <bits/stdc++.h>
#define ls num << 1
#define rs ls | 1
#define li ls, l, mid
#define ri rs, mid + 1, r
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> void chkmax(T& x, T y) { x = max(x, y); }
template <typename T> void chkmin(T& x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
x *= f;
}
const int N = 5e5 + 10;
pair <int, int> a[N], b[N];
int id[N], seg1[N << 2], t[N], seg2[N << 2], ss[N << 2], tmp[N], tot, tag[N << 2], maxn[N << 2];
void add(int x, int y) {
// cout << x << " " << y << endl;
for (; x <= tot; x += x & -x) t[x] += y;//, cout << x << " % " << t[x] << endl;
}
int query(int x) {
int ans = 0;
for (; x; x -= x & -x) ans += t[x];
return ans;
}
void pushup1(int num) {
if (seg1[ls] && (!seg1[rs] || a[seg1[ls]].second < a[seg1[rs]].second)) seg1[num] = seg1[ls];
else seg1[num] = seg1[rs];
// cout << "# " << num << " " << seg1[num] << " " << seg1[ls] << " " << seg1[rs] << endl;
// seg1[num] = min(seg1[ls], seg1[rs]);
}
priority_queue <pair <int, int>, vector <pair <int, int>>, greater <pair <int, int>>> tt[N << 2];
void modify1(int num, int l, int r, int x) {
if (l == r) {
tt[num].emplace(a[x].second, x);
// cout << "* " << num << " " << l << " " << tt[num].top().second << endl;
seg1[num] = tt[num].top().second;
return;
} int mid = (l + r) >> 1;
if (mid >= a[x].first) modify1(li, x);
else modify1(ri, x);
pushup1(num);
}
void mmodify1(int num, int l, int r, int x) {
if (l == r) {
// cout << tt[num].top().second << " & " << x << endl;
assert(tt[num].top().second == x);
// cout << "!!!!!\n";
tt[num].pop();
seg1[num] = tt[num].size() ? tt[num].top().second : 0;
return;
} int mid = (l + r) >> 1;
if (mid >= a[x].first) mmodify1(li, x);
else mmodify1(ri, x);
pushup1(num);
}
int query1(int num, int l, int r, int x, int y) {
// cout << num << " " << l << " " << r << " " << seg1[num] << " " << a[seg1[num]].second << " " << x << " " << y << endl;
if (!seg1[num] || a[seg1[num]].second >= y) return -1;
if (l == r) return seg1[num];
int mid = (l + r) >> 1;
if (mid + 1 < x) {
int t = query1(ri, x, y);
if (~t) return t;
}
return query1(li, x, y);
}
void down(int num, int x) {
tag[num] += x;
seg2[num] += x;
}
void pushdown(int num) {
if (tag[num]) {
down(ls, tag[num]), down(rs, tag[num]);
tag[num] = 0;
}
}
void pushup2(int num) {
if ((seg2[ls] < seg2[rs]) || (seg2[ls] == seg2[rs] && id[ss[ls]] < id[ss[rs]])) {
seg2[num] = seg2[ls];
ss[num] = ss[ls];
} else {
seg2[num] = seg2[rs];
ss[num] = ss[rs];
}
maxn[num] = max(maxn[ls], maxn[rs]);
}
void modify2(int num, int l, int r, int x, int y) {
if (l == r) {
seg2[num] = y;
maxn[num] = a[x].second - 1;
if (y == 1e9) ss[num] = 0;
else ss[num] = x;
// if (x == 4) cout << "! " << seg2[num] << endl;
// cout << "% " << l << " " << x << endl;
// cout << "% " << l << " " << x << " " << y << endl;
return;
}
pushdown(num);
int mid = (l + r) >> 1;
if (mid >= a[x].first) modify2(li, x, y);
else modify2(ri, x, y);
pushup2(num);
}
void change(int num, int l, int r, int L, int R, int x) {
// cout << "$ " << L << " " << R << " " << l << " " << r << endl;
if (L <= l && r <= R) return down(num, x);
pushdown(num);
int mid = (l + r) >> 1;
if (mid >= L) change(li, L, R, x);
if (mid < R) change(ri, L, R, x);
pushup2(num);
}
int prv(int num, int l, int r, int x) {
if (!ss[num]) return -1;
if (l == r) return ss[num];
int mid = (l + r) >> 1;
if (mid + 1 < a[x].first) {
int t = prv(ri, x);
if (~t) return t;
}
return prv(li, x);
}
int nxt(int num, int l, int r, int x) {
if (!ss[num]) return -1;
if (l == r) return ss[num];
int mid = (l + r) >> 1;
if (mid > a[x].first) {
int t = nxt(li, x);
if (~t) return t;
}
return nxt(ri, x);
}
int query2(int num, int l, int r, int x) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (maxn[ls] >= x) return query2(li, x);
return query2(ri, x);
}
bool vis[N];
// int _;
void zhk() {
// _++;
tot = 0;
// ms(seg1, 0), ms(vis, false), ms(seg2, 0x3f), ms(t, 0), ms(tag, 0), ms(ss, 0), ms(maxn, 0);
int n; read(n);
// if (_ == 34) cout << n << endl;
F(i, 1, n * 2) t[i] = 0, vis[i] = false;
F(i, 1, n * 8) {
seg2[i] = 1e9;
seg1[i] = tag[i] = ss[i] = maxn[i] = 0;
while (tt[i].size()) tt[i].pop();
}
// vector <int> tmp;
F(i, 1, n) {
read(a[i].first), read(a[i].second), tmp[++tot] = a[i].first, tmp[++tot] = a[i].second, id[i] = i;
// if (_ == 34) cout << a[i].first << " " << a[i].second << endl;
}
sort(tmp + 1, tmp + tot + 1);
tot = unique(tmp + 1, tmp + tot + 1) - tmp - 1;
F(i, 1, n) a[i].first = lower_bound(tmp + 1, tmp + tot + 1, a[i].first) - tmp, a[i].second = lower_bound(tmp + 1, tmp + tot + 1, a[i].second) - tmp;
sort(id + 1, id + n + 1, [&] (int x, int y) {
if (a[x].first == a[y].first) return a[x].second > a[y].second;
return a[x].first < a[y].first;
// return a[x] < a[y];
});
F(i, 1, n) b[i] = a[id[i]];
// F(i, 1, n) cout << a[i].first << " " << a[i].second << endl;
swap(a, b);
deque <int> q;
set <int> s;
F(i, 1, tot - 1) add(i, tmp[i + 1] - tmp[i]), s.insert(i);
// cout << query(tot) << endl;
F(i, 1, n) {
// cout << i << ": ";
// for (int j: q) cout << j << " "; cout << endl;
// if (q.size()) cout << a[q.back()].second << " " << a[i].first << " " << a[i].second << endl;
while (q.size() && a[q.back()].second >= a[i].second) q.pop_back();
q.push_back(i);
}
ms(vis, false);
for (int i: q) {
vis[i] = true;
// cout << "! " << i << " " << tmp[a[i].second] - tmp[a[i].first] << endl;
modify2(1, 1, tot, i, tmp[a[i].second] - tmp[a[i].first]);
}
// cout << query(12) - query(5) << endl;
F(i, 1, n)
if (!vis[i]) modify1(1, 1, tot, i);
F(i, 1, n) {
int t = ss[1];
cout << id[t] << " ";
// cout << seg2[1] << " " << t << " " << id[t] << endl;
modify2(1, 1, tot, t, 1e9);
// return;
int p = a[t].first == 1 ? -1 : prv(1, 1, tot, t), q = a[t].first == tot ? -1 : nxt(1, 1, tot, t);
// cout << "@ " << p << " " << q << endl;
// cout << seg1[1] << " " << seg1[9] << endl;
while (true) {
int t = query1(1, 1, tot, (~q) ? a[q].first : tot + 1, (~q) ? a[q].second : tot + 1);
// cout << "$$ " << ((~q) ? a[q].first : tot + 1) << " " << ((~q) ? a[q].second : tot + 1) << endl;
if (t == -1) break;
if ((~p) && a[t].first <= a[p].first) break;
// cout << "# " << t << endl;
mmodify1(1, 1, tot, t);
// cout << "!!!" << endl;
// cout << "~ " << t << " " << query(a[t].second - 1) << ' ' << query(a[t].first - 1) << endl;
modify2(1, 1, tot, t, query(a[t].second - 1) - query(a[t].first - 1));
q = t;
}
// cout << maxn[1] << endl;
// cout << "!!!" << " " << a[t].first << " " << a[t].second << endl;
while (true) {
auto pos = s.lower_bound(a[t].first);
if (pos == s.end() || *pos >= a[t].second) break;
int x = *pos; s.erase(pos);
// cout << "@ " << x << " " << tmp[x] - tmp[x + 1] << endl;
add(x, tmp[x] - tmp[x + 1]);
int pp = query2(1, 1, tot, x);
// cout << "& " << pp << " " << x << " " << tmp[x] - tmp[x + 1] << endl;
change(1, 1, tot, pp, x, tmp[x] - tmp[x + 1]);
}
}
cout << '\n';
}
signed main() {
// freopen("interesting.in", "r", stdin);
// freopen("interesting.out", "w", stdout);
int T, sid; read(T);
while (T--) zhk();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
6 1 2 2 3 3 4 4 5 1 3 3 5