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
| #include <bits/stdc++.h> #define PII pair<int, int> using namespace std; template <typename _Tp> inline void read(_Tp &x) { int neg = 1; char ch; while(ch = getchar(), !isdigit(ch)) if(ch == '-') neg = -1; x = ch - '0'; while(ch = getchar(), isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ '0'); x *= neg; return; }
int n, q, a[200005], blk, cnt, bans[505][200005], x, l, r; PII p[200005]; signed main() { read(n), read(q); for(int i = 1; i <= n; ++i) read(a[i]); a[0] = 0, ++n; for(int i = 0; i < n; ++i) p[i] = {a[i], i}; sort(p, p + n); for(int i = 0; i < n; ++i) { if(i == 0) a[p[i].second] = 1; else { if(p[i].first == p[i - 1].first) a[p[i].second] = a[p[i - 1].second]; else a[p[i].second] = a[p[i - 1].second] + 1; } } blk = sqrt(n); cnt = (n - 1) / blk + 1; for(int i = 0; i < n - 1; ++i) if(a[i] < a[i + 1]) { ++bans[i / blk][a[i]]; --bans[i / blk][a[i + 1]]; } for(int i = 0; i < cnt; ++i) for(int j = 1; j <= n; ++j) bans[i][j] += bans[i][j - 1]; while(q--) { read(l), read(r), read(x); int ind = lower_bound(p, p + n, (PII){x + 1, 0}) - p, ans; x = a[p[ind - 1].second]; ans = a[l] > x; if(l / blk == r / blk) { for(int i = l; i < r; ++i) ans += (a[i] <= x && a[i + 1] > x); } else { for(int i = l; i < (l / blk + 1) * blk; ++i) ans += (a[i] <= x && a[i + 1] > x); for(int i = r / blk * blk; i < r; ++i) ans += (a[i] <= x && a[i + 1] > x); for(int i = l / blk + 1; i < r / blk; ++i)ans += bans[i][x]; } printf("%lld\n", ans); } return 0; }
|