#include <cstdio> #include <algorithm> #include <cstring>using namespace std;const int M = 50006;int w[m ], a[m], root[m];struct tree{int ls, RS, w;} tr[m << 4]; int tot; void insert (int &x, int l, int r, int d) { Tr[++tot] = tr[x]; TR[TOT].W + +; x = tot; if (L = = r) return; int m = (l+r) >> 1; if (d <= m) insert (Tr[tot].ls, L, M, d); else Insert (tr[tot].rs, m+1, R, D);} int query (int rt, int r, int l, int r) {if (L = = R) return TR[RT].W; if (R >= R) return TR[RT].W; int m = (L + r) >> 1; int ret = 0; if (M <= R) ret + = query (Tr[rt].ls, R, L, m); if (R > m) ret + = query (tr[rt].rs, R, M+1, R); return ret;} int main () {int T; scanf ("%d", &t); int m, N, L, R, B; while (T--) {scanf ("%d%d", &n, &m); int tt = 0; for (int i = 1; I <= n; i + +) {scanf ("%d", a[i]); W[tt + +] = A[i]; } sort (w, w+tt); int u = Unique (w, W+TT)-W; Root[0] = 0; for (int i = 1; I <= n; i + +) {int d = lower_bound (W, W+u, A[i])-W; Root[i] = root[i-1]; Insert (Root[i], 0, u-1, D); } while (M--) {scanf ("%d%d%d%d", &l, &r, &a, &b); int rr = Lower_bound (W, W+u, b)-W; int ll = Lower_bound (W, W+u, a+1)-w-1; int ans = query (Root[r], RR, 0, U-1)-query (Root[l-1], LL, 0, u-1); }} return 0;}
Forget is where the topic ~ ~ is a chairman of the tree template, save a bit ...
Chairman Tree Template