QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#136783#242. RMQ Similar SequenceRd_rainydays#100 ✓500ms144500kbC++20929b2023-08-09 11:22:402023-08-09 11:22:44

Judging History

This is the latest submission verdict.

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-09 11:22:44]
  • Judged
  • Verdict: 100
  • Time: 500ms
  • Memory: 144500kb
  • [2023-08-09 11:22:40]
  • Submitted

answer

#include<bits/stdc++.h>
using namespace std;

#define REP(i,a,b) for(int i=(a),i##_end_=(b);i<i##_end_;++i)

static const int M=1000003;
static const int Mod=1000000007;
#define ll long long

int T,n,Inv[M],A[M],B[M][21];
int Lg[M];
int Max(int a,int b){
  return A[a]>=A[b]?a:b;
}
ll Work(int l,int r){
  if(l>=r)return 1;
  int t=Lg[r-l+1];
  int u=Max(B[l][t],B[r-(1<<t)+1][t]);
  return 1ll*Inv[r-l+1]*Work(l,u-1)%Mod*Work(u+1,r)%Mod;
}
int main(){
  
  Inv[0]=Inv[1]=1;
  REP(i,2,M)
    Inv[i]=1ll*(Mod-Mod/i)*Inv[Mod%i]%Mod;
  REP(i,2,M)Lg[i]=Lg[i>>1]+1;

  scanf("%d",&T);
  while(T--){
    scanf("%d",&n);
    REP(i,1,n+1)
      scanf("%d",&A[i]),B[i][0]=i;

    //cout<<"****"<<endl;
    REP(j,1,21)REP(i,1,n+1)if(i+(1<<j-1)<=n)
      B[i][j]=Max(B[i][j-1],B[i+(1<<j-1)][j-1]);
    
    //cout<<"****"<<endl;
    printf("%lld\n",1ll*Work(1,n)*n%Mod*Inv[2]%Mod);
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 500ms
memory: 144500kb

input:

11119
1
1
2
1 2
3
1 1 3
4
3 1 1 2
5
5 5 2 4 2
6
2 6 4 4 3 3
7
3 1 1 3 5 5 4
8
4 8 4 7 1 1 7 6
9
9 3 6 3 7 6 1 7 6
10
5 1 10 2 5 1 7 7 4 1
10
4 5 4 5 10 1 5 1 2 5
10
1 2 3 6 2 9 9 7 6 2
10
2 8 3 5 2 4 10 4 8 4
10
2 9 10 3 1 1 2 10 7 7
10
5 7 7 8 10 6 4 3 10 6
10
1 8 9 3 2 4 2 5 5 9
10
4 7 9 2 5 10 10...

output:

500000004
500000004
250000002
83333334
41666667
520833337
760416672
760416672
190104168
1984127
952083340
650694449
253472224
625744052
301388891
625744052
952083340
63368056
387723217
462574408
500992067
835648154
877232149
476041670
31684028
354456021
877232149
916832017
559076007
650694449
126736...

result:

ok 11119 lines