#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#pragma GCC optimize("Ofast,unroll-loops,fast-math")
#pragma GCC target("popcnt")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define pb push_back
const int mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353; // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const int N = ll(5e4)+100;
const int M = ll(2e5) + 100;
const long long inf = 2e18;
const long double eps = 1e-15L;
ll lcm(ll a, ll b) { return (a / __gcd(a,b))*b; }
ll binpow(ll a, ll b, ll m) { ll res=1; a %= m; while(b>0){ if(b&1)res=(res * a) % m; a=(a * a) % m; b/=2; } return res%m;}
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
int n, k, q, a[N], ql[M], qr[M], ans[M], beg[2][25][25], en[2][25][25], sum1[N][25], sum2[N][25];
void countleft(int tl, int tr) {
beg[0][a[tr]][a[tr]] = 1;
sum1[tr][a[tr]] = 1;
for(int i = tr - 1; i >= tl; i --) {
for(int x = 1; x <= k; x ++) for(int y = 1; y <= k; y ++) (beg[1][x][y] = beg[0][x][y]) %= mod;
beg[1][a[i]][a[i]] ++;
for(int y = a[i]; y <= k; y ++) {
for(int x = a[i]; x <= y; x ++) (beg[1][a[i]][y] += beg[0][x][y]) %= mod;
}
for(int x = 1; x <= k; x ++) {
sum1[i][x] = 0;
for(int y = 1; y <= k; y ++) (sum1[i][x] += beg[1][y][x]) %= mod, beg[0][x][y] = beg[1][x][y];
}
}
}
void countright(int tl, int tr) {
en[0][a[tl]][a[tl]] = 1;
sum2[tl][a[tl]] = 1;
for(int i = tl + 1; i <= tr; i ++) {
for(int x = 1; x <= k; x ++) for(int y = 1; y <= k; y ++) (en[1][x][y] = en[0][x][y]);
// we take a_i
en[1][a[i]][a[i]] ++;
for(int x = 1; x <= a[i]; x ++) {
for(int y = x; y <= a[i]; y ++) (en[1][x][a[i]] += en[0][x][y]) %= mod;
}
for(int x = 1; x <= k; x ++) {
sum2[i][x] = 0;
for(int y = 1; y <= k; y ++) (sum2[i][x] += en[1][x][y]) %= mod, en[0][x][y] = en[1][x][y];
}
}
}
void solve(int tl, int tr, vector<int>& qu) {
if(tl == tr) {
for(auto i : qu) ans[i] = 1;
return;
}
int mid = (tl + tr) / 2;
for(int x = 1; x <= k; x ++) {
sum1[mid][x] = sum2[mid + 1][x] = 0;
for(int y = 1; y <= k; y ++) {
beg[0][x][y] = 0;
en[0][x][y] = 0;
}
}
countleft(tl, mid);
countright(mid + 1, tr);
vector<int> qul, qur;
for(auto id : qu) {
if(qr[id] <= mid) qul.pb(id);
else if(mid + 1 <= ql[id]) qur.pb(id);
else {
for(int k1 = 1; k1 <= k; k1 ++) {
ans[id] = (ans[id] + sum1[ql[id]][k1]) % mod;
ans[id] = (ans[id] + sum2[qr[id]][k1]) % mod;
for(int k2 = k1; k2 <= k; k2 ++) {
ans[id] = (ans[id] + (1LL * sum1[ql[id]][k1] * sum2[qr[id]][k2]) % mod) % mod;
}
}
}
}
solve(tl, mid, qul);
solve(mid + 1, tr, qur);
}
void Baizho() {
cin>>n>>k;
for(int i = 1; i <= n; i ++) cin>>a[i];
vector<int> qu;
cin>>q;
for(int i = 1; i <= q; i ++) {
cin>>ql[i]>>qr[i];
qu.pb(i);
}
solve(1, n, qu);
for(int i = 1; i <= q; i ++) cout<<ans[i] + 1<<"\n";
}
signed main() {
// Freopen("nondec");
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// precalc();
int ttt = 1;
// cin>>ttt;
for(int i=1; i<=ttt; i++) {Baizho(); }
}
Compilation message
joi2019_ho_t3.cpp: In function 'void Freopen(std::string)':
joi2019_ho_t3.cpp:35:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joi2019_ho_t3.cpp:35:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |