답안 #1097120

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097120 2024-10-06T06:42:12 Z TrinhKhanhDung Examination (JOI19_examination) C++14
100 / 100
209 ms 23044 KB
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18 + 10
#define INF (ll)1e9
#define MOD (ll)(998244353)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

struct fen{
    vector<int> tree;
    int n;

    fen(int _n = 0){
        n = _n;
        tree.resize(n + 3, 0);
    }

    void upd(int p, int c){
        for(; p <= n; p += p & -p) tree[p] += c;
    }

    int get(int p){
        int ans = 0;
        for(; p > 0; p -= p & -p) ans += tree[p];
        return ans;
    }

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

const int MAX = 1e5 + 10;

int N, Q;
int S[MAX], T[MAX], X[MAX], Y[MAX], Z[MAX], ans[MAX];

void process1(vector<pair<ll, ll>> A, vector<array<ll, 3>> queries){
    sort(ALL(A), greater<pair<ll, ll>>());
    sort(ALL(queries), greater<array<ll, 3>>());

    vector<ll> tmp;
    for(auto t: A) tmp.push_back(t.se);
    for(auto t: queries) tmp.push_back(t[1]);
    cps(tmp);

    auto pos = [&](ll c) -> int{
        return lower_bound(ALL(tmp), c) - tmp.begin() + 1;
    };

    fen bit(sz(tmp));
    int i = 0;
    for(auto x: queries){
        while(i < sz(A) && A[i].fi >= x[0]){
            bit.upd(pos(A[i].se), 1);
            i++;
        }
        ans[x[2]] += bit.get(pos(x[1]), sz(tmp));
    }
}

void process2(vector<pair<ll, ll>> A, vector<array<ll, 4>> queries){
    sort(ALL(A), greater<pair<ll, ll>>());
    sort(ALL(queries), greater<array<ll, 4>>());

    vector<ll> tmp;
    for(auto t: A) tmp.push_back(t.se);
    for(auto t: queries) tmp.push_back(t[1]), tmp.push_back(t[2]);
    cps(tmp);

    auto pos = [&](ll c) -> int{
        return lower_bound(ALL(tmp), c) - tmp.begin() + 1;
    };

    fen bit(sz(tmp));
    int i = 0;
    for(auto x: queries){
        while(i < sz(A) && A[i].fi >= x[0]){
            bit.upd(pos(A[i].se), 1);
            i++;
        }
        ans[x[3]] += bit.get(pos(x[1]), pos(x[2]));
    }
}

void solve(){
    cin >> N >> Q;
    for(int i = 1; i <= N; i++) cin >> S[i] >> T[i];
    for(int i = 1; i <= Q; i++) cin >> X[i] >> Y[i] >> Z[i];

    //situation 1: X[i] + Y[i] >= Z[i]
    vector<pair<ll, ll>> A;
    vector<array<ll, 3>> queries1;
    for(int i = 1; i <= N; i++) A.push_back({S[i], T[i]});
    for(int i = 1; i <= Q; i++) if(X[i] + Y[i] >= Z[i]){
        queries1.push_back({X[i], Y[i], i});
    }
    process1(A, queries1);

    //situation 2: X[i] + Y[i] < Z[i]
    A.clear();
    vector<array<ll, 4>> queries2;
    for(int i = 1; i <= N; i++) A.push_back({S[i] + T[i], S[i]});
    for(int i = 1; i <= Q; i++) if(X[i] + Y[i] < Z[i]){
        queries2.push_back({Z[i], X[i], Z[i] - Y[i], i});
    }
    process2(A, queries2);

    queries1.clear();
    A.clear();
    for(int i = 1; i <= N; i++) A.push_back({S[i], T[i]});
    for(int i = 1; i <= Q; i++) if(X[i] + Y[i] < Z[i]){
        queries1.push_back({Z[i] - Y[i] + 1, Y[i], i});
    }
    process1(A, queries1);

    //print result
    for(int i = 1; i <= Q; i++){
        cout << ans[i] << '\n';
    }
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("task.inp","r",stdin);
//    freopen("task.out","w",stdout);

    int T = 1;
//    cin >> T;
    for(; T; T--) solve();

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 1084 KB Output is correct
8 Correct 4 ms 1088 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 5 ms 888 KB Output is correct
11 Correct 4 ms 856 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 860 KB Output is correct
15 Correct 4 ms 860 KB Output is correct
16 Correct 3 ms 996 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 16704 KB Output is correct
2 Correct 124 ms 16588 KB Output is correct
3 Correct 122 ms 16580 KB Output is correct
4 Correct 80 ms 15816 KB Output is correct
5 Correct 96 ms 15816 KB Output is correct
6 Correct 77 ms 15128 KB Output is correct
7 Correct 124 ms 16572 KB Output is correct
8 Correct 113 ms 16576 KB Output is correct
9 Correct 112 ms 16320 KB Output is correct
10 Correct 89 ms 15804 KB Output is correct
11 Correct 72 ms 15812 KB Output is correct
12 Correct 52 ms 15096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 123 ms 16704 KB Output is correct
2 Correct 124 ms 16588 KB Output is correct
3 Correct 122 ms 16580 KB Output is correct
4 Correct 80 ms 15816 KB Output is correct
5 Correct 96 ms 15816 KB Output is correct
6 Correct 77 ms 15128 KB Output is correct
7 Correct 124 ms 16572 KB Output is correct
8 Correct 113 ms 16576 KB Output is correct
9 Correct 112 ms 16320 KB Output is correct
10 Correct 89 ms 15804 KB Output is correct
11 Correct 72 ms 15812 KB Output is correct
12 Correct 52 ms 15096 KB Output is correct
13 Correct 172 ms 16784 KB Output is correct
14 Correct 156 ms 16064 KB Output is correct
15 Correct 118 ms 16584 KB Output is correct
16 Correct 110 ms 16088 KB Output is correct
17 Correct 125 ms 16016 KB Output is correct
18 Correct 70 ms 14280 KB Output is correct
19 Correct 175 ms 16716 KB Output is correct
20 Correct 159 ms 16160 KB Output is correct
21 Correct 186 ms 18320 KB Output is correct
22 Correct 97 ms 15808 KB Output is correct
23 Correct 73 ms 15848 KB Output is correct
24 Correct 56 ms 15076 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 4 ms 1084 KB Output is correct
8 Correct 4 ms 1088 KB Output is correct
9 Correct 4 ms 980 KB Output is correct
10 Correct 5 ms 888 KB Output is correct
11 Correct 4 ms 856 KB Output is correct
12 Correct 3 ms 860 KB Output is correct
13 Correct 4 ms 860 KB Output is correct
14 Correct 4 ms 860 KB Output is correct
15 Correct 4 ms 860 KB Output is correct
16 Correct 3 ms 996 KB Output is correct
17 Correct 3 ms 860 KB Output is correct
18 Correct 2 ms 860 KB Output is correct
19 Correct 123 ms 16704 KB Output is correct
20 Correct 124 ms 16588 KB Output is correct
21 Correct 122 ms 16580 KB Output is correct
22 Correct 80 ms 15816 KB Output is correct
23 Correct 96 ms 15816 KB Output is correct
24 Correct 77 ms 15128 KB Output is correct
25 Correct 124 ms 16572 KB Output is correct
26 Correct 113 ms 16576 KB Output is correct
27 Correct 112 ms 16320 KB Output is correct
28 Correct 89 ms 15804 KB Output is correct
29 Correct 72 ms 15812 KB Output is correct
30 Correct 52 ms 15096 KB Output is correct
31 Correct 172 ms 16784 KB Output is correct
32 Correct 156 ms 16064 KB Output is correct
33 Correct 118 ms 16584 KB Output is correct
34 Correct 110 ms 16088 KB Output is correct
35 Correct 125 ms 16016 KB Output is correct
36 Correct 70 ms 14280 KB Output is correct
37 Correct 175 ms 16716 KB Output is correct
38 Correct 159 ms 16160 KB Output is correct
39 Correct 186 ms 18320 KB Output is correct
40 Correct 97 ms 15808 KB Output is correct
41 Correct 73 ms 15848 KB Output is correct
42 Correct 56 ms 15076 KB Output is correct
43 Correct 192 ms 18836 KB Output is correct
44 Correct 185 ms 18836 KB Output is correct
45 Correct 162 ms 18140 KB Output is correct
46 Correct 122 ms 17296 KB Output is correct
47 Correct 129 ms 17300 KB Output is correct
48 Correct 97 ms 16712 KB Output is correct
49 Correct 179 ms 18540 KB Output is correct
50 Correct 194 ms 20100 KB Output is correct
51 Correct 209 ms 23044 KB Output is correct
52 Correct 126 ms 17296 KB Output is correct
53 Correct 75 ms 16584 KB Output is correct