Submission #1099704

#TimeUsernameProblemLanguageResultExecution timeMemory
1099704Zero_OPExamination (JOI19_examination)C++14
100 / 100
385 ms12024 KiB
#include <bits/stdc++.h>

using namespace std;

#define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i)
#define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i)
#define ROF(i, r, l) for(int i = (r), _l = (l); i >= _l; --i)
#define all(v) begin(v), end(v)
#define compact(v) v.erase(unique(all(v)), end(v))
#define sz(v) (int)v.size()
#define dbg(x) "[" #x " = " << (x) << "]"

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

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

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

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T> T random_int(T l, T r){ return uniform_int_distribution<T>(l, r)(rng); }
template<typename T> T random_real(T l, T r){ return uniform_real_distribution<T>(l, r)(rng); }

const int MAX = 2e5 + 5;

int n, q, math[MAX], informatics[MAX], ans[MAX];

struct event{
    int x, y, z, id;
    bool operator < (const event& o) const{
        return make_tuple(-x, -y, -z, id) < make_tuple(-o.x, -o.y, -o.z, o.id);
    }
};

struct Fenwick{
    vector<int> bit;
    Fenwick(int n) : bit(n + 1, 0) {}

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

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

void cdq(int l, int r, vector<event>& events, Fenwick& fenwick){
    if(l >= r) return;
    int mid = l + r >> 1;

    cdq(l, mid, events, fenwick);
    cdq(mid + 1, r, events, fenwick);

    vector<int> id(r - l + 1);
    iota(all(id), l);

    sort(all(id), [&](int i, int j){
        return make_tuple(-events[i].y, -events[i].z, events[i].id) < make_tuple(-events[j].y, -events[j].z, events[j].id);
    });

    stack<int> st;
    rep(t, 0, sz(id)){
        int i = id[t];

        if(events[i].id == -1 && i <= mid){
            fenwick.update(events[i].z, +1);
            st.push(events[i].z);
        }

        if(events[i].id > 0 && i > mid){
            ans[events[i].id] += fenwick.query(events[i].z);
        }
    }

    while(!st.empty()){
        fenwick.update(st.top(), -1); st.pop();
    }
}

void testcase(){
    cin >> n >> q;
    vector<event> events;
    vector<int> Oz;
    FOR(i, 1, n){
        int x, y;
        cin >> x >> y;
        events.push_back({x, y, x + y, -1});
        Oz.push_back(x + y);
    }

    FOR(i, 1, q){
        int x, y, z;
        cin >> x >> y >> z;
        events.push_back({x, y, z, i});
        Oz.push_back(z);
    }

    sort(all(Oz));
    compact(Oz);

    rep(i, 0, sz(events)){
        events[i].z = lower_bound(all(Oz), events[i].z) - Oz.begin() + 1;
    }

    sort(all(events));

    Fenwick fenwick(sz(Oz));
    cdq(0, sz(events) - 1, events, fenwick);

    FOR(i, 1, q){
        cout << ans[i] << '\n';
    }
}

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

    #define filename "task"
    if(fopen(filename".inp", "r")){
        freopen(filename".inp", "r", stdin);
        freopen(filename".out", "w", stdout);
    }

    int T = 1; //cin >> T;
    while(T--) testcase();

    return 0;
}

Compilation message (stderr)

examination.cpp: In function 'bool minimize(T&, const T&)':
examination.cpp:15:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   15 |     if(a > b) return a = b, true; return false;
      |     ^~
examination.cpp:15:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   15 |     if(a > b) return a = b, true; return false;
      |                                   ^~~~~~
examination.cpp: In function 'bool maximize(T&, const T&)':
examination.cpp:20:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
   20 |     if(a < b) return a = b, true; return false;
      |     ^~
examination.cpp:20:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
   20 |     if(a < b) return a = b, true; return false;
      |                                   ^~~~~~
examination.cpp: In function 'void cdq(int, int, std::vector<event>&, Fenwick&)':
examination.cpp:62:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   62 |     int mid = l + r >> 1;
      |               ~~^~~
examination.cpp: In function 'int main()':
examination.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  134 |         freopen(filename".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:135:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  135 |         freopen(filename".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...