제출 #1307753

#제출 시각아이디문제언어결과실행 시간메모리
1307753mikolaj00Examination (JOI19_examination)C++20
2 / 100
3103 ms287852 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

ll const MAX_VAL = 2e9+1;

struct BIT_2D
{
    void update(ll x, ll y)
    {
        for (ll i = x; i <= MAX_VAL; i |= i+1)
            for (ll j = y; j <= MAX_VAL; j |= j+1)
            {
                arr[i][j]++; 
            }
    }

    ll query_pref(ll x, ll y)
    {
        ll res = 0;
        for (ll i = x; i >= 0; i = (i & (i+1))-1)
            for (ll j = y; j >= 0; j = (j & (j+1))-1)
            {
                if (arr.count(i) && arr[i].count(j))
                    res += arr[i][j];
            }

        return res;
    }

    ll query(ll i, ll j)
    {
        ll res = query_pref(MAX_VAL, MAX_VAL);
        if (i > 0)
            res -= query_pref(i-1, MAX_VAL);
        if (j > 0)
            res -= query_pref(MAX_VAL, j-1);
        if (i > 0 && j > 0)
            res += query_pref(i-1, j-1);
        return res;
    }

    unordered_map<ll, unordered_map<ll, ll>> arr;
};

struct Sweep
{
    ll x, y, z;
    int idx;

    friend bool operator<(Sweep const& s1, Sweep const& s2)
    {
        return make_pair(s1.z, -s1.idx) > make_pair(s2.z, -s2.idx);
    }
};

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, q;
    cin >> n >> q;

    vector<ll> s(n), t(n);
    for (int i = 0; i < n; i++)
        cin >> s[i] >> t[i];

    vector<ll> x(q), y(q), z(q);
    for (int i = 0; i < q; i++)
        cin >> x[i] >> y[i] >> z[i];

    vector<Sweep> sweep;
    sweep.reserve(n+q);
    for (int i = 0; i < n; i++)
        sweep.push_back({s[i], t[i], s[i]+t[i], -1});
    for (int i = 0; i < q; i++)
        sweep.push_back({x[i], y[i], z[i], i});
    sort(sweep.begin(), sweep.end());

    BIT_2D bit;

    vector<ll> ans(q);
    for (auto s : sweep)
    {
        if (s.idx == -1)
        {
            bit.update(s.x, s.y);
        }
        else
            ans[s.idx] = bit.query(s.x, s.y);
    }

    for (auto ans_i : ans)
        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...