1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010, mod = 1e9 + 7;
int T; int n, m; int a[N]; int root[N], tot; int ls[N * 25], rs[N * 25], sum[N * 25];
int qmi(int a, int b) { int res = 1; while (b) { if (b & 1) res = (ll)res * a % mod; a = (ll)a * a % mod; b >>= 1; } return res; }
void modify(int &u, int v, int l, int r, int p) { u = ++tot; ls[u] = ls[v], rs[u] = rs[v], sum[u] = sum[v] + 1; if (l == r) return; int mid = l + r >> 1; if (p <= mid) modify(ls[u], ls[v], l, mid, p); else modify(rs[u], rs[v], mid + 1, r, p); }
int query(int u, int v, int l, int r, int k) { if (l == r) return l; int s = sum[ls[u]] - sum[ls[v]]; int mid = l + r >> 1; if (k <= s) return query(ls[u], ls[v], l, mid, k); else return query(rs[u], rs[v], mid + 1, r, k - s); }
int main() { cin >> T; while (T--) { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) modify(root[i], root[i - 1], 1, n, a[i]); int L = 1, R = n; while (m--) { int l, r, k; cin >> l >> r >> k; int x1 = query(root[r], root[l - 1], 1, n, k), x2; if (k == r - l + 1) x2 = n + 1; else x2 = query(root[r], root[l - 1], 1, n, k + 1); L = max(L, x1), R = min(R, x2 - 1); } if (L == R) cout << "1 " << L << endl; else cout << qmi(R - L + 1, mod - 2) << endl; }
return 0; }
|