P6639 [JYLOI Round 1] 让 题解
题意简述
给定一个初始参数 R,支持两种操作:
- change x:将当前参数 R 修改为 x。
- query n l1 r1 l2 r2 … ln rn:查询 n 个区间 的异或和。
核心思路
本题最重要的点在于快速计算与参数 R 相关的函数前缀异或。
定义 ,则有:
- 当 时,
- 当 时,
- 当 时,
- 当 时,
数据结构设计
构造序列 :
- 迭代 ,直到 ,将每个 加入
对于每个 ,维护 。
对于任意 ,找到最大的 ,有:
复杂度分析
- change 操作:
- query 操作:
- 总时间复杂度:
Code:
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
ll xo(ll n)
{
if(n<=0)return 0;
ll t=n&3;
if(t==0)return n;
if(t==1)return 1;
if(t==2)return n+1;
return 0;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T;cin>>T;
string out;
ll R=0;
vector<ll>p,s;
auto build=[&](ll r)
{
R=r;
p.clear();
p.push_back(0);
ll x=0;
while(1)
{
x=(x+R)/2+1;
if(x>=R)break;
p.push_back(x);
}
s.assign(p.size(),0);
for(int i=1;i<(int)p.size();++i)
{
ll len=p[i]-p[i-1]-1;
s[i]=s[i-1]^xo(len);
}
};
auto pre=[&](ll x)->ll
{
if(x<=0)return 0;
int k=upper_bound(p.begin(),p.end(),x)-p.begin()-1;
if(x==p[k])return s[k];
return s[k]^xo(x-p[k]);
};
while(T--)
{
string op;cin>>op;
if(op=="change")
{
ll x;cin>>x;
build(x);
}
else
{
int n;cin>>n;
ll ans=0;
while(n--)
{
ll l,r;cin>>l>>r;
ans^=pre(r)^pre(l-1);
}
out.push_back(ans?'1':'0');
}
}
cout<<out<<"\n";
return 0;
}