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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| #include <bits/stdc++.h>
using namespace std; typedef long long ll; #define int ll #define io ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define endl '\n' #define itt set<node>::iterator
const int maxn = 1e5 + 5; const ll mod = 1e9 + 7; const ll inf = 0x3f3f3f3f; int ar[maxn]; struct node { int k, x, id; }; vector<node> vt[maxn]; int ans[maxn]; vector<int> vv; int tree[maxn<<1]; int t, n, q, l, r, h;
int lowbit(int x) { return (-x) & x; }
void updata(int pos) { while(pos <= n + q) { ++tree[pos]; pos += lowbit(pos); } }
int get_sum(int pos) { int res = 0; while(pos) { res += tree[pos]; pos -= lowbit(pos); } return res; }
void init() { vv.clear(); for(int i = 0; i <= n; ++i) vt[i].clear(); for(int i = 0; i <= n + q; ++i) tree[i] = 0; for(int i = 0; i <= q; ++i) ans[i] = 0; }
void work() { cin >> n >> q;
init();
for(int i = 1; i <= n; ++i) { cin >> ar[i]; vv.push_back(ar[i]); }
for(int i = 1; i <= q; ++i) { cin >> l >> r >> h; vv.push_back(h); vt[l - 1].push_back({-1, h, i}); vt[r].push_back({1, h, i}); }
sort(vv.begin(), vv.end()); vv.erase(unique(vv.begin(), vv.end()), vv.end());
int id, sum, pos; for(int i = 1; i <= n; ++i) { pos = lower_bound(vv.begin(), vv.end(), ar[i]) - vv.begin() + 1; updata(pos);
for(int j = 0; j < vt[i].size(); ++j) { id = vt[i][j].id; pos = lower_bound(vv.begin(), vv.end(), vt[i][j].x) - vv.begin() + 1; sum = get_sum(pos); ans[id] += vt[i][j].k * sum; } }
for(int i = 1; i <= q; ++i) cout << ans[i] << ' '; cout << '\n'; }
signed main() { io; cin >> t; while(t--) work(); return 0; }
|