BZOJ4571

BZOJ4571

Description

Transmission Gate

给定n个数, m次询问, 每次询问[l,r]范围内的数加上x后异或b的最大值, x, b给出.

\[n,m <= 2e5, ai, b, x \leq 1e5 \]

Solution

考虑不用加上x的做法, 那么直接从高到低位贪心.

但是在加上了x后, 就不那么好搞了. 同样考虑按位处理, 令ans为确定了前几位的答案,那么如果b的某一位是1, 最好在[ans, ans | ((1 << j) - 1)]取值. 我们只要看在这个区间内是否有值就可以了, 因为要加上x, 我们处理时反着减去x即可

Code

#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
const int BUF_SIZE = (int)1e6 + 10;
struct fastIO {
    char buf[BUF_SIZE], buf1[BUF_SIZE];
    int cur, cur1;
    FILE *in, *out;
    fastIO() {
        cur = BUF_SIZE, in = stdin, out = stdout;
		cur1 = 0;
    }
    inline char getchar() {
        if(cur == BUF_SIZE) fread(buf, BUF_SIZE, 1, in), cur = 0;
        return *(buf + (cur++));
    }
    inline void putchar(char ch) {
        *(buf1 + (cur1++)) = ch;
        if (cur1 == BUF_SIZE) fwrite(buf1, BUF_SIZE, 1, out), cur1 = 0;
    }
    inline int flush() {
        if (cur1 > 0) fwrite(buf1, cur1, 1, out);
        return cur1 = 0;
    }
}IO;
#define getchar IO.getchar
#define putchar IO.putchar
int read() {
	char ch = getchar();
	int x = 0, flag = 1;
	for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
	for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
	return x * flag;
}
void write(int x) {
	if(x < 0) putchar('-'), x = -x;
	if(x >= 10) write(x / 10);
	putchar(x % 10 + 48);
}
void putString(char s[], char EndChar = '\n') {
	rep(i, 0, strlen(s) - 1) putchar(*(s + i));
	if(~EndChar) putchar(EndChar);
}

#define Maxn 200009
int n, m, a[Maxn], rt[Maxn];
namespace ChairmanTree {
	struct Node {
		int son[2], val;
	}tree[Maxn * 30]; 
	int amt;
	int build(int l, int r) {
		if(l > r) return 0;
		int u = ++amt, mid = (l + r) >> 1;
		tree[u] = (Node){0, 0, 0};
		if(l == r) return u; /**/
		tree[u].son[0] = build(l, mid);
		tree[u].son[1] = build(mid + 1, r);
		return u;
	}
	int modify(int sou, int l, int r, int v) {
		if(l > r) return 0;
		int u = ++amt, mid = (l + r) >> 1;
		tree[u] = tree[sou]; ++tree[u].val;
		if(l == r) return u;
		if(v <= mid) tree[u].son[0] = modify(tree[sou].son[0], l, mid, v);
		else tree[u].son[1] = modify(tree[sou].son[1], mid + 1, r, v);
		return u;
	}
	int query(int Lr, int Rr, int l, int r, int q1, int q2) {
		if(q1 <= l && r <= q2) return tree[Rr].val - tree[Lr].val;
		int mid = (l + r) >> 1;
		if(q2 <= mid) return query(tree[Lr].son[0], tree[Rr].son[0], l, mid, q1, q2);	
		else if(q1 > mid) return query(tree[Lr].son[1], tree[Rr].son[1], mid + 1, r, q1, q2);
		else return query(tree[Lr].son[0], tree[Rr].son[0], l, mid, q1, mid) + query(tree[Lr].son[1], tree[Rr].son[1], mid + 1, r, mid + 1, q2);
	}
}
namespace INIT {
	void Main() {
		n = read(); m = read();
		rt[0] = ChairmanTree :: build(1, Maxn - 1);
		rep(i, 1, n) a[i] = read(), rt[i] = ChairmanTree :: modify(rt[i - 1], 1, Maxn - 1, a[i]);
	}
}
namespace SOLVE {
	void Main() {
		rep(i, 1, m) {
			int b = read(), x = read(), l = read(), r = read();
			int ans = 0;
			drep(j, 17, 0) 
				if(b & (1 << j)) {
					int L = ans, R = ans | ((1 << j) - 1);
					L -= x, R -= x;
					if(R <= 0 || !ChairmanTree :: query(rt[r], rt[l - 1], 1, Maxn - 1, L, R)) ans ^= (1 << j);
				}else {
					int L = ans | (1 << j), R = ans | ((1 << (j + 1)) - 1);
					L -= x, R -= x;
					if(R <= 0 || ChairmanTree :: query(rt[r], rt[l - 1], 1, Maxn - 1, L, R)) ans ^= (1 << j);
				}
			write(ans ^ b), putchar('\n');
		}
	}
}
int main() {
#ifdef Qrsikno
	freopen("BZOJ4571.in", "r", stdin);
	freopen("BZOJ4571.out", "w", stdout);
#endif
	INIT :: Main();
	SOLVE :: Main();
#ifdef Qrsikno
	debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
	return IO.flush();
}
posted @ 2018-09-22 14:17  Qrsikno  阅读(143)  评论(0编辑  收藏  举报