sparse table

This commit is contained in:
Zengtudor 2024-08-07 23:40:31 +08:00
parent 1770d113bd
commit cd16421688
5 changed files with 73 additions and 31 deletions

4
.gitignore vendored
View File

@ -53,3 +53,7 @@ day5/perfect/perfect
day5/good/good
day5/fair/fair
day5/RMQ/st
day5/RMQ/st_raw
build
.xmake
.vscode

30
.vscode/launch.json vendored
View File

@ -1,30 +0,0 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "gdb",
"request": "launch",
"name": "GDB",
"program": "${workspaceFolder}/${command:AskForProgramName}",
"stopOnEntry": true
},
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(gdb)",
"program": "${fileBasenameNoExtension}",
"cwd": "${workspaceRoot}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/?",
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

7
day5/RMQ/1.in Normal file
View File

@ -0,0 +1,7 @@
5 5
1 2 3 4 5
1 2
2 3
3 4
2 4
4 5

View File

@ -1,7 +1,62 @@
#include<bits/stdc++.h>
using namespace std;
#ifdef OIDEBUG
#define PRINT_ARRAY(v,size){cout<<#v<<" [";for(int i=1;i<=size;i++){cout<<v[i]<<(i!=(size)?",":"]\n");}}
#define PRINT_VALUE(v){cout<<#v<<" :"<<(v)<<endl;}
#define DEBUG_CODE(code){code}
#endif
#ifndef OIDEBUG
#define PRINT_ARRAY(v,size)
#define PRINT_VALUE(v)
#define DEBUG_CODE(code)
#endif
const int MAX_N=1e3+5;
constexpr const int MAX_K=303;
int k,log_2[MAX_N],n,a[MAX_N],s[MAX_N][MAX_K],m;
int main(){
cin>>n>>m;
log_2[1]=0;
for(int i=2;i<=n;i++){
log_2[i]=log_2[i/2]+1;
}
k=log_2[n]+1;
PRINT_VALUE(log_2[3])
PRINT_ARRAY(log_2, n)
for(int i=1;i<=n;i++){
cin>>a[i];
}
PRINT_ARRAY(a, n);
for(int i=1;i<=n;i++){
s[i][0]=a[i];
}
PRINT_VALUE(k);
for(int j=1;j<=k;j++){
PRINT_VALUE(1<<j);
for(int i=1;i+(1<<j)-1<=n;i++){
PRINT_VALUE(i);
PRINT_VALUE(j);
PRINT_VALUE(i+(1<<j))
s[i][j]=min(s[i][j-1],s[i+(j>>1)][j-1]);
PRINT_VALUE(s[i][j]);
}
}
for(int i=1;i<=m;i++){
int r,l;
cin>>l>>r;
PRINT_VALUE(l);
PRINT_VALUE(r);
int j=log_2[r-l+1];
PRINT_VALUE(j);
PRINT_VALUE(r-l+1);
PRINT_VALUE(s[l][j]);
PRINT_VALUE(s[r-(1<<j)+1][j]);
PRINT_VALUE(r-(1<<j)+1);
PRINT_VALUE(1<<j);
cout<<min(s[l][j],s[r-(1<<j)+1][j])<<endl;
}
}

6
xmake.lua Normal file
View File

@ -0,0 +1,6 @@
add_rules("mode.release","mode.debug")
target("st_raw")
set_kind("binary")
add_files("day5/RMQ/st_raw.cpp")
-- add_cxxflags("-DOIDEBUG")
set_rundir("./day5/RMQ")