跳到正文

剪贴板

剪贴板 j40l9zbl

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;
#define _rep(i_,a_,b_) for(int i_ = (a_); i_ <= (b_); ++i_)
#define mid (L + ((R - L) >> 1))
#define multiCase() int testCnt = in(); _rep(curCase,1,testCnt)
#ifdef ONLINE_JUDGE
#define debug(...) 0
#else
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#endif
using ll = long long;
using pii = pair<int,int>;

namespace FastIO {
char buf[1 << 23], *p1 = buf, *p2 = buf;
inline char gc(void) {
	if(p1 == p2 && (p1 = buf, p2 = buf + fread(buf, 1, 1 << 23, stdin), p1 == p2)) return EOF;
	return *p1++;
}
int in(void) {
	int x = 0, c = gc(), f = 1; while(!isdigit(c)) { if(c == '-') f = -1; c = gc(); }
	while(isdigit(c)) x = x * 10 + c - '0', c = gc();
	return x * f;
}
ll inl(void) {
	ll x = 0; int c = gc(), f = 1; while(!isdigit(c)) { if(c == '-') f = -1; c = gc(); }
	while(isdigit(c)) x = x * 10 + c - '0', c = gc();
	return x * f;
}
char obuf[1 << 23], *p3 = obuf;
inline void pc(char c) { *p3++ = c; if(p3 == obuf + (1 << 23)) fwrite(obuf, 1, 1 << 23, stdout), p3 = obuf; }
void fuck(int x) { if(x > 9) fuck(x / 10); pc(x % 10 + '0'); }
void out(int x) { fuck(x), pc(' '); }
void outln(int x) { fuck(x), pc('\n'); }
void fuck(ll x) { if(x > 9) out(x / 10); pc(x % 10 + '0'); }
void out(ll x) { fuck(x), pc(' '); }
void outln(ll x) { fuck(x), pc('\n'); }
struct FLUSHer { ~FLUSHer() { fwrite(obuf, 1, p3 - obuf, stdout); } } flusher;
} using FastIO::in; using FastIO::inl; using FastIO::out; using FastIO::outln;
// int in(void) { int x; scanf("%d", &x); return x; } ll inl(void) { ll x; scanf("%lld", &x); return x; }
// void out(int x) { printf("%d ", x); } void outln(int x) { printf("%d\n", x); }
// void out(ll x) { printf("%lld ", x); } void outln(ll x) { printf("%lld\n", x); }
template<typename T> void chkmax(T &a, const T &b) { a = max(a, b); }
template<typename T> void chkmin(T &a, const T &b) { a = min(a, b); }
const int kN = 105000, kM = 30000500;
int n, m, a[kN];

struct Node {
	int ch[2];
	int add, mx;
} t[kM]; int nc, lst;
inline void pushup(int x) {
	t[x].mx = max(t[t[x].ch[0]].mx, t[t[x].ch[1]].mx);
	t[x].mx += t[x].add;
}
int clone(int x) { return ++nc, t[nc] = t[x], nc; }
void modify(int &x, int L, int R, int l, int r) {
	if(l > r) return;
	if(x <= lst) x = clone(x);
	if(l <= L && R <= r) { ++t[x].add, ++t[x].mx; return; }
	if(l <= mid) modify(t[x].ch[0], L, mid, l, r);
	if(mid < r) modify(t[x].ch[1], mid + 1, R, l, r);
	pushup(x);
}
int query(int x, int L, int R, int l, int r) {
	if(!x) return 0;
	if(l <= L && R <= r) return t[x].mx;
	int res = 0;
	if(l <= mid) chkmax(res, query(t[x].ch[0], L, mid, l, r));
	if(mid < r) chkmax(res, query(t[x].ch[1], mid + 1, R, l, r));
	return res + t[x].add;
}
int main() {
	n = in(), m = in();
	_rep(i,1,n) a[i] = in();
	int currt = 0;
	_rep(i,0,30) {
		int tmp = ++nc;
		t[tmp].ch[0] = t[tmp].ch[1] = currt;
		currt = tmp, pushup(currt);
		lst = nc;
		//第 i 层的线段树维护 [0, 1 << (i + 1)) 这段区间
		_rep(j,1,n) {
			int msk = a[j] & ((1u << i) - 1), tot = 1u << i;
			if(a[j] >> i & 1) {
				modify(currt, 0, (1u << (i + 1)) - 1, 0, tot - msk - 1);
				modify(currt, 0, (1u << (i + 1)) - 1, (1u << i) + tot - msk, (1u << (i + 1)) - 1);
			} else {
				modify(currt, 0, (1u << (i + 1)) - 1, tot - msk, (1u << i) - 1);
				modify(currt, 0, (1u << (i + 1)) - 1, (1u << i), (1u << i) + tot - msk - 1);
			}
		}
	}
	_rep(i,1,m) {
		int l = in(), r = in();
		outln(query(currt, 0, (1u << 31) - 1, l, r));
	}
	return 0;
}