複習已學演算法,重視思維題
記得學字串和計算幾何
9.18
複習了離線二維數點:P2163和P10814
板子如下:
點選檢視程式碼
const int N = 2e6 + 5;
int n, m, a[N], ans[N];
struct Query {
int p, q, x;
Query(int p = 0, int q = 0, int x = 0) : p(p), q(q), x(x) {}
};
int c[N];
inline void update(int pos, int val) {
for (; pos < N; pos += lowbit(pos)) c[pos] += val;
}
inline int query(int pos) {
int ret = 0;
for (; pos; pos -= lowbit(pos)) ret += c[pos];
return ret;
}
vector <Query> v[N];
signed main(void) {
read(n), read(m);
for (int i = 1; i <= n; i++) read(a[i]);
for (int i = 1; i <= m; i++) {
int l, r, x;
read(l), read(r), read(x);
v[l - 1].push_back(Query(i, -1, x));
v[r].push_back(Query(i, 1, x));
}
for (int i = 1; i <= n; i++) {
update(a[i], 1);
for (auto u : v[i]) ans[u.p] += u.q * query(u.x);
}
for (int i = 1; i <= m; i++) writeln(ans[i]);
//fwrite(pf, 1, o1 - pf, stdout);
return 0;
}