Submission #958011

# Submission time Handle Problem Language Result Execution time Memory
958011 2024-04-04T16:19:20 Z steveonalex Examination (JOI19_examination) C++17
100 / 100
359 ms 27500 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()

ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}

ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return 63 - __builtin_clzll(mask);}

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
// mt19937_64 rng(69);
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}

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 T>
    void printArr(T& container, string separator = " ", string finish = "\n", ostream &out = cout){
        for(auto item: container) out << item << separator;
        out << finish;
    }

template <class T>
    void remove_dup(vector<T> &a){
        sort(ALL(a));
        a.resize(unique(ALL(a)) - a.begin());
    }

const int INF = 1e9 + 69;

struct FenwickTree{
    int n;
    vector<vector<int>> a;
    vector<vector<int>> val;

    FenwickTree(int _n){
        n = _n;
        a.resize(n + 1); val.resize(n + 1);
    }

    void fake_update(int i, int j){
        while(i <= n){
            val[i].push_back(j);
            i += LASTBIT(i);
        }
    }

    void build_tree(){
        for(int i = 1; i<=n; ++i) {
            val[i].push_back(-INF);
            remove_dup(val[i]);
            a[i].resize(val[i].size());
        }
    }

    void update(int i, int j, int v){
        while(i <= n){
            int idx = lower_bound(ALL(val[i]), j) - val[i].begin();
            while(idx < val[i].size()){
                a[i][idx] += v;
                idx += LASTBIT(idx); 
            }
            i += LASTBIT(i);
        }
    }

    int get(int i, int j){
        int ans = 0;
        while(i > 0){
            int idx = upper_bound(ALL(val[i]), j) - val[i].begin() - 1;
            while(idx > 0){
                ans += a[i][idx];
                idx -= LASTBIT(idx);
            }
            i -= LASTBIT(i);
        }
        return ans;
    }
};

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    int n, q; cin >> n >> q;
    vector<array<int, 2>> a(n);
    for(int i = 0; i<n; ++i) for(int &j: a[i]) {
        cin >> j;
        j *= -1;
    }

    vector<array<int, 4>> query(q);
    for(int i = 0; i<q; ++i) {
        query[i][3] = i;
        for(int t = 0; t < 3; ++t) {
            int &j = query[i][t];
            cin >> j;
            j *= -1;
        }
    }

    vector<int> b;
    for(int i = 0; i<n; ++i) b.push_back(a[i][0]);
    remove_dup(b);
    
    FenwickTree bit(b.size());
    for(int i = 0; i<n; ++i){
        int idx = lower_bound(ALL(b), a[i][0]) - b.begin() + 1;
        bit.fake_update(idx, a[i][1]);
    }

    bit.build_tree();

    sort(ALL(a), [] (array<int, 2> x, array<int, 2> y) {return x[0] + x[1] > y[0] + y[1];});
    sort(ALL(query), [] (array<int, 4> x, array<int, 4> y) {return x[2] > y[2];});

    // for(auto i: a) printArr(i);
    // for(auto i: query) printArr(i);

    vector<int> ans(q);
    while(query.size()){
        if (a.size() && a.back()[0] + a.back()[1] <= query.back()[2]){
            int idx = lower_bound(ALL(b), a.back()[0]) - b.begin() + 1;
            bit.update(idx, a.back()[1], 1);
            a.pop_back();
        }
        else{
            int idx = upper_bound(ALL(b), query.back()[0]) - b.begin();
            ans[query.back()[3]] = bit.get(idx, query.back()[1]);
            query.pop_back();
        }
    }


    printArr(ans, "\n");

    return 0;
}

Compilation message

examination.cpp: In member function 'void FenwickTree::update(int, int, int)':
examination.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   77 |             while(idx < val[i].size()){
      |                   ~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 6 ms 1168 KB Output is correct
8 Correct 6 ms 1116 KB Output is correct
9 Correct 6 ms 1116 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 4 ms 604 KB Output is correct
12 Correct 2 ms 600 KB Output is correct
13 Correct 5 ms 1116 KB Output is correct
14 Correct 6 ms 1116 KB Output is correct
15 Correct 5 ms 1108 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 4 ms 856 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 18348 KB Output is correct
2 Correct 289 ms 18340 KB Output is correct
3 Correct 285 ms 18388 KB Output is correct
4 Correct 149 ms 15568 KB Output is correct
5 Correct 95 ms 7012 KB Output is correct
6 Correct 49 ms 6096 KB Output is correct
7 Correct 282 ms 19192 KB Output is correct
8 Correct 265 ms 19408 KB Output is correct
9 Correct 274 ms 19112 KB Output is correct
10 Correct 59 ms 6356 KB Output is correct
11 Correct 111 ms 15880 KB Output is correct
12 Correct 32 ms 5208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 313 ms 18348 KB Output is correct
2 Correct 289 ms 18340 KB Output is correct
3 Correct 285 ms 18388 KB Output is correct
4 Correct 149 ms 15568 KB Output is correct
5 Correct 95 ms 7012 KB Output is correct
6 Correct 49 ms 6096 KB Output is correct
7 Correct 282 ms 19192 KB Output is correct
8 Correct 265 ms 19408 KB Output is correct
9 Correct 274 ms 19112 KB Output is correct
10 Correct 59 ms 6356 KB Output is correct
11 Correct 111 ms 15880 KB Output is correct
12 Correct 32 ms 5208 KB Output is correct
13 Correct 319 ms 19888 KB Output is correct
14 Correct 293 ms 19696 KB Output is correct
15 Correct 286 ms 19300 KB Output is correct
16 Correct 151 ms 16704 KB Output is correct
17 Correct 104 ms 8244 KB Output is correct
18 Correct 55 ms 6608 KB Output is correct
19 Correct 303 ms 19664 KB Output is correct
20 Correct 339 ms 19524 KB Output is correct
21 Correct 299 ms 20012 KB Output is correct
22 Correct 58 ms 6240 KB Output is correct
23 Correct 112 ms 15908 KB Output is correct
24 Correct 39 ms 5464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 352 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 6 ms 1168 KB Output is correct
8 Correct 6 ms 1116 KB Output is correct
9 Correct 6 ms 1116 KB Output is correct
10 Correct 4 ms 860 KB Output is correct
11 Correct 4 ms 604 KB Output is correct
12 Correct 2 ms 600 KB Output is correct
13 Correct 5 ms 1116 KB Output is correct
14 Correct 6 ms 1116 KB Output is correct
15 Correct 5 ms 1108 KB Output is correct
16 Correct 2 ms 600 KB Output is correct
17 Correct 4 ms 856 KB Output is correct
18 Correct 2 ms 604 KB Output is correct
19 Correct 313 ms 18348 KB Output is correct
20 Correct 289 ms 18340 KB Output is correct
21 Correct 285 ms 18388 KB Output is correct
22 Correct 149 ms 15568 KB Output is correct
23 Correct 95 ms 7012 KB Output is correct
24 Correct 49 ms 6096 KB Output is correct
25 Correct 282 ms 19192 KB Output is correct
26 Correct 265 ms 19408 KB Output is correct
27 Correct 274 ms 19112 KB Output is correct
28 Correct 59 ms 6356 KB Output is correct
29 Correct 111 ms 15880 KB Output is correct
30 Correct 32 ms 5208 KB Output is correct
31 Correct 319 ms 19888 KB Output is correct
32 Correct 293 ms 19696 KB Output is correct
33 Correct 286 ms 19300 KB Output is correct
34 Correct 151 ms 16704 KB Output is correct
35 Correct 104 ms 8244 KB Output is correct
36 Correct 55 ms 6608 KB Output is correct
37 Correct 303 ms 19664 KB Output is correct
38 Correct 339 ms 19524 KB Output is correct
39 Correct 299 ms 20012 KB Output is correct
40 Correct 58 ms 6240 KB Output is correct
41 Correct 112 ms 15908 KB Output is correct
42 Correct 39 ms 5464 KB Output is correct
43 Correct 359 ms 27500 KB Output is correct
44 Correct 357 ms 27376 KB Output is correct
45 Correct 337 ms 27184 KB Output is correct
46 Correct 179 ms 23252 KB Output is correct
47 Correct 110 ms 9428 KB Output is correct
48 Correct 53 ms 6348 KB Output is correct
49 Correct 319 ms 27088 KB Output is correct
50 Correct 322 ms 27092 KB Output is correct
51 Correct 302 ms 27128 KB Output is correct
52 Correct 74 ms 8044 KB Output is correct
53 Correct 139 ms 22136 KB Output is correct