QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#411957 | #8672. 排队 | Xiaohuba | 0 | 3ms | 88688kb | C++23 | 5.9kb | 2024-05-15 22:14:19 | 2024-05-16 17:38:43 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define LOCK_GETCHAR
// #define USE_INT_128
#if __cplusplus < 201400
#warning "Please use c++14 or higher."
#define CONSTEXPR_FUNC
#define ENABLE_IF_INT
#else
#define CONSTEXPR_FUNC constexpr
#define ENABLE_IF_INT , enable_if_t<_is_integer<T>, int> = 0
template <class T> constexpr bool _is_integer = numeric_limits<T>::is_integer;
template <> constexpr bool _is_integer<bool> = false;
template <> constexpr bool _is_integer<char> = false;
#ifdef USE_INT_128
template <> constexpr bool _is_integer<__int128> = true;
template <> constexpr bool _is_integer<__uint128_t> = true;
#endif
template <class T ENABLE_IF_INT>
constexpr T INF = numeric_limits<T>::max() >> 1;
#endif
#if !defined(_WIN32) && !defined(LOCK_GETCHAR)
#define getchar getchar_unlocked
#endif
#define il inline
#define mkp make_pair
#define fi first
#define se second
#define For(i, j, k) for (decltype(j - k) i = (j); i <= (k); ++i) // NOLINT
#define ForDown(i, j, k) for (decltype(j - k) i = (j); i >= (k); --i) // NOLINT
#define pb push_back
#define eb emplace_back
#ifndef ONLINE_JUDGE
#define FileIO(filename) \
freopen(filename ".in", "r", stdin); \
freopen(filename ".out", "w", stdout)
#else
#define FileIO(filename) void(0)
#endif
using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#ifdef USE_INT_128
using lll = __int128_t;
using ulll = __uint128_t;
#endif
// clang-format off
#define lg(x) (31 ^ __builtin_clz((x)))
#define lgll(x) (31ll ^ __builtin_clzll((x)))
template<typename T> constexpr il T sq(const T & x){ return x * x; }
template<typename T> CONSTEXPR_FUNC il void cmin(T & x, const T &y){ x = min(x, y); }
template<typename T> CONSTEXPR_FUNC il void cmax(T & x, const T &y){ x = max(x, y);}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y, T mod){T ans = 1; x %= mod; while (y) { if(y & 1)(ans *= x) %= mod;(x *= x) %= mod; y >>= 1;} return ans;}
template<typename T> CONSTEXPR_FUNC il T qpow(T x, ull y){T ans = 1; while (y) {if(y & 1) ans *= x;x *= x;y >>= 1;} return ans;}
template<typename T ENABLE_IF_INT> il void read(T &x){ x = 0; int f = 1; int c = getchar(); while(!isdigit(c)) {if (c == '-') f = -1;c = getchar();} while(isdigit(c)) {x = x * 10 + c - '0';c = getchar();} x *= f;}
template<typename T, typename ... Args> il void read(T &x, Args &... y){ read(x); read(y...); }
int gcd(int a, int b) { if (!a | !b) return a + b; int az = __builtin_ctz(a); int bz = __builtin_ctz(b); int z = min(az, bz); a >>= az, b >>= bz; while (a != b) { int diff = b - a; az = __builtin_ctz(diff); b = min(a, b), a = abs(diff) >> az; } return a << z; }
ll gcd(ll a, ll b) { if (!a | !b) return a + b; ll az = __builtin_ctzll(a); ll bz = __builtin_ctzll(b); ll z = min(az, bz); a >>= az, b >>= bz; while (a != b) { ll diff = b - a; az = __builtin_ctzll(diff); b = min(a, b), a = abs(diff) >> az; } return a << z; }
// clang-format on
// File head end
namespace {
constexpr ll MAXN = 1e6 + 5, INF = 1e9 + 5;
struct Node {
int l, r, tag;
pii minv;
Node() : l(0), r(0), tag(0), minv(INF, INF) {}
} T[MAXN << 2];
#define lc(p) ((p) << 1)
#define rc(p) ((p) << 1 | 1)
[[gnu::always_inline]] il void _add(pii &x, int y) { x.fi += y, x.se += y; }
[[gnu::always_inline]] il pii _cmp(pii x, pii y) {
return {min(x.fi, y.fi), min(x.se, y.se)};
}
[[gnu::always_inline]] il int _sel(pii x, int dim) {
return (dim) ? x.se : x.fi;
}
il void pu(int p) { T[p].minv = _cmp(T[lc(p)].minv, T[rc(p)].minv); }
il void pd(int p) {
_add(T[lc(p)].minv, T[p].tag), _add(T[rc(p)].minv, T[p].tag);
T[p].tag = 0;
}
void build(int p, int l, int r) {
T[p].l = l, T[p].r = r;
if (l == r)
return;
int mid = (l + r) >> 1;
build(lc(p), l, mid), build(rc(p), mid + 1, r);
pu(p);
}
void add(int p, int ql, int val) {
int l = T[p].l, r = T[p].r;
if (ql <= T[p].l)
return _add(T[p].minv, val);
int mid = (l + r) >> 1;
pd(p);
if (ql <= mid)
add(lc(p), ql, val);
add(rc(p), ql, val);
pu(p);
}
void upd(int p, int pos, pii v) {
int l = T[p].l, r = T[p].r;
if (l == r) {
if (v.fi != -1)
T[p].minv.fi = v.fi;
if (v.se != -1)
T[p].minv.se = v.se;
return;
}
int mid = (l + r) >> 1;
pd(p);
if (pos <= mid)
upd(lc(p), pos, v);
else
upd(rc(p), pos, v);
pu(p);
}
int qry(int p, int dim) {
if (T[p].l == T[p].r)
return T[p].l;
pd(p);
if (_sel(T[lc(p)].minv, dim) <= 0)
return qry(lc(p), dim);
if (_sel(T[rc(p)].minv, dim) <= 0)
return qry(rc(p), dim);
return T[p].r + 1;
}
int n, q, Ans[MAXN], bit[MAXN];
pii a[MAXN];
vector<pii> Qry[MAXN];
#define lbt(x) (x & -x)
il void bit_add(int p, int v) {
for (; p <= n; p += lbt(p))
bit[p] += v;
}
il int bit_sum(int p) {
int ans = 0;
for (; p; p -= lbt(p))
ans += bit[p];
return ans;
}
il void change(int x, int dlt) {
while (1) {
bit_add(x, dlt);
if (dlt == 1) {
upd(1, x, mkp(INF, -1));
(x < n) && (add(1, x + 1, -1), 1);
} else {
upd(1, x, mkp(INF, INF));
(x < n) && (add(1, x + 1, 1), 1);
}
int v1 = qry(1, 0), v2 = qry(1, 1);
if (v1 < v2)
x = v1, dlt = 1;
else if (v1 > v2)
x = v2, dlt = -1;
else {
assert(v1 == n + 1);
break;
}
}
}
il void Main() {
read(n, q);
For(i, 1, n) read(a[i].fi, a[i].se);
For(i, 1, q) {
int l, r;
read(l, r), Qry[l].eb(r, i);
}
build(1, 1, n);
ForDown(i, n, 1) {
upd(1, i, mkp(a[i].fi, a[i].se + 1));
if (!a[i].fi)
change(i, 1);
for (auto [j, k] : Qry[i])
Ans[k] = bit_sum(j);
}
For(i, 1, q) cout << Ans[i] << '\n';
}
} // namespace
signed main() { return Main(), 0; }
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Time Limit Exceeded
Test #1:
score: 10
Accepted
time: 3ms
memory: 88688kb
input:
3 3 0 0 1 2 0 2 1 1 1 3 2 3
output:
1 3 1
result:
ok 3 number(s): "1 3 1"
Test #2:
score: 0
Time Limit Exceeded
input:
5000 5000 5 10 3 9 3 8 2 7 2 5 3 6 1 5 0 2 7 8 2 10 0 3 3 6 4 6 1 6 4 8 7 8 2 7 3 4 4 9 7 8 2 9 2 5 3 6 0 5 6 7 1 2 2 4 2 10 1 5 7 9 6 9 2 3 9 10 5 5 2 9 3 3 2 7 2 4 0 6 0 3 1 7 7 7 4 8 2 9 4 8 0 10 1 8 1 1 2 7 5 9 1 7 1 7 1 4 2 4 1 4 2 9 1 7 4 7 3 8 1 3 4 6 1 5 1 6 0 0 3 9 4 7 2 8 1 8 1 2 7 8 2 7 2...
output:
result:
Subtask #2:
score: 0
Time Limit Exceeded
Test #12:
score: 0
Time Limit Exceeded
input:
200000 200000 3 6 3 3 6 10 1 7 2 7 6 9 4 6 3 4 0 8 0 6 3 5 3 4 1 8 7 8 4 5 0 3 1 5 2 9 1 2 1 2 3 4 5 7 6 10 3 9 4 7 1 6 2 6 1 7 2 5 1 7 6 8 1 1 0 7 7 8 0 9 1 7 3 8 3 7 1 2 4 8 5 6 0 6 5 6 2 7 2 6 0 6 0 6 1 7 2 5 0 3 0 3 7 10 3 8 0 2 3 4 3 7 4 9 0 6 4 7 2 6 8 10 2 10 4 10 3 3 2 6 4 5 3 9 1 8 1 2 2 9 ...
output:
result:
Subtask #3:
score: 0
Time Limit Exceeded
Test #22:
score: 0
Time Limit Exceeded
input:
200000 200000 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 0 0 9 0 10 0 0 0 0 0 13 0 14 0 0 0 16 0 17 0 18 0 19 0 0 0 21 0 22 0 23 0 0 0 0 0 0 0 0 0 28 0 0 0 30 0 31 0 32 0 33 0 34 0 35 0 0 0 0 0 38 0 39 0 40 0 41 0 42 0 43 0 44 0 45 0 46 0 0 0 48 0 49 0 50 0 51 0 52 0 53 0 54 0 55 0 56 0 57 0 0 0 59 0 60 0 0 0 0 ...
output:
result:
Subtask #4:
score: 0
Time Limit Exceeded
Test #32:
score: 0
Time Limit Exceeded
input:
200000 200000 0 200000 1 200000 1 200000 0 200000 0 200000 1 200000 1 200000 1 200000 0 200000 1 200000 0 200000 0 200000 1 200000 0 200000 0 200000 0 200000 0 200000 1 200000 0 200000 0 200000 1 200000 0 200000 1 200000 1 200000 1 200000 1 200000 0 200000 0 200000 1 200000 2 200000 1 200000 2 20000...
output:
result:
Subtask #5:
score: 0
Skipped
Dependency #1:
0%
Subtask #6:
score: 0
Skipped
Dependency #5:
0%