rt
这是第一份代码
#include <bits/stdc++.h>
using namespace std;
template<class T>void Rd(T &x) {
char c; bool f(1);
while (!isdigit(c = getchar())) f = c != '-';
x = T(c ^ 48);
while (isdigit(c = getchar())) x = x * 10 + (c ^ 48);
x = f ? x : -x;
}
template<class A, class... B>void Rd(A &x, B& ...y) {
Rd(x), Rd(y...);
}
template<class T> inline void Cmx(T &a, T b) {
if (a < b) a = b;
}
template<class T> inline void Cmn(T &a, T b) {
if (a > b) a = b;
}
#define Wr(...) fprintf(stderr, __VA_ARGS__)
const int N(1e5);
struct Tree {
int lc, rc; long long sum;
Tree() {lc = rc = sum = 0;}
};
main() {
int n, m; Rd(n);
vector<int> a(n);
for (int &i : a) Rd(i);
vector<int> root(n); vector<Tree> tr(1);
#define mid ((L + R) >> 1)
function<void(int&, int, int, int, int, int)>
Build = [&](int &rt, int prt, int L, int R, int p, int x) {
rt = tr.size(), tr.push_back(tr[prt]), tr[rt].sum += x;
if (L == R) return ;
p <= mid ? Build(tr[rt].lc, tr[prt].lc, L, mid, p, x) :
Build(tr[rt].rc, tr[prt].rc, mid + 1, R, p, x);
};
function<long long(int, int, int, int, int, int)>
Query = [&](int rt, int prt, int L, int R, int l, int r) {
if (l <= L && R <= r) return tr[rt].sum - tr[prt].sum;
long long res(0);
if (l <= mid) res += Query(tr[rt].lc, tr[prt].lc, L, mid, l, r);
if (r > mid) res += Query(tr[rt].rc, tr[prt].rc, mid + 1, R, l, r);
return res;
};
#undef mid
for (int i(0); i < n; ++i)
Build(root[i], i ? root[i - 1] : 0, 1, 1e9, a[i], a[i]);
Rd(m);
for (int l, r; m--; ) {
Rd(l, r), --l, --r;
int rtl(l ? root[l - 1] : 0), rtr(root[r]);
for (int ans(1); ; ) {
int res(Query(rtr, rtl, 1, 1e9, 1, ans));
if (res >= ans) ans = res + 1;
else { printf("%d\n", ans); break; }
}
}
return 0;
}
这是第二份代码
#include <bits/stdc++.h>
using namespace std;
template<class T>void Rd(T &x) {
char c; bool f(1);
while (!isdigit(c = getchar())) f = c != '-';
x = T(c ^ 48);
while (isdigit(c = getchar())) x = x * 10 + (c ^ 48);
x = f ? x : -x;
}
template<class A, class... B>void Rd(A &x, B& ...y) {
Rd(x), Rd(y...);
}
template<class T> inline void Cmx(T &a, T b) {
if (a < b) a = b;
}
template<class T> inline void Cmn(T &a, T b) {
if (a > b) a = b;
}
#define Wr(...) fprintf(stderr, __VA_ARGS__)
const int N(1e5);
struct Tree {
int lc, rc, sum;
Tree() {lc = rc = sum = 0;}
} tr[N * 100 + 5];
int cnt;
#define mid ((L + R) >> 1)
void Build(int &rt, int prt, int L, int R, int p, int x) {
tr[rt = ++cnt] = tr[prt], tr[rt].sum += x;
if (L == R) return ;
p <= mid ? Build(tr[rt].lc, tr[prt].lc, L, mid, p, x) :
Build(tr[rt].rc, tr[prt].rc, mid + 1, R, p, x);
};
long long Query(int rt, int prt, int L, int R, int l, int r) {
if (l <= L && R <= r) return tr[rt].sum - tr[prt].sum;
long long res(0);
if (l <= mid) res += Query(tr[rt].lc, tr[prt].lc, L, mid, l, r);
if (r > mid) res += Query(tr[rt].rc, tr[prt].rc, mid + 1, R, l, r);
return res;
};
#undef mid
main() {
int n, m; Rd(n);
vector<int> a(n);
for (int &i : a) Rd(i);
vector<int> root(n); vector<Tree> tr(1);
for (int i(0); i < n; ++i)
Build(root[i], i ? root[i - 1] : 0, 1, 1e9, a[i], a[i]);
Rd(m);
for (int l, r; m--; ) {
Rd(l, r), --l, --r;
int rtl(l ? root[l - 1] : 0), rtr(root[r]);
for (int ans(1); ; ) {
int res(Query(rtr, rtl, 1, 1e9, 1, ans));
if (res >= ans) ans = res + 1;
else { printf("%d\n", ans); break; }
}
}
return 0;
}
表面上看好像基本一直,但是第一份代码怎么会RE捏。
测试信息是:Runtime Error. Received signal 11: Segmentation fault with invalid memory reference.