[ICPC 2024 Hangzhou R] Barkley III
time limit per test
5 s
memory limit per test
1024 megabytes
There are n little pigs in Pigeland. All of them are proficient in competitive programming, and the i-th of them has ai rating. If k pigs p1,p2,⋯,pk form a team, the rating of the team will be ap1 & ap2 & ap3 &⋯& apk, where & denotes the bitwise AND operation.
There are some programming contests to take place. Pigeland can send exactly one team to participate in each contest. For the i-th competition, only the pigs numbered between li and ri (both inclusive) have time to participate. Unfortunately, due to a shortage of funds, exactly one pig numbered between li and ri has to be removed. Meanwhile, all other pigs in the interval will participate in the contest. Pig-head, the coach of Pigeland, needs to properly select pigs who will not participate so that the team's rating is maximized.
However, through training and participating in contests, the rating of pigs may be changed. As Pig-head's best friend, your task is to maintain the pigs' rating for the following q events belonging to three types.
- 1lrx: Pig-head changes the rating of each pig numbered between l and r (both inclusive) by executing the bitwise AND operation with x. More formally, for all l≤i≤r, ai becomes ai & x.
- 2sx: Pig-head changes the rating of the s-th pig to x.
- 3lr: Pig-head asks for the maximum rating when forming a team by selecting pigs numbered between l and r (both inclusive) and removing exactly one of them.
有道 翻译
小猪乐园里有小猪。他们都精通竞技编程,其中 i \第1位的评分为 ai 。如果 k 只猪 p1,p2,⋯,pk 组成一个队,则该队的评分为 ap1 & ap2 & ap3 &⋯& apk ,其中 & 表示按位与运算。
这里将举行一些编程竞赛。Pigeland只能派出一支队伍参加每场比赛。对于 i 第1次比赛,只有编号在 li 和 ri 之间的猪才有时间参加。不幸的是,由于资金短缺,只有一头编号在 li 和 ri 之间的猪不得不被移除。与此同时,其他所有的猪都将参加比赛。猪头作为Pigeland的教练,需要正确选择不参加比赛的猪,使球队的评分最大化。
然而,通过训练和参加比赛,猪的等级可能会改变。作为猪头最好的朋友,你的任务是维持猪的评级以下事件属于三种类型。
— 1lrx :猪头对编号为 l 到 r (含)的每头猪执行与 x 的位与运算,改变其评级。更正式地说,对于所有 l≤i≤r , ai 变成 ai & x 。
-
2sx :猪头将第 s 只猪的评分改为 x 。
-
3lr :猪头在选择编号在 l 到 r 之间的猪(包括)并移除其中一只时,要求获得最大评分。
Input
There is only one test case in each test file.
The first line contains two integers n and q (2≤n≤106, 1≤q≤106) indicating the number of pigs and the number of events.
The second line contains n integers a1,a2,⋯,an (0≤ai<263) where ai indicates the rating of the i-th pig.
For the following q lines, the i-th line first contains an integer opi (opi∈{1,2,3}) indicating the type of the i-th event. If opi=1, then three integers l, r, and x follow (1≤l≤r≤n, 0≤x<263); If opi=2, then two integers s and x follow (1≤s≤n, 0≤x<263); If opi=3, then two integers l and r follow (1≤l<r≤n).
有道 翻译
输入** **
每个测试文件中只有一个测试用例。
第一行包含两个整数 n 和 q ( 2≤n≤106 , 1≤q≤106 ),表示猪的数量和事件的数量。
第二行包含 n 个整数 a1,a2,⋯,an ( 0≤ai<263 ),其中 ai 表示第 i 只猪的评级。
对于接下来的 q 行, i 第一行首先包含一个整数 opi ( opi∈{1,2,3} ),表示 i \第一个事件的类型。如果 opi=1 ,则后面跟着三个整数 l , r 和 x ( 1≤l≤r≤n , 0≤x<263 );如果 opi=2 ,则后面跟着两个整数 s 和 x ( 1≤s≤n , 0≤x<263 );如果 opi=3 ,则后面跟着两个整数 l 和 r 。
Output
For each event of the third type, output one line containing one integer indicating the maximum rating of the team.
有道 翻译
** **输出
对于第三种类型的每个事件,输出一行,其中包含一个整数,表示该队的最大评级。
Example
Input
Copy
5 9
7 7 7 6 7
3 1 5
2 1 3
3 1 5
3 1 3
1 1 2 3
3 1 3
2 2 8
3 1 3
3 1 2
Output
Copy
7 6 7 3 3 8
思路
线段树优化。
代码见下
#include<bits/stdc++.h>
using namespace std;
long long n,q,a[1000006],op,l,r,s,x;
long long te[4000006],te2[4000006],lz[4000006],siz[4000006],xx,bz=(~0ll),oj=(~0ULL);
struct one{
long long a,b;
}ty;
void alz(long long a1,long long v){
te2[a1]&=v;
long long ts=(~v)&((siz[a1]==1)?~0ULL:0);
te[a1]=(te[a1]&v)|ts;
lz[a1]&=v;
}//a1打标记值v
void dow(long long a1){
if(lz[a1]!=bz){
alz(a1*2,lz[a1]);
alz(a1*2+1,lz[a1]);
lz[a1]=bz;
}
}//a1下发标记至儿子
void bu(long long a1,long long l,long long r){
siz[a1]=r-l+1;
lz[a1]=bz;
if(l==r){
te[a1]=(~a[l])&(bz);
te2[a1]=a[l];
return ;
}
long long mid=(l+r)/2;
bu(a1*2,l,mid);
bu(a1*2+1,mid+1,r);
te[a1]=(te2[a1*2]&te[a1*2+1])|(te2[a1*2+1]&te[a1*2]);
te2[a1]=te2[a1*2]&te2[a1*2+1];
return ;
}//l~r建树节点a1
void ci(long long a1,long long l,long long r,long long x,long long y,long long v){
if(l>=x&&r<=y){
alz(a1,v);
return ;
}
long long mid=(l+r)/2;
dow(a1);
if(x<=mid){
ci(a1*2,l,mid,x,y,v);
}
if(mid+1<=y){
ci(a1*2+1,mid+1,r,x,y,v);
}
te[a1]=(te2[a1*2]&te[a1*2+1])|(te2[a1*2+1]&te[a1*2]);
te2[a1]=te2[a1*2]&te2[a1*2+1];
return ;
}//x~y区间加v至l~r区间a1点
void ci2(long long a1,long long l,long long r,long long x,long long v){
if(l==r&&l==x){
te2[a1]=v;
te[a1]=(~v)&bz;
return ;
}
long long mid=(l+r)/2;
dow(a1);
if(x<=mid){
ci2(a1*2,l,mid,x,v);
}
else{
ci2(a1*2+1,mid+1,r,x,v);
}
te[a1]=(te2[a1*2]&te[a1*2+1])|(te2[a1*2+1]&te[a1*2]);
te2[a1]=te2[a1*2]&te2[a1*2+1];
return ;
}//x~y区间加v至l~r区间a1点
one co(long long a1,long long l,long long r,long long x,long long y){
if(x<=l&&r<=y){
return {te2[a1],te[a1]};
}
long long mid=(l+r)/2;
long long xa,xb;
bool xl=0,xr=0;
one tl,tr;
dow(a1);
if(mid>=x){
xl=1;
tl=co(a1*2,l,mid,x,y);
}
if(mid+1<=y){
xr=1;
tr=co(a1*2+1,mid+1,r,x,y);
}
if(xl&&xr){
xa=tl.a&tr.a;
xb=(tl.a&tr.b)|(tr.a&tl.b);
}
if(xl&&!xr){
xa=tl.a;
xb=tl.b;
}
if(!xl&&xr){
xa=tr.a;
xb=tr.b;
}
te[a1]=(te2[a1*2]&te[a1*2+1])|(te2[a1*2+1]&te[a1*2]);
te2[a1]=te2[a1*2]&te2[a1*2+1];
return {xa,xb};
}//x~y区间问至l~r区间a1点
long long co2(long long a1,long long l,long long r,long long x,long long y){
if(te2[a1]>>y&1){
return 0;
}
if(l==r){
return l;
}
long long mid=(l+r)/2,qw;
dow(a1);
te[a1]=(te2[a1*2]&te[a1*2+1])|(te2[a1*2+1]&te[a1*2]);
te2[a1]=te2[a1*2]&te2[a1*2+1];
if(l>=x){
if(te2[a1*2]>>y&1){
return co2(a1*2+1,mid+1,r,x,y);
}
else{
return co2(a1*2,l,mid,x,y);
}
}
else if(mid<=x-1){
return co2(a1*2+1,mid+1,r,x,y);
}
else{
qw=co2(a1*2,l,mid,x,y);
if(!qw){
qw=co2(a1*2+1,mid+1,r,x,y);
}
return qw;
}
}//x~y区间问至l~r区间a1点
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
bu(1,1,n);
for(int i2=1;i2<=q;i2++){
cin>>op;
if(op==1){
cin>>l>>r>>x;
ci(1,1,n,l,r,x);
}
else if(op==2){
cin>>s>>x;
ci2(1,1,n,s,x);
}
else{
cin>>l>>r;
if(l==r){
cout<<0<<endl;
}
else{
ty=co(1,1,n,l,r);
if(ty.b==0){
cout<<ty.a<<endl;
}
else{
for(int i=63;i>=0;i--){
if(ty.b>>i&1){
long long pp=co2(1,1,n,l,i);
long long bz2=bz;
if(l<=pp-1){
bz2&=co(1,1,n,l,pp-1).a;
}
if(pp+1<=r){
bz2&=co(1,1,n,pp+1,r).a;
}
cout<<bz2<<endl;
break;
}
}
}
}
}
}
return 0;
}

浙公网安备 33010602011771号