題目鏈接:https://www.luogu.org/problemnew/show/P5105
題目大意:中文題,就不解釋了-,-
題目思路:首先,對於求解,我們通過打表可以知道,求解[1,n]區間的異或和,有如下的規律:
當n%4 == 0 時,等於1;
當n%4 == 1 時,等於2×(n-1);
當n%4 == 2 時,等於3;
當n%4 == 3時,等於2×n。
那麼區間 [l,r] 的異或和就是,代表異或。
這樣就是轉化成了只需要求若干個連續區間的異或和異或後再與中間相隔的值異或的結果就行。
這樣只需要將各區間端點異或之後,藉助線段樹的區間更新和區間合併即可。
由於線段樹內維護的區間是離散後的區間,所以再求結果的時候還需要將其還原回去,記錄一下離散後的兩點是否是連續的,如果是連續的,還得將這段區間異或的結果也計算進去。
具體實現看代碼:
#include <bits/stdc++.h>
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pb push_back
#define MP make_pair
#define all(v) v.begin(),v.end()
#define IOS ios::sync_with_stdio(false)
#define FIN freopen("in.txt","r",stdin)
#define clr(a) memset(a,0,sizeof(a))
#define fuck(x) cout<<"["<<#x<<" "<<x<<"]\n"
using namespace std;
typedef long long ll;
typedef pair<int, int>pii;
typedef pair<ll, ll>pll;
const int MX = 6e5 + 5;
const int inf = 0x3f3f3f3f;
ll cal(ll x) {
if (x % 4 == 0) return 1;
if (x % 4 == 1) return 2ll * (x - 1);
if (x % 4 == 2) return 3;
return 2ll * x;
}
ll cal_sum(ll l, ll r) {
return cal(l) ^ cal(r);
}
vector<int>has;
int get_id(int x) {
return lower_bound(all(has), x) - has.begin() + 1;
}
int q;
struct Que {
int l, r, op;
} Q[MX];
struct Seg_Tree {
int l, r;
bool lazy, tag, vis;
ll left, right;
ll sum;
Seg_Tree operator + (const Seg_Tree &A)const {
Seg_Tree res;
res.l = l; res.r = A.r;
res.left = min(left, A.left);
res.right = max(right, A.right);
if (right == -inf || A.left == inf)
res.sum = sum ^ A.sum;
else
res.sum = sum ^ A.sum ^ (A.left * A.left - right * right);
return res;
}
} T[MX << 2];
void push_up(int rt) {
bool tag = T[rt].tag, lazy = T[rt].lazy, vis = T[rt].vis;
T[rt] = T[rt << 1] + T[rt << 1 | 1];
if (tag)
T[rt].sum = T[rt << 1].sum ^ T[rt << 1 | 1].sum ^ cal_sum(T[rt << 1].right, T[rt << 1 | 1].left);
T[rt].tag = tag; T[rt].lazy = lazy; T[rt].vis = vis;
}
void upd(int rt) {
T[rt].sum = cal_sum(has[T[rt].l - 1], has[T[rt].r - 1]);
T[rt].left = has[T[rt].l - 1];
T[rt].right = has[T[rt].r - 1];
}
void push_down(int rt) {
if (T[rt].lazy) {
upd(rt << 1); upd(rt << 1 | 1);
T[rt << 1].lazy = T[rt << 1 | 1].lazy = 1;
T[rt].lazy = 1;
T[rt].vis = 1;
}
}
void build(int l, int r, int rt) {
T[rt].l = l; T[rt].r = r;
T[rt].left = inf; T[rt].right = -inf;
T[rt].lazy = 0;
T[rt].sum = 0;
if (l == r) return;
int m = (l + r) >> 1;
build(lson); build(rson);
}
void update(int L, int R, int l, int r, int rt) {
if (L <= l && r <= R) {
upd(rt);
T[rt].lazy = 1;
T[rt].vis = 1;
return;
}
push_down(rt);
if (T[rt].vis) return;
int m = (l + r) >> 1;
if (L > m) update(L, R, rson);
else if (R <= m) update(L, R, lson);
else {
T[rt].tag = 1;
update(L, R, lson);
update(L, R, rson);
}
push_up(rt);
}
int main() {
// FIN;
scanf("%d", &q);
for (int i = 1; i <= q; i++) {
scanf("%d", &Q[i].op);
if (Q[i].op == 1) {
scanf("%d%d", &Q[i].l, &Q[i].r);
has.pb(Q[i].l); has.pb(Q[i].r);
}
}
sort(all(has));
has.erase(unique(all(has)), has.end());
int n = has.size();
build(1, n, 1);
for (int i = 1; i <= q; i++) {
if (Q[i].op == 2) printf("%lld\n", T[1].sum);
else update(get_id(Q[i].l), get_id(Q[i].r), 1, n, 1);
}
return 0;
}