segment tree fix day5 perfect

This commit is contained in:
Zengtudor 2024-08-09 17:12:44 +08:00
parent 50e1c209b3
commit a836526826
3 changed files with 111 additions and 48 deletions

View File

@ -1,63 +1,63 @@
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAX_N = 3e5+5;
const int MAX_K = 19;
int sMin[MAX_N][MAX_K];
int sMax[MAX_N][MAX_K];
int l2[MAX_N];
int a[MAX_N],tmax[MAX_N*4],tmin[MAX_N*4];
int n;
int ans=INT_MIN;
void build(int tmin[],int tmax[],int a[],int s,int e,int n){
if(s==e){
tmax[n]=tmin[n]=a[s];
return;
}
int m=s+(e-s)/2;
build(tmin, tmax, a, s, m, n*2);
build(tmin, tmax, a, m+1, e, n*2+1);
tmax[n]=max(tmax[n*2],tmax[n*2+1]);
tmin[n]=min(tmin[n*2],tmin[n*2+1]);
}
#ifndef OITEST
#define PRINT_VALUE(v)
#endif
int search_max(int tmax[],int s,int e,int n,int l,int r){
if(l<=s&&e<=r)return tmax[n];
if(e<l||r<s)return LONG_LONG_MIN;
int m=s+(e-s)/2;
int nmax=LONG_LONG_MIN;
if(l<=m)nmax=max(search_max(tmax, s, m, n*2, l, r),nmax);
if(m<r)nmax=max(search_max(tmax, n+1, e, n*2+1, l, r),nmax);
return nmax;
}
int main(int argc,char* argv[]){
#ifdef OITEST
// #define PRINT_VALUE(v){cout<<#v<<" :"<<(v)<<endl;}
// PRINT_VALUE(argv[0])
string fileName="perfect";
assert(argc==2);
string num(argv[1]);
string inputFileName = fileName+num+".in";
string ansFileName = fileName+num+".ans";
auto ansFile = ifstream(ansFileName);
auto inputFile =ifstream(inputFileName);
int correctAns;
ansFile>>correctAns;
#define cin inputFile
#endif
cin.sync_with_stdio(false);
int search_min(int tmin[],int s,int e,int n,int l,int r){
if(l<=s&&e<=r)return tmin[n];
if(e<l||r<s)return LONG_LONG_MAX;
int m=s+(e-2)/2;
int nmin=LONG_LONG_MAX;
if(l<=m)nmin=min(search_max(tmax, s, m, n*2, l, r),nmin);
if(m<r)nmin=min(search_max(tmax, m+1, e, n*2+1, l, r),nmin);
return nmin;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
l2[0]=0;
l2[1]=0;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>sMin[i][0];
sMax[i][0]=sMin[i][0];
cin>>a[i];
}
for(int i=2;i<=n;i++){
l2[i]=l2[i/2]+1;
}
int k=l2[n]+1;
for(int j=1;j<=k;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
sMax[i][j]=max(sMax[i][j-1],sMax[i+(1<<(j-1))][j-1]);
sMin[i][j]=min(sMin[i][j-1],sMin[i+(1<<(j-1))][j-1]);
}
}
int ans=INT_MIN;
build(tmin, tmax, a, 1, n, 1);
for(int l=1;l<=n;l++){
for(int r=l;r<=n;r++){
int j=l2[r-l+1];
int maxlr=max(sMax[l][j],sMax[r-(1<<j)+1][j]);
int minlr=min(sMin[l][j],sMin[r-(1<<j)+1][j]);
ans=max(ans,maxlr^minlr);
ans=max(
search_max(tmax, 1, n, 1, l, r)
^
search_min(tmin, 1, n, 1, l, r)
,
ans
);
}
}
cout<<ans<<endl;
#ifdef OITEST
assert(ans==correctAns);
#endif
}

63
day5/perfect/fix.cpp.old Normal file
View File

@ -0,0 +1,63 @@
#include<bits/stdc++.h>
using namespace std;
const int MAX_N = 3e5+5;
const int MAX_K = 19;
int sMin[MAX_N][MAX_K];
int sMax[MAX_N][MAX_K];
int l2[MAX_N];
#ifndef OITEST
#define PRINT_VALUE(v)
#endif
int main(int argc,char* argv[]){
#ifdef OITEST
// #define PRINT_VALUE(v){cout<<#v<<" :"<<(v)<<endl;}
// PRINT_VALUE(argv[0])
string fileName="perfect";
assert(argc==2);
string num(argv[1]);
string inputFileName = fileName+num+".in";
string ansFileName = fileName+num+".ans";
auto ansFile = ifstream(ansFileName);
auto inputFile =ifstream(inputFileName);
int correctAns;
ansFile>>correctAns;
#define cin inputFile
#endif
cin.sync_with_stdio(false);
cin.tie(0);
l2[0]=0;
l2[1]=0;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>sMin[i][0];
sMax[i][0]=sMin[i][0];
}
for(int i=2;i<=n;i++){
l2[i]=l2[i/2]+1;
}
int k=l2[n]+1;
for(int j=1;j<=k;j++){
for(int i=1;i+(1<<j)-1<=n;i++){
sMax[i][j]=max(sMax[i][j-1],sMax[i+(1<<(j-1))][j-1]);
sMin[i][j]=min(sMin[i][j-1],sMin[i+(1<<(j-1))][j-1]);
}
}
int ans=INT_MIN;
for(int l=1;l<=n;l++){
for(int r=l;r<=n;r++){
int j=l2[r-l+1];
int maxlr=max(sMax[l][j],sMax[r-(1<<j)+1][j]);
int minlr=min(sMin[l][j],sMin[r-(1<<j)+1][j]);
ans=max(ans,maxlr^minlr);
}
}
cout<<ans<<endl;
#ifdef OITEST
assert(ans==correctAns);
#endif
}

View File

@ -31,8 +31,8 @@ target("fperfect")
add_files("./day5/perfect/fix.cpp")
set_rundir("./day5/perfect/")
for val=1,4 do
local strval = tostring(val)
add_tests(strval,{files="./day5/perfect/fix.cpp",defines="OITEST",runargs=strval,run_timeout=1000})
local s = tostring(val)
add_tests(s,{files="./day5/perfect/fix.cpp",defines="OITEST",runargs={"perfect"..s..".in","perfect"..s..".ans"},run_timeout=1000})
end
target("pre88")