Submission #1148944

#TimeUsernameProblemLanguageResultExecution timeMemory
1148944weakweakweakExamination (JOI19_examination)C++20
100 / 100
411 ms28952 KiB
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
using arr = array<int,4>;
using pii = pair<int,int>;
#define fs first 
#define sc second

int n, q, ans[888888], a[888888] = {0};

void modify (int i, int x) {
    i++;
    for (; i <= 610000; i+=i&-i) a[i] += x;
}
int query (int i) {
    i++;
    int res = 0;
    for (; i > 0; i-=i&-i) res += a[i];
    return res;
}

vector<arr> cdq (vector<arr> a) {
    if (a.size() == 1) return a;
    int mid = a.size()/2;
    vector<arr> b(a.begin(), a.begin()+mid), c(a.begin()+mid, a.end()), d;
    b = cdq(b), c = cdq(c);

    int j1 = 0, j2 = 0, cnt = 0;
    vector<pii> whatidid;
    for (arr x : c) if (x[3] == 0) cnt++;
    for (int i = 0; i < a.size(); i++) {
        if (j2 == c.size() or (j1 != b.size() and b[j1][1] <= c[j2][1])) {
            if (b[j1][3] != 0) {
                // if (b[j1][3] == -1) cout << "!" << ' ' << query(400000) << ' ' << query(b[j1][2]-1) << '\n';
                ans[-b[j1][3]] -= query(601000)-query(b[j1][2]-1);
            }
            d.push_back(b[j1]);
            j1++;
        }
        else {
            if (c[j2][3] == 0) {
                cnt--;
                modify(c[j2][2], 1);
                whatidid.push_back(make_pair(c[j2][2], 1));
            }
            d.push_back(c[j2]);
            j2++;
        }
    }
    // for (auto x : b) cout << x[0] << ' ' << x[1] << ' ' << x[2] << ' ' << x[3] << '\n';
    // cout << '\n';
    // for (auto x : c) cout << x[0] << ' ' << x[1] << ' ' << x[2] << ' ' << x[3] << '\n';
    // cout << '\n';
    for (auto x : b) if (x[3] != 0) {
        // if (x[3] == -1) cout << "!" << query(400000) << ' ' << query(x[2]-1) << ' ' << whatidid.size() << '\n';
        ans[-x[3]] += query(601000)-query(x[2]-1);
    }
    // cout << "------------------------------------\n\n\n";

    for (auto p : whatidid) modify(p.fs, -p.sc);
    return d;
}

int main() {
    // ios_base::sync_with_stdio(false); cin.tie(0);
    vector<arr> v;
    cin >> n >> q;
    vector<int> bv;
    for (int i = 1; i <= n; i++) {
        int x, y;
        cin >> x >> y;
        bv.push_back(x); bv.push_back(y); bv.push_back(x+y);
        v.push_back({x, y, x+y, 0});
    }
    for (int i = 1; i <= q; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        bv.push_back(a); bv.push_back(b); bv.push_back(c);
        v.push_back({a, b, c, -i});
    }
    sort(bv.begin(), bv.end()); bv.erase(unique(bv.begin(),bv.end()), bv.end());
    for (auto &x : v) {
        for (int i = 0; i < 3; i++) x[i] = lower_bound(bv.begin(), bv.end(), x[i]) - bv.begin() + 1;
    }

    sort(v.begin(), v.end());
    cdq(v);

    for (int i = 1; i <= q; i++) cout << ans[i] << '\n'; 
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...