#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.empty() || 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 |
0 ms |
344 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 |
1116 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 |
3 ms |
604 KB |
Output is correct |
12 |
Incorrect |
2 ms |
564 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
19156 KB |
Output is correct |
2 |
Correct |
290 ms |
19304 KB |
Output is correct |
3 |
Correct |
282 ms |
19184 KB |
Output is correct |
4 |
Correct |
152 ms |
16380 KB |
Output is correct |
5 |
Correct |
99 ms |
7636 KB |
Output is correct |
6 |
Incorrect |
50 ms |
6524 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
19156 KB |
Output is correct |
2 |
Correct |
290 ms |
19304 KB |
Output is correct |
3 |
Correct |
282 ms |
19184 KB |
Output is correct |
4 |
Correct |
152 ms |
16380 KB |
Output is correct |
5 |
Correct |
99 ms |
7636 KB |
Output is correct |
6 |
Incorrect |
50 ms |
6524 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1116 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 |
3 ms |
604 KB |
Output is correct |
12 |
Incorrect |
2 ms |
564 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |