This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX = 5e5;
struct fenwick_tree{
    int bit[MAX + 1] = {0};
    void update(int pos, int val){
        for(int i = pos; i <= MAX; i = i | (i + 1)){
            bit[i] += val;
        }
    }
    int query(int pos){
        int res = 0;
        for(int i = pos; i >= 0; i = (i & (i + 1)) - 1){
            res += bit[i];
        }
        return res;
    }
    int query(int l, int r){
        if(l == 0) return query(r);
        return query(r) - query(l - 1);
    }
};
struct event{
    int x, y, z, idx, type;
    bool operator<(event other){
        if(z == other.z) return type < other.type;
        return z > other.z;
    }
};
int n, m;
void solve(){
    vector<event> events;
    map<int, int> mm;
    for(int i = 0; i < n; i++){
        int x, y;
        cin>>x>>y;
        mm[x]; mm[y];
        events.push_back({x, y, x + y, -1, 0});
    }
    for(int i = 0; i < m; i++){
        int x, y, z;
        cin>>x>>y>>z;
        mm[x]; mm[y];
        events.push_back({x, y, max(x + y, z), i, 1});
    }
    int idx = 0;
    for(auto& i : mm) i.second = idx++;
    sort(events.begin(), events.end());
    int ans[m];
    fenwick_tree X, Y;
    int cnt = 0;
    for (auto i : events)
    {
        if(i.type == 0){
            Y.update(mm[i.y], 1);
            X.update(mm[i.x], 1);
            cnt++;
        }else{
            ans[i.idx] = Y.query(mm[i.y], MAX) + X.query(mm[i.x], MAX) - cnt;
        }
    }
    for (int i = 0; i < m; i++) cout<<ans[i]<<endl;
}
signed main(){
    
    cin>>n>>m;
    solve();
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |