题目链接
主席树裸题,不知道为什么一建空树就re
#include
using namespace std;
#define int long long
using i64 = long long;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int mod1 = 954169327;
const int mod2 = 906097321;
int idx = 0;
int root[N];
struct Node
{
int ls, rs, sum;
} tr[N * 40];
void pushup(Node &u, Node &left, Node &right)
{
u.sum = left.sum + right.sum;
}
int build(int l, int r)
{
int p = ++ idx;
if (l == r) return p;
int mid = l + r >> 1;
tr[p].ls = build(l, mid);
tr[p].rs = build(mid + 1, r);
return p;
}
int modify(int p, int l, int r, int x)
{
int q = ++ idx;
tr[q] = tr[p];
if (l == x && r == x)
{
tr[q].sum += x;
return q;
}
int mid = l + r >> 1;
if (x <= mid) tr[q].ls = modify(tr[p].ls, l, mid, x);
else tr[q].rs = modify(tr[p].rs, mid + 1, r, x);
pushup(tr[q], tr[tr[q].ls], tr[tr[q].rs]);
return q;
}
Node query(int p, int l, int r, int ll, int rr)
{
if (l >= ll && r <= rr) return tr[p];
int mid = l + r >> 1;
if (rr <= mid) return query(tr[p].ls, l, mid, ll, rr);
else if (ll > mid) return query(tr[p].rs, mid + 1, r, ll, rr);
else
{
auto left = query(tr[p].ls, l, mid, ll, mid);
auto right = query(tr[p].rs, mid + 1, r, mid + 1, rr);
Node res;
pushup(res, left, right);
return res;
}
}
void solve()
{
int n;
cin >> n;
vector<int> a(n + 1);
int maxx = 0;
for (int i = 1; i <= n; i ++ ) cin >> a[i], maxx = max(maxx, a[i]);
// root[0] = build(0, maxx);
for (int i = 1; i <= n; i ++ ) root[i] = modify(root[i - 1], 0, maxx, a[i]);
int q;
cin >> q;
vector<int> ans(q + 1);
for (int i = 1; i <= q; i ++ )
{
int aa, bb, cc;
cin >> aa >> bb >> cc;
int l = aa ^ ans[i - 1];
int r = bb ^ ans[i - 1];
int x = cc ^ ans[i - 1];
auto res1 = query(root[l - 1], 0, maxx, 0, x);
auto res2 = query(root[r], 0, maxx, 0, x);
ans[i] = res2.sum - res1.sum;
cout << ans[i] << '\n';
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
// cin >> t;
while (t -- )
{
solve();
}
}