当前位置:网站首页>LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
LOJ 510 - "libreoj noi round 1" memories outside the north school gate [line segment tree]
2022-07-27 16:51:00 【QuantAsk】
On the subject
Topic link :https://loj.ac/p/510
The main idea of the topic
Give me a code
function add(x,v)
while x <= n do
s[x] = s[x] xor v
x = x + lowbit(x) // Be careful , Here is lowbit, This is also the only difference between the two codes
end while
end function
function query(x)
ans = 0
while x > 0 do
ans = ans xor s[x]
x = x - lowbit(x)
end while
return ans
end function
among lowbit(x) \text{lowbit(x)} lowbit(x) Express x x x stay K K K The value of the lowest non-zero position in hexadecimal .
Now give n , q , K n,q,K n,q,K, q q q Secondary call a d d ( x , v ) add(x,v) add(x,v) perhaps q u e r y ( x ) query(x) query(x).
Require output each time q u e r y ( x ) query(x) query(x) The value of the call .
1 ≤ n ≤ 1 0 9 , 2 ≤ q , K ≤ 2 × 1 0 5 1\leq n\leq 10^9,2\leq q,K\leq 2\times 10^5 1≤n≤109,2≤q,K≤2×105
Their thinking
Notice that we can do it one by one when asking , Mainly when modifying .
We first default that the lowest non-zero bit is a bit , Then it's equivalent to every time x = x + x % K x=x+x\% K x=x+x%K until x ∣ K x|K x∣K, If only one bit is considered , It can also be regarded as every time x = 2 x % K x=2x\% K x=2x%K.
First of all, there are only two conditions for each number to reach it , One is x 2 \frac{x}{2} 2x, The other is x + K 2 \frac{x+K}{2} 2x+K, So if K K K If it's an odd number , Exactly one of these two is an integer , That is, the entry side of each number is 1 1 1, It's also out of the way 1 1 1, So the final graph will definitely form several rings .
Then consider K K K Not an odd number , If x x x Of 2 2 2 The number of qualitative factors shall not be less than K K K Of , Then they can be expressed as x 2 p \frac{x}{2^p} 2px and K 2 p \frac{K}{2^p} 2pK, In this case K K K It's odd again , It will also form a ring .
otherwise x x x Multiply by two until the number of prime factors is not less than K K K, That is to say, this situation can't go beyond log K \log K logK One step is bound to reach a ring or 0 0 0.
So let's calculate the quantile first , Jump violently if the lowest position is not on the ring , Consider maintenance on the ring .
Now it's mainly about how to maintain the ring , On the same lowest position , For a modification x y xy xy( One bit is y y y, The remaining bits are x x x), Will affect an inquiry x ′ y ′ x'y' x′y′ Conditions .
We need to find a condition that is not about the bits of these points , Then we will consider defining a point for each ring as the ring head , Then all the operation queries are moved to the ring head .
Then we divide each modification into a section from the modified position to the ring head and a section from the ring head that keeps circling ,
Then one of the first restrictions is that the query position should be behind it and they all fall back to the same value behind the ring head , You can use the segment tree to maintain the positional relationship .
The second one first moves the inquiry upside down to the ring head , Then the limitation is that they are for The sum of carry times on the ring The remainder of is equal and the queried value is greater than the modified value , You can use the weight segment tree to maintain .
Time complexity : O ( K log 2 n ) O(K\log^2 n) O(Klog2n)
code
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define mp(x,y) make_pair(x,y)
using namespace std;
const int N=2e5+10,M=N<<7;
int n,q,K,cnt,lg[N],id[N],sum[N],noe[N],dis[N],len[N];
map<pair<int,int> ,int>rt[40],Rt[40];
map<int,int> g;
struct SegTree{
int cnt,w[M],ls[M],rs[M];
void Change(int &x,int L,int R,int pos,int val){
if(pos<L||pos>R)return;
if(!x)x=++cnt;w[x]^=val;
if(L==R)return;int mid=(L+R)>>1;
if(pos<=mid)Change(ls[x],L,mid,pos,val);
else Change(rs[x],mid+1,R,pos,val);
return;
}
int Ask(int x,int L,int R,int l,int r){
r=min(r,R);l=max(l,L);
if(l>r||!w[x])return 0;
if(L==l&&R==r)return w[x];
int mid=(L+R)>>1;
if(r<=mid)return Ask(ls[x],L,mid,l,r);
if(l>mid)return Ask(rs[x],mid+1,R,l,r);
return Ask(ls[x],L,mid,l,mid)^Ask(rs[x],mid+1,R,mid+1,r);
}
}T;
void dfs(int x,int pos,int one,int fr){
if(id[x]){
sum[fr]=one;return;}
noe[x]=one;id[x]=fr;dis[x]=pos;len[fr]++;
dfs(x*2%K,pos+1,one+(x*2>=K),fr);
return;
}
int main()
{
scanf("%d%d%d",&n,&q,&K);
for(int i=1;i<=K;i++)
if(i%2==0)lg[i]=lg[i/2]+1;
for(int i=1;i<K;i++)
if(lg[i]>=lg[K]&&!id[i])
++cnt,dfs(i,0,0,cnt);
while(q--){
int op;scanf("%d",&op);
if(op==1){
int x,w,i=0,pw=1;
scanf("%d%d",&x,&w);
while(x*pw<=n){
int p=x%K,y=x/K;
while(p&&x*pw<=n){
if(lg[p]>=lg[K]){
T.Change(rt[i][mp(id[p],y-noe[p])],0,len[id[p]]-1,dis[p],w);
T.Change(Rt[i][mp(id[p],(y-noe[p]+sum[id[p]])%sum[id[p]])],0,n/pw/K,y+sum[id[p]]-noe[p],w);
x=0;break;
}
else g[x*pw]^=w,x+=x%K,p=x%K,y=x/K;
}
if(!x||x*pw>n)break;
x=x/K;pw=pw*K;i++;
}
}
else{
int x;scanf("%d",&x);
int i=0,pw=1,ans=0;
while(x){
int p=x%K,y=x/K;
if(p!=0){
if(lg[p]>=lg[K]){
ans^=T.Ask(rt[i][mp(id[p],y-noe[p])],0,len[id[p]]-1,0,dis[p]);
ans^=T.Ask(Rt[i][mp(id[p],(y-noe[p]+sum[id[p]])%sum[id[p]])],0,n/pw/K,0,y-noe[p]);
}
else ans^=g[x*pw];
}
x/=K;i++;pw=pw*K;
}
printf("%d\n",ans);
}
}
return 0;
}
边栏推荐
- Collection! 0 basic open source data visualization platform flyfish large screen development guide
- KMEANS 实现
- Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl
- 最大子段和 Go 四种的四种求解
- (二)动态卷积之Dynamic Convolution
- JSON data format usage
- Google Chrome reversecaptcha ad blocking
- In addition to "adding machines", in fact, your micro service can be optimized like this
- t5 学习
- Reduce PDF document size (Reprint)
猜你喜欢

知网、万方数据库免费下载论文------比连接学校内网速度快数倍不止(有的学校万方数据库不支持下载)

Rotate string left

Life game, universe 25 and striver

字节跳动服务网格基于 Hertz 框架的落地实践

201403-1

补充—整数规划例题

Replication of complex linked list

HowNet and Wanfang database download papers for free ----- several times faster than connecting to the school intranet (some schools Wanfang database does not support downloading)

Matlab legend usage

数据采集之:巧用布隆过滤器提取数据摘要
随机推荐
Duplicate numbers in array
android中的图片三级缓存
Opencv (V) -- moving target recognition
Start from scratch blazor server (1) -- project construction
Array analog linked list
Database notes sorting
gpt-2 文本生成
2021-03-09
Exe program encryption lock
Cvxpy - latest issue
string数字类型转换为千分位
201403-1
C语言逆序输出字符串
Crawl common English names
UML图介绍
2021-06-02
Fast Planner - detailed explanation of kinetic astar
雪花ID(Go 实现)
JSON data format usage
[paper reading] transformer with transfer CNN for remote sensing imageobject detection