Submission #1281320

#TimeUsernameProblemLanguageResultExecution timeMemory
1281320InvMODMatryoshka (JOI16_matryoshka)C++17
100 / 100
428 ms61956 KiB
#include<bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define eb emplace_back

#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())

template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}

#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"

using ll = long long;
using ld = long double;

template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}


struct BIT{
    vector<int> bit;

    BIT(int n = 0): bit(n + 1) {}

    void update(int p, int val){
        for(; p > 0; p -= p & -p){
            bit[p] += val;
        }
    }

    int query(int p){
        int ans = 0;
        for(; p < sz(bit); p += p & -p){
            ans += bit[p];
        }
        return ans;
    }
};

void Main()
{
    int n, q; cin >> n >> q;

    vector<pi> a(n + 1);
    FOR(i, 1, n){
        cin >> a[i].se >> a[i].fi;
    }

    vector<pi> Q(n + 1);
    FOR(i, 1, q){
        cin >> Q[i].se >> Q[i].fi;
    }

    vi comp(1, -1);
    FOR(i, 1, n) comp.eb(a[i].fi), comp.eb(a[i].se);
    FOR(i, 1, q) comp.eb(Q[i].fi), comp.eb(Q[i].se);

    sort(all(comp)), compact(comp);
    FOR(i, 1, n){
        a[i].fi = lwrbound(comp, a[i].fi);
        a[i].se = lwrbound(comp, a[i].se);
    }

    FOR(i, 1, q){
        Q[i].fi = lwrbound(comp, Q[i].fi);
        Q[i].se = lwrbound(comp, Q[i].se);
    }

    int m = sz(comp) - 1;
    vector<vi> Upd(m + 1, vi()), Que(m + 1, vi());

    FOR(i, 1, n) Upd[a[i].fi].eb(i);
    FOR(i, 1, q) Que[Q[i].fi].eb(i);

    vi ans(q + 1); BIT bit(m); multiset<int> s;
    FOR(i, 1, m){
        sort(all(Upd[i]), [&](int x, int y){
            return a[x].se < a[y].se;
        });

        for(int x : Upd[i]){
            auto it = s.lower_bound(a[x].se);
            if(it != s.begin()){
                --it;
                s.erase(it);
                bit.update(*it, -1);
            }
        }
        for(int x : Upd[i]) s.insert(a[x].se), bit.update(a[x].se, +1);

        for(int j : Que[i]){
            ans[j] = bit.query(Q[j].se);
        }
    }

    FOR(i, 1, q) cout << ans[i] << el;
}

int32_t main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    #define name "InvMOD"
    if(fopen(name".INP", "r")){
        freopen(name".INP", "r", stdin);
        freopen(name".OUT", "w", stdout);
    }

    int t = 1; while(t--) Main();
    return 0;
}

Compilation message (stderr)

matryoshka.cpp: In function 'int32_t main()':
matryoshka.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  119 |         freopen(name".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
matryoshka.cpp:120:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  120 |         freopen(name".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...