QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#30432#242. RMQ Similar SequencesrfAC ✓470ms120764kbC++111.1kb2022-04-28 14:47:542022-04-28 14:48:50

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.
  • [2022-04-28 14:48:50]
  • Judged
  • Verdict: AC
  • Time: 470ms
  • Memory: 120764kb
  • [2022-04-28 14:47:54]
  • Submitted

answer

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

inline void read(int &x){
	static char c; x = 0,c = getchar();
	while (!isdigit(c)) c = getchar();
	while (isdigit(c)) x = x * 10 + c - '0',c = getchar();
}

const int P = 1e9 + 7,N = 1000050;
int n,a[N],st[N][20],Log[N];
long long ans;

int power(int x,int y){
	static long long res;
	res = 1;
	while (y){ if (y&1) res = res * x % P; x = 1ll*x*x%P; y>>=1; } return res;
}
void dfs(int l,int r){
	int t = Log[r-l+1],x = st[l][t],y = st[r-(1<<t)+1][t];
	int z = (a[x] != a[y]) ? ((a[x] > a[y])?x:y) : min(x,y);
	if (l<z) dfs(l,z-1); if (z<r) dfs(z+1,r);
	ans = ans * (r - l + 1ll) % P;
}

void solve(){
	int i,j,x,y;
	read(n);
	for (i = 2; i <= n; ++i) Log[i] = Log[i >> 1] + 1;
	for (i = 1; i <= n; ++i) read(a[i]),st[i][0] = i;
	for (i = 1; i <= Log[n]; ++i)
	for (j = 1; j+(1<<i)-1 <= n; ++j){
		x = st[j][i-1],y = st[j + (1<<(i-1))][i-1];
		st[j][i] = (a[x] != a[y]) ? ((a[x] > a[y])?x:y) : min(x,y);
	}
	ans = 1;
	dfs(1,n);
	ans = power(ans,P-2);
	ans = ans * n % P * (P+1>>1) % P;
	cout << ans << '\n'; 
}

int main(){
	int T;
	read(T);
	while (T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 470ms
memory: 120764kb

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