Description
https://www.lydsy.com/JudgeOnline/problem.php?id=4299
Solution
考虑对于当前集合答案为,然后向集合加入一个,如果,那么答案变为,否则答案不变。
对于本题,如果当前答案为,那么查询小于等于的数的和是多少,如果大于等于,那么更新答案。
用主席树维护区间内小于等于的数的和即可。
Code
/**************************************
* Au: Hany01
* Prob: Sep 24th, 2018
* Date: [BZOJ4299][BZOJ4408][CC FRBSUM]
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1e5 + 5, N = 1e9;
int n, tot, rt[maxn];
struct Node {
int val, lc, rc;
}tr[maxn * 33];
#define mid ((l + r) >> 1)
void update(int las, int& t, int l, int r, int x) {
tr[t = ++ tot] = tr[las], tr[t].val += x;
if (l < r)
if (x <= mid) update(tr[las].lc, tr[t].lc, l, mid, x);
else update(tr[las].rc, tr[t].rc, mid + 1, r, x);
}
int query(int t1, int t2, int l, int r, int x, int y) {
if (x <= l && r <= y) return tr[t2].val - tr[t1].val;
if (y <= mid) return query(tr[t1].lc, tr[t2].lc, l, mid, x, y);
if (x > mid) return query(tr[t1].rc, tr[t2].rc, mid + 1, r, x, y);
return query(tr[t1].lc, tr[t2].lc, l, mid, x, y) + query(tr[t1].rc, tr[t2].rc, mid + 1, r, x, y);
}
int main()
{
#ifdef hany01
File("bzoj4299");
#endif
For(i, 1, n = read()) update(rt[i - 1], rt[i], 1, N, read());
for (register int m = read(), l, r, ans; m --; ) {
l = read(), r = read(), ans = 0;
while (chkmax(ans, query(rt[l - 1], rt[r], 1, N, 1, ans + 1)));
printf("%d\n", ans + 1);
}
return 0;
}
/**************************************
* Au: Hany01
* Prob: Sep 24th, 2018
* Date: [BZOJ4299][BZOJ4408][CC FRBSUM]
* Email: hany01@foxmail.com
**************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = j; i < i##_end_; ++ i)
#define For(i, j ,k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define PB(a) push_back(a)
#define MP(a, b) make_pair(a, b)
#define x first
#define y second
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define y1 wozenmezhemecaia
#ifdef hany01
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read() {
register char c_; register int _, __;
for (_ = 0, __ = 1, c_ = getchar(); !isdigit(c_); c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; isdigit(c_); c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 1e5 + 5, N = 1e9;
int n, tot, rt[maxn];
struct Node {
int val, lc, rc;
}tr[maxn * 33];
#define mid ((l + r) >> 1)
void update(int las, int& t, int l, int r, int x) {
tr[t = ++ tot] = tr[las], tr[t].val += x;
if (l < r)
if (x <= mid) update(tr[las].lc, tr[t].lc, l, mid, x);
else update(tr[las].rc, tr[t].rc, mid + 1, r, x);
}
int query(int t1, int t2, int l, int r, int x, int y) {
if (x <= l && r <= y) return tr[t2].val - tr[t1].val;
if (y <= mid) return query(tr[t1].lc, tr[t2].lc, l, mid, x, y);
if (x > mid) return query(tr[t1].rc, tr[t2].rc, mid + 1, r, x, y);
return query(tr[t1].lc, tr[t2].lc, l, mid, x, y) + query(tr[t1].rc, tr[t2].rc, mid + 1, r, x, y);
}
int main()
{
#ifdef hany01
File("bzoj4299");
#endif
For(i, 1, n = read()) update(rt[i - 1], rt[i], 1, N, read());
for (register int m = read(), l, r, ans; m --; ) {
l = read(), r = read(), ans = 0;
while (chkmax(ans, query(rt[l - 1], rt[r], 1, N, 1, ans + 1)));
printf("%d\n", ans + 1);
}
return 0;
}