标签:
#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; }
忘记是哪里的题目了~~就是个主席树的模板,保存一下。。。
标签:
原文地址:http://www.cnblogs.com/acmood/p/4449037.html