链接:https://ac.nowcoder.com/acm/contest/1114/E来源:牛客网
时间限制:C/C++ 2秒,其他语言4秒
空间限制:C/C++ 524288K,其他语言1048576K
64bit IO Format: %lld
题目描述
老瞎眼有一个长度为 n 的数组 a,为了为难小鲜肉,他准备了 Q 次询问,每次给出 一个区间[L,R],他让小鲜肉寻 找一对 l,r 使L<=l<=r<=R 且 a[l]^a[l+1]^a[l+2]...^a[r]=0,老瞎眼只让他回答r-l+1 最小是多少,若没有符合条件的 l,r 输出”-1”。
输入描述:
第一行输入 n,Q。第二行输入 n 个数,表示 a 数组。接下来 Q 行,每行输入 L,R。1<=n,Q<=500000,0<=a[i]<=1000000,1<=L<=R<=n
输出描述:
若有解,输出 r-l+1 最小是多少。否则输出“-1”。
示例1
输入
[复制](javascript:void(0)😉
4 2
2 1 3 3
1 2
1 3
输出
[复制](javascript:void(0)😉
-1
3
说明
第一次询问无解。第二次询问:l=1,r=3
思路:
首先for一遍,可以维护出数组\(a[i]=x\) 代表i左面的第一个位置x,使之\(a[x] \oplus a[x+1]... \oplus a[i]=0\)
然后离线处理询问,将询问的R升序排序,建立节点初始化为无穷大的线段树,然后处理1~n的每一个位置,对每一个位置i,更新线段树中 a[i] ]位置的数值,用线段树维护最小值, 对于R==i时,询问区间 (l,r)的最小值为答案。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline void getInt(int *p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n, q;
struct ask {
int l, r;
int id;
} b[maxn];
int ans[maxn];
struct node {
int l, r;
int val;
} segment_tree[maxn << 2];
void build(int rt, int l, int r)
{
segment_tree[rt].l = l;
segment_tree[rt].r = r;
if (l == r) {
segment_tree[rt].val = inf;
} else {
int mid = (l + r) >> 1;
build(rt << 1, l, mid);
build(rt << 1 | 1, mid + 1, r);
segment_tree[rt].val = min(segment_tree[rt << 1].val, segment_tree[rt << 1 | 1].val);
}
}
void update(int rt, int pos, int val)
{
if (segment_tree[rt].l == segment_tree[rt].r && segment_tree[rt].l == pos) {
segment_tree[rt].val = min(segment_tree[rt].val, val);
return ;
}
int mid = (segment_tree[rt].l + segment_tree[rt].r) >> 1;
if (pos <= mid) {
update(rt << 1, pos, val);
} else {
update(rt << 1 | 1, pos, val);
}
segment_tree[rt].val = min(segment_tree[rt << 1].val, segment_tree[rt << 1 | 1].val);
}
int query(int rt, int l, int r)
{
if (segment_tree[rt].l >= l && segment_tree[rt].r <= r) {
return segment_tree[rt].val;
}
int res = inf;
int mid = (segment_tree[rt].r + segment_tree[rt].l) >> 1;
if (mid >= l) {
res = min(res, query(rt << 1, l, r));
}
if (mid < r) {
res = min(res, query(rt << 1 | 1, l, r));
}
return res;
}
int pos[maxn * 3];
int sum = 0;
int x;
void init()
{
memset(pos, -1, sizeof(pos));
}
bool cmp(ask aa, ask bb)
{
return aa.r < bb.r;
}
int a[maxn];
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
scanf("%d %d", &n, &q);
// cin >> n >> q;
init();
pos[0] = 0ll;
repd(i, 1, n) {
scanf("%d", &x);
// cin >> x;
sum ^= x;
if (pos[sum] != -1) {
a[i] = pos[sum] + 1;
} else {
a[i] = -1;
}
pos[sum] = i;
}
// repd(i, 1, n) {
// cout << a[i] << " ";
// }
// cout << endl;
repd(i, 1, q) {
scanf("%d %d", &b[i].l, &b[i].r);
// cin >> b[i].l >> b[i].r;
b[i].id = i;
}
sort(b + 1, b + 1 + q, cmp);
build(1, 1, n);
int dw = 1;
repd(i, 1, n) {
if (a[i] != -1) {
update(1, a[i], i - a[i] + 1);
// cout << a[i] << " " << i - a[i] + 1 << endl;
}
while (b[dw].r == i) {
ans[b[dw].id] = query(1, b[dw].l, b[dw].r);
dw++;
}
}
repd(i, 1, q) {
if (ans[i] > n) {
printf("-1\n");
// cout << -1 << endl;
} else {
printf("%d\n", ans[i] );
// cout << ans[i] << endl;
}
}
return 0;
}
inline void getInt(int *p)
{
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
} else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}