#include <bits/stdc++.h>
#define fi first
#define se second
#define FOR(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define FORD(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)
template<typename T>
void __prine_one(const char *&s, const T &x)
{
while (*s == ' ') ++s;
const char *p = s;
int bal = 0;
while (*s)
{
if (*s == '(') ++bal;
else if (*s == ')') --bal;
else if (*s == ',' && bal == 0) break;
++s;
}
cerr.write(p, s - p) << " = " << x;
if (*s == ',')
{
cerr << " , ";
++s;
}
}
template<typename... Args>
void debug(const char *s, Args... args)
{
cerr << "[ ";
int dummy[] = {0, (__prine_one(s, args), 0)...};
(void)dummy;
cerr << " ]\n\n";
}
template<class X, class Y>
bool maximize(const X &a, const X &b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
template<class X, class Y>
bool minimize(const X &a, const X &b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
// --------------------------------------------------------------------------------------------
int n, q, ans[100005];
vector<int> ids;
struct Obj
{
ll A, B, C;
int id;
bool operator < (const Obj &other) const
{
if (A != other.A) return A > other.A;
if (B != other.B) return B > other.B;
if (C != other.C) return C > other.C;
return id < other.id;
}
};
vector<Obj> vecs;
// --------------------------------------------------------------------------------------------
struct FenTree
{
vector<int> fw;
void init(int n)
{
fw.assign(n + 5, 0);
}
void upd(int pos, int val)
{
for (; pos; pos &= pos - 1)
fw[pos] += val;
}
int get(int pos)
{
int res = 0;
for (; pos < (int)fw.size(); pos += pos & -pos)
res += fw[pos];
return res;
}
} fw;
void CDQ(int l, int r)
{
if (l == r)
return;
int mid = l + r >> 1;
vector<Obj> con_left, ask_right;
FOR(i, l, mid)
if (vecs[i].id == 0)
con_left.push_back(vecs[i]);
FOR(i, mid + 1, r)
if (vecs[i].id)
ask_right.push_back(vecs[i]);
sort(all(con_left), [&](const Obj &a, const Obj &b) {
return a.B > b.B;
});
sort(all(ask_right), [&](const Obj &a, const Obj &b) {
return a.B > b.B;
});
int j = 0;
for (int i = 0; i < (int)ask_right.size(); ++i)
{
while (j < (int)con_left.size() && con_left[j].B >= ask_right[i].B)
{
fw.upd(con_left[j].C, 1);
++j;
}
ans[ask_right[i].id] += fw.get(ask_right[i].C);
}
FOR(i, 0, j - 1)
fw.upd(con_left[i].C, -1);
CDQ(l, mid);
CDQ(mid + 1, r);
}
void solve()
{
cin >> n >> q;
FOR(i, 1, n)
{
int A, B; cin >> A >> B;
vecs.push_back({A, B, A + B, 0});
ids.push_back(A + B);
}
FOR(iq, 1, q)
{
int A, B, C; cin >> A >> B >> C;
vecs.push_back({A, B, C, iq});
}
uni(ids);
for (auto &it : vecs)
{
it.C = lower_bound(all(ids), it.C) - ids.begin() + 1;
}
sort(all(vecs));
fw.init((int)ids.size());
CDQ(0, (int)vecs.size() - 1);
FOR(iq, 1, q)
cout << ans[iq] << '\n';
}
signed main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define TASK "TEST"
if (fopen(TASK".INP", "r"))
{
freopen(TASK".INP", "r", stdin);
freopen(TASK".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message (stderr)
examination.cpp: In function 'int main()':
examination.cpp:189:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
189 | freopen(TASK".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:190:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
190 | freopen(TASK".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... |