Submission #261622

#TimeUsernameProblemLanguageResultExecution timeMemory
261622dooweyExamination (JOI19_examination)C++14
100 / 100
1913 ms306148 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

struct P{
    int x;
    int y;
    int sum;
};

const int N = (int)1e5 + 100;

vector<pii> S[N];

int zsz, ysz;
struct Segt{
    struct Node{
        int nex_l;
        int nex_r;
        int value;
    };
    vector<Node> cur;
    int id;
    int add_node(){
        cur.push_back({-1, -1, 0});
        id ++ ;
        return id - 1;
    }
    void upd(int node, int cl, int cr, int pos){
        cur[node].value ++ ;
        if(cl == cr)
            return;
        int mid = (cl + cr) / 2;
        if(mid >= pos){
            if(cur[node].nex_l == -1){
                int nxi = add_node();
                cur[node].nex_l = nxi;
            }
            upd(cur[node].nex_l, cl, mid, pos);

        }
        else{
            if(cur[node].nex_r == -1){
                int nxi = add_node();
                cur[node].nex_r = nxi;
            }
            upd(cur[node].nex_r, mid + 1, cr, pos);
        }
    }
    int get(int node, int cl, int cr, int tl, int tr){
        if(cr < tl || cl > tr || node == -1)
            return 0;
        if(cl >= tl && cr <= tr)
            return cur[node].value;
        int mid = (cl + cr) / 2;
        return get(cur[node].nex_l, cl, mid, tl, tr) + get(cur[node].nex_r, mid + 1, cr, tl, tr);
    }
};

Segt F[N * 4 + 512];

void update(int node, int cl, int cr, int pos, int vv){
    if(F[node].cur.empty())
        F[node].add_node();
    F[node].upd(0, 0, zsz - 1, vv);
    if(cl == cr)
        return;
    int mid = (cl + cr) / 2;
    if(mid >= pos)
        update(node * 2, cl, mid, pos, vv);
    else
        update(node * 2 + 1, mid + 1, cr, pos, vv);
}

int query(int node, int cl, int cr, int tl, int tr, int nl, int nr){
    if(cr < tl || cl > tr) return 0;
    if(cl >= tl && cr <= tr){
        if(F[node].cur.empty()) return 0;
        else return F[node].get(0, 0, zsz - 1, nl, nr);
    }
    int mid = (cl + cr) / 2;
    return query(node * 2, cl, mid, tl, tr, nl, nr) + query(node * 2 + 1, mid + 1, cr, tl, tr, nl, nr);
}

struct Query{
    int yy;
    int zz;
    int id;
};

vector<Query> tq[N];

int res[N];

int main(){
    fastIO;
    int n, q;
    cin >> n >> q;
    vector<P> st;
    int a, b;
    vector<int> xx, yy, zz;
    for(int i = 1; i <= n; i ++ ){
        cin >> a >> b;
        st.push_back({a, b, a + b});
        xx.push_back(a);
        yy.push_back(b);
        zz.push_back(a + b);
    }
    sort(xx.begin(), xx.end());
    sort(yy.begin(), yy.end());
    sort(zz.begin(), zz.end());
    xx.resize(unique(xx.begin(), xx.end()) - xx.begin());
    yy.resize(unique(yy.begin(), yy.end()) - yy.begin());
    zz.resize(unique(zz.begin(), zz.end()) - zz.begin());
    ysz = yy.size();
    zsz = zz.size();
    for(auto &p : st){
        p.x = lower_bound(xx.begin(), xx.end(), p.x) - xx.begin();
        p.y = lower_bound(yy.begin(), yy.end(), p.y) - yy.begin();
        p.sum = lower_bound(zz.begin(), zz.end(), p.sum) - zz.begin();
        S[p.x].push_back(mp(p.y, p.sum));
    }
    int c;
    for(int i = 0; i < q; i ++ ){
        cin >> a >> b >> c;
        a = lower_bound(xx.begin(), xx.end(), a) - xx.begin();
        b = lower_bound(yy.begin(), yy.end(), b) - yy.begin();
        c = lower_bound(zz.begin(), zz.end(), c) - zz.begin();
        tq[a].push_back({b, c, i});
    }
    for(int i = N - 1; i >= 0 ; i -- ){
        for(auto x : S[i]){
            update(1, 0, ysz - 1, x.fi, x.se);
        }
        for(auto x : tq[i]){
            res[x.id] = query(1, 0, ysz - 1, x.yy, ysz - 1, x.zz, zsz - 1);
        }
    }
    for(int i = 0 ; i < q; i ++ ){
        cout << res[i] << "\n";
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...