Submission #65692386
Source Code Expand
Copy
#include <bits/stdc++.h>using namespace std;using ll = long long;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;}int main(){ios::sync_with_stdio(false);cin.tie(nullptr);int N, Q;cin >> N >> Q;vector<int> A(N+1);
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
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;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> A(N+1);
for(int i = 1; i <= N; i++) {
cin >> A[i];
}
vector<ll> fact(N+1), inv_fact(N+1);
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;
vector<vector<int>> pos(N+1);
for(int i = 1; i <= N; i++){
pos[A[i]].push_back(i);
}
struct Query { int l, r, x, id; };
vector<Query> qs(Q);
for(int i = 0; i < Q; i++){
cin >> qs[i].l >> qs[i].r >> qs[i].x;
qs[i].id = i;
}
int B = max(1, (int)pow(N, 2.0/3));
sort(qs.begin(), qs.end(), [&](auto &a, auto &b){
int ab = a.l / B, bb = b.l / B;
if(ab != bb) return ab < bb;
int ar = a.r / B, br = b.r / B;
if(ar != br) return ar < br;
return a.x < b.x;
});
vector<ll> ans(Q);
vector<int> cnt(N+1, 0);
ll curMul = 1;
int curSize = 0;
int L = 1, R = 0, X = 1;
auto add_val = [&](int v){
int old = cnt[v], nw = old + 1;
curMul = curMul * inv_fact[nw] % MOD * fact[old] % MOD;
cnt[v] = nw;
curSize++;
};
auto rem_val = [&](int v){
int old = cnt[v], nw = old - 1;
curMul = curMul * inv_fact[nw] % MOD * fact[old] % MOD;
cnt[v] = nw;
curSize--;
};
auto add_pos = [&](int i){
int v = A[i];
if(v < X) add_val(v);
};
auto rem_pos = [&](int i){
int v = A[i];
if(v < X) rem_val(v);
};
auto incX = [&](){
for(int p: pos[X]){
if(L <= p && p <= R) add_val(X);
}
X++;
};
auto decX = [&](){
X--;
for(int p: pos[X]){
if(L <= p && p <= R) rem_val(X);
}
};
for(auto &q: qs){
while(X < q.x) incX();
while(X > q.x) decX();
while(L > q.l) add_pos(--L);
while(R < q.r) add_pos(++R);
while(L < q.l) rem_pos(L++);
while(R > q.r) rem_pos(R--);
ans[q.id] = fact[curSize] * curMul % MOD;
}
for(ll v: ans){
cout << v << "\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 | 2604 Byte |
| Status | TLE |
| Exec Time | 2208 ms |
| Memory | 28600 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 | 1 ms | 3772 KiB |
| 00_sample_01.txt | AC | 1 ms | 3684 KiB |
| 01_random_00.txt | TLE | 2208 ms | 22148 KiB |
| 01_random_01.txt | TLE | 2208 ms | 23192 KiB |
| 01_random_02.txt | TLE | 2208 ms | 25968 KiB |
| 01_random_03.txt | TLE | 2208 ms | 23744 KiB |
| 01_random_04.txt | TLE | 2208 ms | 24320 KiB |
| 01_random_05.txt | TLE | 2208 ms | 25980 KiB |
| 01_random_06.txt | TLE | 2208 ms | 22764 KiB |
| 02_corner_00.txt | AC | 142 ms | 28600 KiB |
| 02_corner_01.txt | AC | 80 ms | 21860 KiB |
| 02_corner_02.txt | AC | 80 ms | 21992 KiB |
| 03_anti_fenwick_00.txt | AC | 1358 ms | 21864 KiB |
| 03_anti_fenwick_01.txt | TLE | 2208 ms | 22160 KiB |
| 03_anti_fenwick_02.txt | TLE | 2208 ms | 24000 KiB |
| 03_anti_fenwick_03.txt | TLE | 2208 ms | 24140 KiB |
| 03_anti_fenwick_04.txt | TLE | 2208 ms | 22204 KiB |
| 03_anti_fenwick_05.txt | TLE | 2208 ms | 22344 KiB |
| 03_anti_fenwick_06.txt | TLE | 2207 ms | 22564 KiB |
| 03_anti_fenwick_07.txt | TLE | 2208 ms | 22820 KiB |