This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
const ll N = 2e5 + 5;
const ll blocksz = 505;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
ll n, q;
ll cnt[N], ans[N];
map<ll, ll> mp;
struct Info{
ll x, y, z, id;
} a[N];
struct BIT{
ll b[N];
void upd(ll i, ll x){
for (; i <= n + q; i += i & (-i)) b[i] += x;
}
ll get(ll i){
ll ans = 0;
for (; i; i -= i & (-i)) ans += b[i];
return ans;
}
} ft;
vector<ll> record;
vector<Info> tmp;
void CDQ(ll l, ll r){
if (l == r) return;
ll m = l + r >> 1;
CDQ(l, m); CDQ(m + 1, r);
ll i = l, j = m + 1, sum = 0;
while (i <= m && j <= r){
if (a[i].y > a[j].y){
if (a[i].id <= n){
++sum; ft.upd(a[i].z, 1); record.push_back(a[i].z);
}
tmp.push_back(a[i++]);
}else{
ans[a[j].id] += sum - ft.get(a[j].z); tmp.push_back(a[j++]);
}
}
while (i <= m) tmp.push_back(a[i++]);
while (j <= r) ans[a[j].id] += sum - ft.get(a[j].z), tmp.push_back(a[j++]);
for (int i = l; i <= r; ++i) a[i] = tmp[i - l];
for (int i : record) ft.upd(i, -1);
vector<ll> ().swap(record);
vector<Info> ().swap(tmp);
}
void solve(){
cin >> n >> q;
vector<ll> v;
for (int i = 1; i <= n; ++i){
cin >> a[i].x >> a[i].y; a[i].z = a[i].x + a[i].y; a[i].id = i;
v.push_back(a[i].z);
}
for (int i = n + 1; i <= n + q; ++i){
cin >> a[i].x >> a[i].y >> a[i].z; a[i].id = i;
--a[i].x; --a[i].y; --a[i].z;
v.push_back(a[i].z);
}
sort(all(v));
v.erase(unique(all(v)), v.end());
for (int i = 0; i < v.size(); ++i) mp[v[i]] = i + 1;
for (int i = 1; i <= n + q; ++i) a[i].z = mp[a[i].z];
sort(a + 1, a + n + q + 1, [&](Info A, Info B){
return (A.x == B.x ? (A.y == B.y ? A.z < B.z : A.y < B.y) : A.x > B.x);
});
CDQ(1, n + q);
for (int i = n + 1; i <= n + q; ++i) cout << ans[i] << "\n";
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll run_case = 1; // cin >> run_case;
while (run_case--){
solve();
}
}
Compilation message (stderr)
examination.cpp: In function 'void CDQ(ll, ll)':
examination.cpp:42:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | ll m = l + r >> 1;
| ~~^~~
examination.cpp: In function 'void solve()':
examination.cpp:82:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for (int i = 0; i < v.size(); ++i) mp[v[i]] = i + 1;
| ~~^~~~~~~~~~
# | 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... |