Submission #65693330
Source Code Expand
Copy
#include <bits/stdc++.h>using namespace std;using ll = long long;const int MAXN = 250000;const int MOD = 998244353;ll modpow(ll a, ll e=MOD-2){ll r=1;while(e){if(e&1) r=r*a%MOD;a=a*a%MOD;e>>=1;}return r;}inline ll hilbert(int x, int y, int pow=18, ll rot=0){if(pow==0) return 0;int h = 1<<(pow-1);int seg = ((x<h)?0:1) + ((y<h)?0:2);seg = (seg+rot)&3;
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 250000;
const int MOD = 998244353;
ll modpow(ll a, ll e=MOD-2){
ll r=1;
while(e){
if(e&1) r=r*a%MOD;
a=a*a%MOD;
e>>=1;
}
return r;
}
inline ll hilbert(int x, int y, int pow=18, ll rot=0){
if(pow==0) return 0;
int h = 1<<(pow-1);
int seg = ((x<h)?0:1) + ((y<h)?0:2);
seg = (seg+rot)&3;
static const ll dr[4]={3,0,0,1};
int nx = x&(h-1), ny = y&(h-1);
ll sub = hilbert(nx,ny,pow-1,(rot+dr[seg])&3);
return seg*(1LL<<(2*pow-2)) + ((seg&1)?sub:((1LL<<(2*pow-2))-sub-1));
}
int N,Q;
int A[MAXN+2], cnt[MAXN+2];
vector<int> pos[MAXN+2];
ll fact[MAXN+2], inv_fact[MAXN+2];
struct Qry{int l,r,x,id; ll ord;} qs[MAXN+2];
ll ans[MAXN+2];
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>N>>Q;
for(int i=1;i<=N;i++){
cin>>A[i];
pos[A[i]].push_back(i);
}
fact[0]=1;
for(int i=1;i<=N;i++) fact[i]=fact[i-1]*i%MOD;
inv_fact[N]=modpow(fact[N]);
for(int i=N;i>=1;i--) inv_fact[i-1]=inv_fact[i]*i%MOD;
for(int i=0;i<Q;i++){
cin>>qs[i].l>>qs[i].r>>qs[i].x;
qs[i].id=i;
qs[i].ord = hilbert(qs[i].l, qs[i].r);
}
sort(qs, qs+Q, [&](auto &a, auto &b){ return a.ord<b.ord; });
int L=1, R=0, X=1, sz=0;
ll denom=1;
auto add = [&](int v){
int c=cnt[v]++;
denom = denom * inv_fact[c+1] % MOD * fact[c] % MOD;
sz++;
};
auto rem = [&](int v){
int c=cnt[v]--;
denom = denom * inv_fact[c-1] % MOD * fact[c] % MOD;
sz--;
};
for(int i=0;i<Q;i++){
auto &q=qs[i];
while(X<q.x){
for(int p:pos[X]) if(L<=p&&p<=R) add(X);
X++;
}
while(X>q.x){
X--;
for(int p:pos[X]) if(L<=p&&p<=R) rem(X);
}
while(L>q.l) if(A[--L]<X) add(A[L]);
while(R<q.r) if(A[++R]<X) add(A[R]);
while(L<q.l) if(A[L++]<X) rem(A[L-1]);
while(R>q.r) if(A[R--]<X) rem(A[R+1]);
ans[q.id] = fact[sz] * denom % MOD;
}
for(int i=0;i<Q;i++) cout<<ans[i]<<"\n";
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | G - Range Shuffle Query |
| User | OYU__0YU |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 2258 Byte |
| Status | TLE |
| Exec Time | 2212 ms |
| Memory | 30788 KiB |
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 625 | ||||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | 00_sample_00.txt, 00_sample_01.txt |
| All | 00_sample_00.txt, 00_sample_01.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 03_anti_fenwick_00.txt, 03_anti_fenwick_01.txt, 03_anti_fenwick_02.txt, 03_anti_fenwick_03.txt, 03_anti_fenwick_04.txt, 03_anti_fenwick_05.txt, 03_anti_fenwick_06.txt, 03_anti_fenwick_07.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| 00_sample_00.txt | AC | 2 ms | 3516 KiB |
| 00_sample_01.txt | AC | 2 ms | 3424 KiB |
| 01_random_00.txt | TLE | 2208 ms | 23928 KiB |
| 01_random_01.txt | TLE | 2208 ms | 23864 KiB |
| 01_random_02.txt | TLE | 2209 ms | 27612 KiB |
| 01_random_03.txt | TLE | 2209 ms | 25604 KiB |
| 01_random_04.txt | TLE | 2209 ms | 25508 KiB |
| 01_random_05.txt | TLE | 2209 ms | 27620 KiB |
| 01_random_06.txt | TLE | 2209 ms | 24136 KiB |
| 02_corner_00.txt | AC | 114 ms | 30788 KiB |
| 02_corner_01.txt | AC | 78 ms | 16924 KiB |
| 02_corner_02.txt | AC | 75 ms | 16992 KiB |
| 03_anti_fenwick_00.txt | TLE | 2208 ms | 16960 KiB |
| 03_anti_fenwick_01.txt | TLE | 2208 ms | 16984 KiB |
| 03_anti_fenwick_02.txt | TLE | 2209 ms | 25924 KiB |
| 03_anti_fenwick_03.txt | TLE | 2209 ms | 25820 KiB |
| 03_anti_fenwick_04.txt | TLE | 2208 ms | 17088 KiB |
| 03_anti_fenwick_05.txt | TLE | 2208 ms | 17240 KiB |
| 03_anti_fenwick_06.txt | TLE | 2212 ms | 17572 KiB |
| 03_anti_fenwick_07.txt | TLE | 2208 ms | 17852 KiB |