QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#709773 | #9325. Random Permutation | lytqwq | ML | 397ms | 114912kb | C++14 | 2.8kb | 2024-11-04 16:45:05 | 2024-11-04 16:45:05 |
Judging History
answer
#include<bits/stdc++.h>
#include<ext/pb_ds/hash_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#define vectorwyx maze
namespace vectorwyx{
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define sml(x,y) (x=min(x,y))
#define big(x,y) (x=max(x,y))
#define ll long long
#define uint unsigned
#define ull unsigned long long
#define umap unordered_map
#define db double
#define fo(i,x,y) for(int i=(x);i<=(y);++i)
#define go(i,x,y) for(int i=(x);i>=(y);--i)
#define ptc putchar
#define gtc getchar
#define emp emplace
#define re return
#define co continue
#define brk break
#define HH (ptc('\n'))
#define bctz __builtin_ctz
#define bclz __builtin_clz
#define bppc __builtin_popcount
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
ll seed=chrono::system_clock::now().time_since_epoch().count();
mt19937 rnd(seed);
inline int rm(int x,int y){return (int)(rnd()%(y-x+1ll)+x);}
inline int read(){signed ch=getchar();int x=0,f=1;while(!isdigit(ch)){if(ch==(int)('-'))f=-1;ch=getchar();}while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
template<typename T> void out(T *a,int l,int r){fo(i,l,r) cout<<*(a+i)<<' ';puts("");}
const int N=2e5+5,mod=998244353;
#define modS(x) x=x>=mod?x-mod:x
int a[N],n,lg[N];
pii st[21][N];
// umap<int,int> f[N];
cc_hash_table<int,int> f[N],Sl[N],Sr[N];
int F(int l,int r);
int Sum_l(int l,int r);
int Sum_r(int l,int r);
void solve(){
cin>>n;
fo(i,1,n) a[i]=read(),st[0][i]=mk(a[i],i),f[i].clear(),Sl[i].clear(),Sr[i].clear();
fo(i,1,lg[n]) fo(j,1,n) st[i][j]=min(st[i-1][j],st[i-1][min(j+(1<<(i-1)),n)]);
cout<<F(1,n)<<'\n';
}
signed main(){
fo(i,2,N-5) lg[i]=lg[i>>1]+1;
int T=read();
while(T--) solve();
return 0;
}
int rmq(int l,int r){
int w=lg[r-l+1];
re min(st[w][l],st[w][r-(1<<w)+1]).se;
}
int F(int l,int r){
// printf("F(%d,%d)\n",l,r);
if(l==r) re 1;
if(f[l][r]) re f[l][r];
int mid=rmq(l,r);
if(l==mid) f[l][r]=1+Sum_l(mid+1,r);
else if(r==mid) f[l][r]=1+Sum_r(l,mid-1);
else f[l][r]=1+Sum_l(mid+1,r)+Sum_r(l,mid-1);
modS(f[l][r]);
re f[l][r];
// if(mid==l) f[l][r]=F(l,r-1)+F(l+1,r);
// else f[l][r]=F(l+1,r)+F(l,mid-1);
// modS(f[l][r]);
// re f[l][r];
}
int Sum_l(int l,int r){
if(l==r) re 1;
if(Sl[l][r]) re Sl[l][r];
Sl[l][r]=F(l,r)+Sum_l(l,r-1);
modS(Sl[l][r]);
re Sl[l][r];
}
int Sum_r(int l,int r){
if(l==r) re 1;
if(Sr[l][r]) re Sr[l][r];
Sr[l][r]=F(l,r)+Sum_r(l+1,r);
modS(Sr[l][r]);
re Sr[l][r];
}
}
/*
4
5
4 3 5 1 2
5
5 3 1 2 4
2
2 1
3
1 3 2
-------------------------------------------------
*/
signed main(){re vectorwyx::main();}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 45ms
memory: 114220kb
input:
4 5 4 3 5 1 2 5 5 3 1 2 4 2 2 1 3 1 3 2
output:
8 7 2 4
result:
ok 4 number(s): "8 7 2 4"
Test #2:
score: 0
Accepted
time: 34ms
memory: 113652kb
input:
5 2 1 2 4 2 1 4 3 3 3 2 1 2 1 2 3 3 1 2
output:
2 5 4 2 3
result:
ok 5 number(s): "2 5 4 2 3"
Test #3:
score: 0
Accepted
time: 32ms
memory: 112628kb
input:
332 4 3 2 4 1 3 3 2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 20 21 161 8 2 4 31 4 14 98 5 11 5 23 9 58 22 36 39 48 7 23 9 8 10 3 9 2 32 12 13 3 133 33 27 8 17 8 13 36 15 30 79 4 118 20 3 2 78 2 277 58 4 2 15 55 7 8 39 51 4 5 19 72 28 3 5 4 7 6...
result:
ok 166 numbers
Test #5:
score: 0
Accepted
time: 40ms
memory: 113936kb
input:
92 3 1 2 3 13 1 7 12 5 10 8 13 11 9 4 6 3 2 8 7 6 2 5 8 1 3 4 6 6 2 5 1 3 4 5 2 1 5 4 3 20 16 19 18 2 20 14 3 9 11 8 5 1 10 15 4 12 13 17 7 6 17 13 3 2 1 14 5 15 12 10 9 17 11 8 6 4 16 7 10 7 1 10 6 8 9 5 4 2 3 15 2 11 8 9 7 5 4 12 13 15 14 6 3 1 10 16 3 1 14 6 4 2 10 16 9 15 13 8 11 12 7 5 5 4 1 3 ...
output:
4 1241 23 10 9 363 1783 178 1579 824 8 16 34 467 382 392 1960 680 46 490 2812 45 144 99 116 965 99 1710 83 18 392 163 277 142 3 2 199 4 56 2 476 188 44 17 28 552 41 2 2 32 200 37 5 2 746 3 770 57 1006 9 21 11 1387 99 21 55 1421 730 15 7 103 2 2 93 47 96 72 2464 828 279 27 30 45 39 15 49 3110 93 10 3...
result:
ok 92 numbers
Test #6:
score: 0
Accepted
time: 62ms
memory: 113860kb
input:
28548 5 4 2 5 3 1 2 1 2 3 1 3 2 5 4 2 3 1 5 2 1 2 4 4 1 2 3 3 1 2 3 3 3 1 2 4 1 4 3 2 3 3 1 2 4 4 3 2 1 4 2 3 1 4 5 2 1 3 4 5 3 1 2 3 2 2 1 2 1 2 2 2 1 4 3 2 4 1 2 1 2 3 2 1 3 3 1 3 2 5 5 3 1 4 2 5 4 3 5 2 1 4 2 1 3 4 5 3 4 5 2 1 3 2 1 3 5 3 1 2 5 4 5 1 3 5 2 4 5 1 2 5 4 3 5 4 1 2 5 3 3 2 1 3 5 1 5 ...
output:
13 2 4 8 2 5 4 3 8 3 8 5 9 4 2 2 2 7 2 3 4 7 15 5 16 3 9 13 16 9 3 12 3 4 16 7 7 3 2 3 7 2 4 3 7 4 4 4 8 13 16 4 4 4 7 4 5 7 7 16 7 4 5 12 3 12 2 5 9 2 7 8 9 2 5 2 5 4 8 2 3 2 4 8 3 3 2 2 3 2 5 4 5 2 2 2 5 2 3 5 2 7 4 2 7 4 2 7 8 2 3 5 2 2 5 4 2 8 5 2 3 5 7 2 2 3 4 7 7 3 2 2 5 5 2 3 9 2 13 8 5 9 4 8...
result:
ok 28548 numbers
Test #7:
score: 0
Accepted
time: 72ms
memory: 113612kb
input:
13361 9 9 6 7 8 2 5 1 4 3 8 4 8 5 7 6 2 3 1 7 1 2 4 3 5 6 7 7 4 6 7 1 3 5 2 9 9 5 3 8 7 4 6 2 1 8 4 3 6 2 7 8 5 1 6 2 1 3 6 4 5 8 7 3 4 2 5 1 8 6 8 6 2 5 1 8 4 3 7 7 3 2 5 1 6 4 7 5 3 4 2 5 1 7 3 5 1 2 7 4 6 8 8 4 5 1 2 7 3 6 7 1 3 7 5 2 6 4 9 3 2 7 9 5 8 6 4 1 5 2 5 4 1 3 9 9 3 1 2 7 6 4 5 8 10 1 8...
output:
38 61 48 15 146 50 16 23 19 13 12 18 21 36 130 9 52 143 67 17 45 74 11 21 70 38 15 53 11 26 35 31 8 52 34 16 27 40 11 23 32 12 32 38 64 23 79 15 21 69 17 17 15 28 159 61 50 65 35 51 32 46 8 82 46 214 32 50 36 91 70 33 23 28 20 15 54 32 35 24 31 26 21 17 16 9 7 21 30 58 19 99 9 14 115 61 19 71 13 223...
result:
ok 13361 numbers
Test #8:
score: 0
Accepted
time: 142ms
memory: 114540kb
input:
667 173 161 83 56 53 140 121 5 136 39 9 48 122 171 67 123 79 8 78 92 130 145 85 103 153 168 70 165 166 30 62 11 96 14 100 66 13 133 32 18 20 25 132 87 126 69 142 55 120 60 105 22 101 26 45 33 80 128 127 107 82 81 172 169 167 135 41 71 89 40 119 59 154 129 114 93 139 27 162 31 63 118 157 117 38 52 95...
output:
583596139 584020170 471598730 739812209 116431625 722912939 184047388 12959806 37590670 167680609 87355140 961692527 903322137 248925145 650084912 914516987 289517842 300879479 207183975 262390037 225944732 134224253 740449198 768041290 17348915 726661844 733916897 358513219 471853816 755322624 1164...
result:
ok 667 numbers
Test #9:
score: 0
Accepted
time: 199ms
memory: 114288kb
input:
209 619 83 21 64 495 220 9 560 88 553 585 284 342 337 158 98 597 4 437 251 210 165 385 61 74 22 2 137 470 270 8 536 351 375 376 177 366 101 490 576 231 16 217 310 614 399 491 149 362 230 126 15 410 546 363 300 539 80 603 235 404 562 368 442 371 258 377 429 354 505 424 14 62 157 475 214 182 131 569 5...
output:
855992658 367026702 431800434 142191123 391617963 485308070 122398054 776250649 894154628 903985773 256203323 391558634 139075151 462471709 524355604 354340488 966695180 3933957 780375111 5768 678820611 61518971 112453740 104852252 171591774 202816532 240147595 437398882 879685262 484 930889179 9055...
result:
ok 209 numbers
Test #10:
score: 0
Accepted
time: 397ms
memory: 114912kb
input:
396 605 239 221 33 196 164 452 291 411 211 298 558 578 62 43 141 84 247 47 220 13 559 444 330 552 254 71 362 97 167 481 206 424 74 119 203 111 413 171 351 6 288 195 135 434 91 581 226 599 576 526 42 312 23 95 24 9 11 563 73 170 498 592 334 485 81 546 27 159 453 58 587 96 229 535 594 15 225 569 156 3...
output:
851397519 14197112 93073453 245922039 607161943 346953197 1941681 636713992 275782906 919582671 414981911 80334661 171750972 615441593 226220372 591309040 585300324 938611752 696956754 55894365 439181693 60695373 907628533 895768785 899204917 21272207 964271104 188914371 787802798 451701420 12269012...
result:
ok 396 numbers
Test #11:
score: -100
Memory Limit Exceeded
input:
1 200000 22615 127709 117908 176454 85854 188189 130909 22945 89774 111769 171103 149965 33152 105768 138968 118006 131227 25021 70289 190599 189803 161484 143425 155923 150567 161783 4292 15618 138979 154825 74802 100143 174639 151972 74186 108745 91114 198721 77415 52988 154864 53550 120930 92176 ...
output:
802841307