QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#820427 | #4619. Fast Bubble Sort | SGColin | AC ✓ | 227ms | 17892kb | C++20 | 1.8kb | 2024-12-18 21:24:57 | 2024-12-18 21:24:57 |
Judging History
answer
#include<cstdio>
#include<cctype>
#include<algorithm>
using namespace std;
const int maxn=100000,LOG=16;
int te,n,Q,a[maxn+5];
int ST[LOG+1][maxn+5],sum[LOG+1][maxn+5];
int c[maxn+5];
#define EOLN(x) ((x)==10 || (x)==13 || (x)==EOF)
inline char readc(){
static char buf[100000],*l=buf,*r=buf;
return l==r && (r=(l=buf)+fread(buf,1,100000,stdin),l==r)?EOF:*l++;
}
template<typename T> int readi(T &x){
T tot=0;char ch=readc(),lst='+';
while (!isdigit(ch)) {if (ch==EOF) return EOF;lst=ch;ch=readc();}
while (isdigit(ch)) tot=(tot<<3)+(tot<<1)+(ch^48),ch=readc();
lst=='-'?x=-tot:x=tot;return EOLN(ch);
}
struct fastO{
int si;char buf[100000];
fastO() {si=0;}
void putc(char ch){
if (si==100000) fwrite(buf,1,si,stdout),si=0;
buf[si++]=ch;
}
~fastO() {fwrite(buf,1,si,stdout);}
}fo;
template<typename T> void writei(T x,char ch='\n'){
int len=0,buf[100];
if (x<0) fo.putc('-'),x=-x;
do buf[len++]=x%10,x/=10; while (x);
while (len) fo.putc(buf[--len]+48);
if (ch) fo.putc(ch);
}
inline void Add(int x,int y) {for (;x<=n;x+=x&-x) c[x]=min(c[x],y);}
inline int Min(int x) {int MIN=n+1;for (;x;x-=x&-x) MIN=min(MIN,c[x]);return MIN;}
int main(){
for (readi(te);te;te--){
readi(n);readi(Q);
for (int i=1;i<=n;i++) readi(a[i]);
for (int j=0;j<=LOG;j++) ST[j][n+1]=n+1,sum[j][n+1]=0;
for (int i=1;i<=n;i++) c[i]=n+1;
for (int i=n;i;i--){
ST[0][i]=Min(n-a[i]+1);
sum[0][i]=(ST[0][i]>i+1);
Add(n-a[i]+1,i);
}
for (int j=1;j<=LOG;j++)
for (int i=1;i<=n;i++)
ST[j][i]=ST[j-1][ST[j-1][i]],
sum[j][i]=sum[j-1][i]+sum[j-1][ST[j-1][i]];
for (int t=1,x,y;t<=Q;t++){
readi(x);readi(y);
int ans=0;
for (int j=LOG;~j;j--) if (ST[j][x]<=y) ans+=sum[j][x],x=ST[j][x];
if (x<y) ans++;
printf("%d\n",ans);
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 227ms
memory: 17892kb
input:
10 100000 100000 94437 49936 62522 1166 33452 90618 91436 81173 99242 37280 46125 44869 52811 45694 11550 29857 48941 56538 35057 40907 79925 70785 29658 19044 32804 11428 4127 92197 12680 61636 27200 56790 83389 80340 54411 15308 38952 78085 81397 37554 44186 33581 35662 73331 84991 32044 69012 822...
output:
4 7 10 16 15 8 2 9 7 8 6 13 11 13 9 9 9 5 5 8 10 11 10 17 14 6 7 5 17 8 13 17 6 12 8 13 13 12 12 10 6 8 19 10 10 19 17 8 12 5 11 20 12 7 17 5 9 12 10 8 15 6 9 9 10 6 10 9 7 16 8 7 12 11 15 16 7 6 10 11 10 7 6 11 11 11 10 9 8 11 5 15 5 6 11 3 11 13 8 13 7 15 14 6 13 10 11 10 8 10 12 3 6 6 16 11 9 9 1...
result:
ok 1000000 lines