Submission #484154

# Submission time Handle Problem Language Result Execution time Memory
484154 2021-11-02T08:18:29 Z Mahfel Examination (JOI19_examination) C++17
100 / 100
255 ms 28936 KB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<pair<pair<ll,ll>,ll>> vp;
#define int ll
const int MXN = 5e5 + 20 , Inf = 1e9 + 7;
vector<pair<pair<int,int> , int>> q1, q2, a;
int ans[MXN] , x[MXN] , y[MXN] , z[MXN] , A[MXN] , B[MXN];
int n,q;

struct fenwick {
    int size;
    vector<int> bit;
    fenwick(int n) {
        size = n;
        bit.assign(n , 0);
    }
    void add(int i , int k) {
        for(; i < size ; i |= (i+1)) {
            bit[i] += k;
        }
    }
    int sum(int i) {
        int res = 0;
        for(; i >= 0 ; i = (i&(i+1))-1) {
            res += bit[i];
        }
        return res;
    }
    int sum(int l , int r) {
        return (l == 0 ? sum(r) : sum(r)-sum(l-1));
    }
};

void compress(vp &a , vp &b) {
    vector<int> tmp;
    for(auto p : a) {
        tmp.push_back(p.first.first); tmp.push_back(p.first.second);
    }
    for(auto p : b) {
        tmp.push_back(p.first.first); tmp.push_back(p.first.second);
    }
    sort(tmp.begin() , tmp.end());
    int us = unique(tmp.begin() , tmp.end()) - tmp.begin();
    for(auto &p : a) {
        p.first.first = lower_bound(tmp.begin() , tmp.end() , p.first.first) - tmp.begin()+1;
        p.first.second = lower_bound(tmp.begin() , tmp.end() , p.first.second) - tmp.begin()+1;
    }
    for(auto &p : b) {
        p.first.first = lower_bound(tmp.begin() , tmp.end() , p.first.first) - tmp.begin()+1;
        p.first.second = lower_bound(tmp.begin() , tmp.end() , p.first.second) - tmp.begin()+1;
    }
}

void solve1(vp a , vp q) {
    compress(a , q);
    fenwick cnt(MXN);
    sort(a.rbegin() , a.rend()); sort(q.rbegin() , q.rend());
    a.push_back({{-Inf , -Inf} , -1});
    int ptr = 0;
    for(auto query : q) {
        while(a[ptr].first.first >= query.first.first) {
            cnt.add(a[ptr++].first.second , 1);
        }
        ans[query.second] = cnt.sum(query.first.second , cnt.size-1);
    }
}
void solve2(vp a , vp q) {
    compress(a , q);
    fenwick cntx(MXN) , cnty(MXN);
    sort(a.begin() , a.end() , [&](pair<pair<int,int>,int> x , pair<pair<int,int>,int> y) {
        return A[x.second]+B[x.second] > A[y.second]+B[y.second];
    });
    sort(q.begin() , q.end() , [&](pair<pair<int,int>,int> x , pair<pair<int,int>,int> y) {
        return z[x.second] > z[y.second];
    });
    a.push_back({{-Inf , -Inf} , MXN-1});
    int ptr = 0;
    for(auto query : q) {
        while(A[a[ptr].second]+B[a[ptr].second] >= z[query.second]) {
            cntx.add(a[ptr].first.first , 1);
            cnty.add(a[ptr].first.second , 1);
            ptr++;
        }
        ans[query.second] = ptr-cntx.sum(0 , query.first.first-1)-cnty.sum(0 , query.first.second-1);
    }
}

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

    cin >> n >> q;
    a.resize(n);
    for(int i = 0 ; i < n ; i++) {
        cin >> a[i].first.first >> a[i].first.second;
        a[i].second = i;
        A[i] = a[i].first.first; B[i] = a[i].first.second;
    }
    for(int i = 0 ; i < q ; i++) {
        cin >> x[i] >> y[i] >> z[i];
        if(x[i]+y[i] >= z[i]) q1.push_back({{x[i] , y[i]} , i});
        else q2.push_back({{x[i] , y[i]} , i});
    }
    solve2(a , q2); solve1(a , q1); 

    for(int i = 0 ; i < q ; i++) cout << ans[i] << "\n";
}

Compilation message

examination.cpp: In function 'void compress(vp&, vp&)':
examination.cpp:44:9: warning: unused variable 'us' [-Wunused-variable]
   44 |     int us = unique(tmp.begin() , tmp.end()) - tmp.begin();
      |         ^~
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8140 KB Output is correct
3 Correct 5 ms 8140 KB Output is correct
4 Correct 5 ms 8140 KB Output is correct
5 Correct 5 ms 8140 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 10 ms 8964 KB Output is correct
8 Correct 10 ms 8908 KB Output is correct
9 Correct 10 ms 8908 KB Output is correct
10 Correct 9 ms 8868 KB Output is correct
11 Correct 10 ms 8852 KB Output is correct
12 Correct 8 ms 8836 KB Output is correct
13 Correct 9 ms 8780 KB Output is correct
14 Correct 10 ms 8804 KB Output is correct
15 Correct 9 ms 8784 KB Output is correct
16 Correct 9 ms 8780 KB Output is correct
17 Correct 9 ms 8872 KB Output is correct
18 Correct 7 ms 8784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 26032 KB Output is correct
2 Correct 212 ms 26008 KB Output is correct
3 Correct 219 ms 26020 KB Output is correct
4 Correct 167 ms 25236 KB Output is correct
5 Correct 173 ms 25404 KB Output is correct
6 Correct 123 ms 24468 KB Output is correct
7 Correct 217 ms 25884 KB Output is correct
8 Correct 211 ms 25884 KB Output is correct
9 Correct 201 ms 25816 KB Output is correct
10 Correct 154 ms 25232 KB Output is correct
11 Correct 152 ms 25236 KB Output is correct
12 Correct 85 ms 24468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 212 ms 26032 KB Output is correct
2 Correct 212 ms 26008 KB Output is correct
3 Correct 219 ms 26020 KB Output is correct
4 Correct 167 ms 25236 KB Output is correct
5 Correct 173 ms 25404 KB Output is correct
6 Correct 123 ms 24468 KB Output is correct
7 Correct 217 ms 25884 KB Output is correct
8 Correct 211 ms 25884 KB Output is correct
9 Correct 201 ms 25816 KB Output is correct
10 Correct 154 ms 25232 KB Output is correct
11 Correct 152 ms 25236 KB Output is correct
12 Correct 85 ms 24468 KB Output is correct
13 Correct 226 ms 25516 KB Output is correct
14 Correct 228 ms 26888 KB Output is correct
15 Correct 210 ms 25968 KB Output is correct
16 Correct 175 ms 24804 KB Output is correct
17 Correct 175 ms 24872 KB Output is correct
18 Correct 128 ms 25028 KB Output is correct
19 Correct 221 ms 25716 KB Output is correct
20 Correct 226 ms 25348 KB Output is correct
21 Correct 215 ms 26860 KB Output is correct
22 Correct 155 ms 25280 KB Output is correct
23 Correct 157 ms 25268 KB Output is correct
24 Correct 85 ms 24548 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 8140 KB Output is correct
2 Correct 5 ms 8140 KB Output is correct
3 Correct 5 ms 8140 KB Output is correct
4 Correct 5 ms 8140 KB Output is correct
5 Correct 5 ms 8140 KB Output is correct
6 Correct 5 ms 8128 KB Output is correct
7 Correct 10 ms 8964 KB Output is correct
8 Correct 10 ms 8908 KB Output is correct
9 Correct 10 ms 8908 KB Output is correct
10 Correct 9 ms 8868 KB Output is correct
11 Correct 10 ms 8852 KB Output is correct
12 Correct 8 ms 8836 KB Output is correct
13 Correct 9 ms 8780 KB Output is correct
14 Correct 10 ms 8804 KB Output is correct
15 Correct 9 ms 8784 KB Output is correct
16 Correct 9 ms 8780 KB Output is correct
17 Correct 9 ms 8872 KB Output is correct
18 Correct 7 ms 8784 KB Output is correct
19 Correct 212 ms 26032 KB Output is correct
20 Correct 212 ms 26008 KB Output is correct
21 Correct 219 ms 26020 KB Output is correct
22 Correct 167 ms 25236 KB Output is correct
23 Correct 173 ms 25404 KB Output is correct
24 Correct 123 ms 24468 KB Output is correct
25 Correct 217 ms 25884 KB Output is correct
26 Correct 211 ms 25884 KB Output is correct
27 Correct 201 ms 25816 KB Output is correct
28 Correct 154 ms 25232 KB Output is correct
29 Correct 152 ms 25236 KB Output is correct
30 Correct 85 ms 24468 KB Output is correct
31 Correct 226 ms 25516 KB Output is correct
32 Correct 228 ms 26888 KB Output is correct
33 Correct 210 ms 25968 KB Output is correct
34 Correct 175 ms 24804 KB Output is correct
35 Correct 175 ms 24872 KB Output is correct
36 Correct 128 ms 25028 KB Output is correct
37 Correct 221 ms 25716 KB Output is correct
38 Correct 226 ms 25348 KB Output is correct
39 Correct 215 ms 26860 KB Output is correct
40 Correct 155 ms 25280 KB Output is correct
41 Correct 157 ms 25268 KB Output is correct
42 Correct 85 ms 24548 KB Output is correct
43 Correct 242 ms 27480 KB Output is correct
44 Correct 255 ms 27496 KB Output is correct
45 Correct 242 ms 28864 KB Output is correct
46 Correct 181 ms 25972 KB Output is correct
47 Correct 180 ms 25996 KB Output is correct
48 Correct 118 ms 25132 KB Output is correct
49 Correct 238 ms 27252 KB Output is correct
50 Correct 237 ms 28936 KB Output is correct
51 Correct 235 ms 28580 KB Output is correct
52 Correct 164 ms 25968 KB Output is correct
53 Correct 158 ms 25972 KB Output is correct