#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define taskname ""
#define ld long double
const int mod = 1e9+7;
using namespace std;
#define int ll
struct Q {
int x, y, z;
int id;
int type;
};
vector<Q> qs;
vector<ll> ans;
vector<int> bit;
int M;
void update(int i, int v) {
for (; i <= M; i += i & -i)
bit[i] += v;
}
int get(int i) {
int s = 0;
for (; i > 0; i -= i & -i)
s += bit[i];
return s;
}
void cdq(int l, int r) {
if (l >= r) return;
int m = (l + r) >> 1;
cdq(l, m);
cdq(m + 1, r);
vector<Q> tmp;
tmp.reserve(r - l + 1);
int i = l, j = m + 1, added = 0;
vector<int> to_clear;
while (i <= m && j <= r) {
if (qs[i].y >= qs[j].y) {
if (qs[i].type == 0) {
update(qs[i].z, 1);
to_clear.pb(qs[i].z);
added++;
}
tmp.pb(qs[i++]);
} else {
if (qs[j].type == 1) {
ans[qs[j].id] += added - get(qs[j].z - 1);
}
tmp.pb(qs[j++]);
}
}
while (i <= m) tmp.pb(qs[i++]);
while (j <= r) {
if (qs[j].type == 1) {
ans[qs[j].id] += added - get(qs[j].z - 1);
}
tmp.pb(qs[j++]);
}
for (int z : to_clear) update(z, -1);
for (int k = 0; k < tmp.size(); k++)
qs[l + k] = tmp[k];
}
signed main() {
if (fopen(taskname".inp","r")) {
freopen(taskname".inp","r",stdin);
freopen(taskname".out","w",stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, q;
cin >> n >> q;
qs.reserve(n + q);
ans.assign(q, 0LL);
vector<int> allz;
for (int i = 0; i < n; i++) {
int S, T; cin >> S >> T;
qs.pb({S, T, S + T, -1, 0});
allz.pb(S + T);
}
for (int i = 0; i < q; i++) {
int A, B, C; cin >> A >> B >> C;
qs.pb({A, B, C, i, 1});
allz.pb(C);
}
sort(allz.begin(), allz.end());
allz.erase(unique(allz.begin(), allz.end()), allz.end());
M = allz.size() + 2;
bit.assign(M + 1, 0);
for (auto &e : qs)
e.z = lower_bound(allz.begin(), allz.end(), e.z) - allz.begin() + 1;
sort(qs.begin(), qs.end(), [](auto &a, auto &b){
if (a.x != b.x) return a.x > b.x;
if (a.y != b.y) return a.y > b.y;
return a.type < b.type;
});
cdq(0, qs.size() - 1);
for (int i = 0; i < q; i++)
cout << ans[i] << "\n";
return 0;
}
Compilation message (stderr)
examination.cpp: In function 'int main()':
examination.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | freopen(taskname".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
80 | freopen(taskname".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |