Submission #1107494

#TimeUsernameProblemLanguageResultExecution timeMemory
1107494InvMODExamination (JOI19_examination)C++14
100 / 100
343 ms20448 KiB
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long

using ll = long long;
using ld = long double;
using ull = unsigned long long;

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

const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;


struct Query{
    /* -1: Update, +1: Query */
    int x, y, z, op;
    Query(int x = 0, int y = 0 ,int z = 0, int op = 0): x(x), y(y), z(z), op(op) {}
    bool operator < (const Query& q) const{
        if(z != q.z) return z > q.z;
        else{
            return op < q.op;
        }
    }
};

int n, q, bit[6*N], answer[N];

Query Q[N<<1];
vector<int> compress;

void update(int p, int val){
    for(; p <= sz(compress); p += p&-p) bit[p] += val;
    return;
}

int get(int p){
    int res = 0;
    for(; p; p -= p&-p) res += bit[p];
    return res;
}

int get_range(int l, int r){return get(r) - get(l-1);}

void Dnc(int l, int r){
    if(l == r){
        return;
    }
    else{
        int m = l+r>>1;

        vector<Query> Upd, Que;
        for(int i = l; i <= m; i++)
            if(Q[i].op < 0) Upd.pb(Q[i]);
        for(int i = m+1; i <= r; i++)
            if(Q[i].op > 0) Que.pb(Q[i]);

        sort(all(Upd), [&](Query x, Query y){return x.x > y.x;});
        sort(all(Que), [&](Query x, Query y){return x.x > y.x;});

        stack<int> save_Upd;

        int pointer = 0;
        for(Query &cur_q : Que){
            while(pointer < sz(Upd) && Upd[pointer].x >= cur_q.x){
                update(Upd[pointer].y, 1);
                save_Upd.push(Upd[pointer++].y);
            }

            answer[cur_q.op] += get_range(cur_q.y, sz(compress));
        }

        while(sz(save_Upd)){
            update(save_Upd.top(), -1);
            save_Upd.pop();
        }

        Dnc(l, m), Dnc(m+1, r);
    }
    return;
}

void solve()
{
    cin >> n >> q;
    for(int i = 1; i <= n; i++){
        int x,y; cin >> x >> y;
        Q[i] = Query(x, y, x+y, -1);
        compress.pb(x), compress.pb(y), compress.pb(x+y);
    }

    for(int i = n+1; i <= n+q; i++){
        int x,y,z; cin >> x >> y >> z;
        Q[i] = Query(x, y, z, i-n);
        compress.pb(x), compress.pb(y), compress.pb(z);
    }

    sort(all(compress)), compact(compress);

    for(int i = 1; i <= n+q; i++){
        Q[i].x = lower_bound(all(compress), Q[i].x) - compress.begin()+1;
        Q[i].y = lower_bound(all(compress), Q[i].y) - compress.begin()+1;
        Q[i].z = lower_bound(all(compress), Q[i].z) - compress.begin()+1;
    }

    sort(Q+1, Q+1+n+q);

    Dnc(1, n+q);

    for(int i = 1; i <= q; i++){
        cout << answer[i] <<"\n";
    }
}

signed 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; //cin >> t;
    while(t--) solve();
    return 0;
}

Compilation message (stderr)

examination.cpp: In function 'void Dnc(int, int)':
examination.cpp:63:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   63 |         int m = l+r>>1;
      |                 ~^~
examination.cpp: In function 'int main()':
examination.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  136 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
examination.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  137 |         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...