Submission #417988

#TimeUsernameProblemLanguageResultExecution timeMemory
417988MarcoMeijerExamination (JOI19_examination)C++14
100 / 100
2954 ms15616 KiB
#include <bits/stdc++.h>
using namespace std;
 
// macros
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> lll;
typedef tuple<int, int, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<ll> vll;
typedef vector<lll> vlll;
#define REP(a,b,c) for(int a=int(b); a<int(c); a++)
#define RE(a,c) REP(a,0,c)
#define RE1(a,c) REP(a,1,c+1)
#define REI(a,b,c) REP(a,b,c+1)
#define REV(a,b,c) for(int a=int(c-1); a>=int(b); a--)
#define FOR(a,b) for(auto& a : b)
#define all(a) a.begin(), a.end()
#define INF 1e18
#define EPS 1e-9
#define pb push_back
#define popb pop_back
#define fi first
#define se second
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
 
// input
template<class T> void IN(T& x) {cin >> x;}
template<class H, class... T> void IN(H& h, T&... t) {IN(h); IN(t...); }
 
// output
template<class T1, class T2> void OUT(const pair<T1,T2>& x);
template<class T> void OUT(const vector<T>& x);
template<class T> void OUT(const T& x) {cout << x;}
template<class H, class... T> void OUT(const H& h, const T&... t) {OUT(h); OUT(t...); }
template<class T1, class T2> void OUT(const pair<T1,T2>& x) {OUT(x.fi,' ',x.se);}
template<class T> void OUT(const vector<T>& x) {RE(i,x.size()) OUT(i==0?"":" ",x[i]);}
template<class... T> void OUTL(const T&... t) {OUT(t..., "\n"); }
template<class H> void OUTLS(const H& h) {OUTL(h); }
template<class H, class... T> void OUTLS(const H& h, const T&... t) {OUT(h,' '); OUTLS(t...); }
 
// dp
template<class T> bool ckmin(T&a, T&b) { bool bl = a > b; a = min(a,b); return bl;}
template<class T> bool ckmax(T&a, T&b) { bool bl = a < b; a = max(a,b); return bl;}
 
void program();
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    program();
}
 
 
//===================//
//   begin program   //
//===================//
 
const int MX = 5e5;
const int N = (1<<20);
const int SQ = 450;

int n, q;
int s[MX], t[MX], c[MX];
int x[MX], y[MX], z[MX];
int a[MX], b[MX];
int sa[MX], sb[MX];
vi difs, dift, difz;

// fenwick tree
int FT[MX], ftTotal=0;
void addFT(int i, int value) {
    ftTotal += value;
    for(i++; i<MX; i+=i&-i) FT[i] += value;
}
int getFT(int i) {
    int ans = 0;
    for(i++; i; i-=i&-i) ans += FT[i];
    return ans;
}

int cnt[MX], ans[MX];

void program() {
    IN(n,q);
    RE(i,n) IN(s[i],t[i]);
    RE(i,q) IN(x[i],y[i],z[i]);

    // coördinate compression
    RE(i,n) c[i]=s[i]+t[i];
    RE(i,n) difs.pb(s[i]), dift.pb(t[i]), difz.pb(c[i]);
    RE(i,q) difs.pb(x[i]), dift.pb(y[i]), difz.pb(z[i]);
    sort(all(difs));
    sort(all(dift));
    sort(all(difz));
    RE(i,n) a[i]=lower_bound(all(difs),s[i])-difs.begin(), b[i]=lower_bound(all(dift),t[i])-dift.begin(), c[i]=lower_bound(all(difz),c[i])-difz.begin();
    RE(i,q) x[i]=lower_bound(all(difs),x[i])-difs.begin(), y[i]=lower_bound(all(dift),y[i])-dift.begin(), z[i]=lower_bound(all(difz),z[i])-difz.begin();

    // sort
    RE(i,n) sa[i]=i;
    sort(sa,sa+n,[](int i, int j) {
        return a[i]<a[j];
    });
    RE(i,n) sb[i]=i;
    sort(sb,sb+n,[](int i, int j) {
        return b[i]<b[j];
    });

    // answering queries
    int cx=n, cy=n;
    viii pq;
    RE(i,q) pq.pb({x[i]/SQ,y[i]/SQ,i});
    sort(all(pq));
    FOR(p,pq) {
        int _, i;
        tie(_,_,i) = p;
        while(cx && a[sa[cx-1]] >= x[i]) {
            cx--;
            cnt[sa[cx]]++;
            if(cnt[sa[cx]] == 2)
                addFT(c[sa[cx]],1);
        }
        while(cy && b[sb[cy-1]] >= y[i]) {
            cy--;
            cnt[sb[cy]]++;
            if(cnt[sb[cy]] == 2)
                addFT(c[sb[cy]],1);
        }
        while(cx != n && a[sa[cx]] < x[i]) {
            if(cnt[sa[cx]] == 2)
                addFT(c[sa[cx]],-1);
            cnt[sa[cx]]--;
            cx++;
        }
        while(cy != n && b[sb[cy]] < y[i]) {
            if(cnt[sb[cy]] == 2)
                addFT(c[sb[cy]],-1);
            cnt[sb[cy]]--;
            cy++;
        }
        ans[i] = ftTotal - getFT(z[i]-1);
    }

    RE(i,q) OUTL(ans[i]);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...