Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <cmath>
- #include<iomanip>
- using namespace std;
- const int INF = 1e9, N = 6e5;
- int n, t, I, totalsum, a[N];
- struct Node {
- vector<int> cont;
- int moreThan(int l) {
- int h = cont.end() - upper_bound(cont.begin(), cont.end(), l);
- return h;
- }
- int lessThan(int l) {
- int h = cont.begin() - lower_bound(cont.begin(), cont.end(), l);
- return -h;
- }
- };
- struct SegmentTree {
- vector<Node> tree;
- void build(int n) {
- int h = 1;
- while (h < n) {
- h *= 2;
- }
- h *= 2;
- tree.resize(h);
- for (int i = h - 1; i > 0; i--) {
- tree[i].cont.clear();
- if (i >= h / 2) {
- tree[i].cont.push_back(a[i - h / 2]);
- }
- else {
- for (int j = 0; j < tree[i * 2].cont.size(); j++) {
- tree[i].cont.push_back(tree[i * 2].cont[j]);
- }
- for (int j = 0; j < tree[i * 2 + 1].cont.size(); j++) {
- tree[i].cont.push_back(tree[i * 2 + 1].cont[j]);
- }
- sort(tree[i].cont.begin(), tree[i].cont.end());
- }
- }
- }
- int FindMore(int v, int l, int r, int L, int R, long long x) {
- if (L >= l && R <= r) {
- return tree[v].moreThan(x);
- }
- if (L > r || R < l) {
- return 0;
- }
- return FindMore(v * 2, l, r, L, (L + R) / 2, x) + FindMore(v * 2 + 1, l, r, (L + R) / 2 + 1, R, x);
- }
- int FindLess(int v, int l, int r, int L, int R, long long x) {
- if (L >= l && R <= r) {
- return tree[v].lessThan(x);
- }
- if (L > r || R < l) {
- return 0;
- }
- return FindLess(v * 2, l, r, L, (L + R) / 2, x) + FindLess(v * 2 + 1, l, r, (L + R) / 2 + 1, R, x);
- }
- int More(int l, int r, int x) {
- return FindMore(1, l, r, 0, tree.size() / 2 - 1, x);
- }
- int Less(int l, int r, int x) {
- return FindLess(1, l, r, 0, tree.size() / 2 - 1, x);
- }
- };
- SegmentTree ST;
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cin >> n;
- for (int i = 0; i < n; i++) {
- cin >> a[i];
- }
- ST.build(n);
- int q;
- cin >> q;
- int u, v, x;
- for (int i = 0; i < q; i++) {
- cin >> u >> v >> x;
- cout << ST.More(u, v, x) << " " << ST.Less(u,v,x) << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement