D. Yet Another Problem

Problem - D - Codeforces

题意:给定一个数组,m个查询,每个查询l,r,要求查询l,r中最少有几个字段进行替换,将L,R中每一个元素变为字段异或和,将整个l,r变为0。

思路:对于一些较小的异或和0的字段可以合并为一个大的字段。对于每个查询,l,r,如果该段内的异或和不为0,则直接是-1,如果长度为偶数,分类讨论,将前后缀分别置零opnum==2,前后有为0的opnum==1

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<string>
#include<bitset>
#include<cmath>
#include<array>
#include<atomic>
#include<sstream>
#include<stack>
#include<iomanip>
//#include<bits/stdc++.h>

//#define int ll
#define IOS std::ios::sync_with_stdio(false);std::cin.tie(0);
#define pb push_back
#define endl '\n'
#define x first
#define y second
#define Endl endl
#define pre(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,b,a) for(int i=b;i>=a;i--)
#define si(x) scanf("%d", &x);
#define sl(x) scanf("%lld", &x);
#define ss(x) scanf("%s", x);
#define YES {puts("YES");return;}
#define NO {puts("NO"); return;}
#define all(x) x.begin(),x.end()

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<int, PII> PIII;
typedef pair<char, int> PCI;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<ll, ll> PLL;
const int N = 200010, M = 2 * N, B = N, MOD = 2e18;
const double eps = 1e-7;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;

int dx[4] = { -1,0,1,0 }, dy[4] = { 0,1,0,-1 };
//int dx[8] = { 1,2,2,1,-1,-2,-2,-1 }, dy[8] = { 2,1,-1,-2,-2,-1,1,2 };
int n, m, k;
int a[N];
int prexor[N],cnt[N];
int ne[N], pre[N];

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll lowbit(ll x) { return x & -x; }
ll qmi(ll a, ll b, ll MOD) {
	ll res = 1;
	while (b) {
		if (b & 1) res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

inline void init() {}

inline void slove()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) si(a[i]);

	for (int i = 1; i <= n; i++)
		prexor[i] = prexor[i - 1] ^ a[i], cnt[i] = cnt[i - 1] + (a[i] == 0), ne[i] = n + 1;

	map<int, int> last[2];
	last[0][0] = 0;
	for (int i = 1; i <= n; i++)
	{
		int bit = i & 1;
		if (last[bit ^ 1].count(prexor[i])) {
			int lastpos = last[bit ^ 1][prexor[i]];
			pre[i] = lastpos + 1;
			ne[lastpos + 1] = min(ne[lastpos + 1], i);
		}
		last[bit][prexor[i]] = i;
	}

	while (m--)
	{
		int l, r;
		si(l); si(r);
		int x = prexor[r] ^ prexor[l - 1];
		if (x) {
			cout << -1 << endl;
		}
		else if (cnt[r] - cnt[l - 1] == r - l + 1) {
			cout << 0 << endl;
		}
		else if ((r - l + 1) & 1) {
			cout << 1 << endl;
		}
		else if (a[l] == 0 || a[r] == 0) {
			cout << 1 << endl;
		}
		else if (ne[l] <= r || pre[r] >= l)
		{
			cout << 2 << Endl;
		}
		else cout << -1 << endl;
	}
}

signed main()
{
	//IOS;
	int _ = 1;
	//si(_);
	init();
	while (_--)
	{
		slove();
	}
	return 0;
}
/*
1
6 6
5 5 5 5 5 5

*/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值