#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int n, q;
vector<pair<pair<int, int>, int>> s;
vector<int> poi;
vector<int> poi1;
struct BIT2D
{
vector<int> po[maxn];
vector<int> bit[maxn];
void pre_upd(int x, int y)
{
for (int i = x; i >= 1; i -= i & (-i))
{
po[i].pb(y);
}
}
void pre_get(int x, int y)
{
for (int i = x; i <= (int)(poi.size()); i += i & (-i))
{
po[i].pb(y);
}
}
void init()
{
for (int i = 1; i <= (int)(poi.size()); i++)
{
sort(po[i].begin(), po[i].end());
po[i].resize(unique(po[i].begin(), po[i].end()) - po[i].begin());
bit[i].resize(po[i].size() + 1, 0);
}
}
void upd(int x, int y)
{
for (int i = x; i >= 1; i -= i & (-i))
{
int id = lower_bound(po[i].begin(), po[i].end(), y) - po[i].begin() + 1;
for (int j = id; j >= 1; j -= j & (-j))
{
bit[i][j] += 1;
}
}
}
int calc(int x, int y)
{
int res = 0;
for (int i = x; i <= (int)(poi.size()); i += i & (-i))
{
int id = lower_bound(po[i].begin(), po[i].end(), y) - po[i].begin() + 1;
for (int j = id; j < (int)(bit[i].size()); j += j & (-j))
{
res += bit[i][j];
}
}
return res;
}
} DS;
bool cmp(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b)
{
if (a.fi.fi == b.fi.fi)
return a.se < b.se;
return a.fi.fi < b.fi.fi;
}
int X[maxn];
int Y[maxn];
int A[maxn], B[maxn], C[maxn];
int ans[maxn];
signed main()
{
freopen("input.inp", "r", stdin);
freopen("output.out", "w", stdout);
cin.tie(0), cout.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
int x, y;
cin >> x >> y;
poi.pb(y);
poi1.pb(x + y);
X[i] = x;
Y[i] = y;
s.pb({{x, i}, 0});
}
for (int i = 1; i <= q; i++)
{
int a, b, c;
cin >> a >> b >> c;
A[i] = a;
B[i] = b;
C[i] = c;
poi.pb(b);
poi1.pb(c);
s.pb({{a, i}, 1});
}
sort(poi.begin(), poi.end());
sort(poi1.begin(), poi1.end());
poi.resize(unique(poi.begin(), poi.end()) - poi.begin());
poi1.resize(unique(poi1.begin(), poi1.end()) - poi1.begin());
for (int i = 1; i <= n; i++)
{
int d = X[i] + Y[i];
int c = lower_bound(poi.begin(), poi.end(), Y[i]) - poi.begin() + 1;
d = lower_bound(poi1.begin(), poi1.end(), d) - poi1.begin() + 1;
DS.pre_upd(c, d);
}
for (int i = 1; i <= q; i++)
{
int b = lower_bound(poi.begin(), poi.end(), B[i]) - poi.begin() + 1;
int c = lower_bound(poi1.begin(), poi1.end(), C[i]) - poi1.begin() + 1;
DS.pre_get(b, c);
}
sort(s.begin(), s.end());
DS.init();
for (int i = (int)(s.size()) - 1; i >= 0; i--)
{
int id = s[i].fi.se;
int type = s[i].se;
if (type == 0)
{
int d = X[id] + Y[id];
int c = lower_bound(poi.begin(), poi.end(), Y[id]) - poi.begin() + 1;
d = lower_bound(poi1.begin(), poi1.end(), d) - poi1.begin() + 1;
// cout << s[i].fi.fi << ' ' << c << ' ' << d << ' ' << type << ' ' << id << '\n';
DS.upd(c, d);
}
else
{
int b = lower_bound(poi.begin(), poi.end(), B[id]) - poi.begin() + 1;
int c = lower_bound(poi1.begin(), poi1.end(), C[id]) - poi1.begin() + 1;
// cout << s[i].fi.fi << ' ' << b << ' ' << c << ' ' << type << ' ' << id << '\n';
ans[id] = DS.calc(b, c);
}
}
for (int i = 1; i <= q; i++)
{
cout << ans[i] << '\n';
}
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:77:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:78:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | freopen("output.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
5076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |