#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
//#define int ll
struct query {
int a, b, sum, id;
query() {};
query(int a, int b, int sum, int id) : a(a), b(b), sum(sum), id(id) {};
};
const int N = 1e5 + 10;
unordered_map<int, int> t[N];
int ans[N], sz1, sz2;
inline void update(int a, int b) {
for (int i = a; i <= sz1; i += i & (-i)) {
for (int j = b; j <= sz2; j += j & (-j)) {
t[i][j]++;
}
}
}
inline int get(int a, int b) {
int res = 0;
for (int i = a; i > 0; i -= i & (-i)) {
for (int j = b; j > 0; j -= j & (-j)) {
res += t[i][j];
}
}
return res;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
vector<int> a(n), b(n), sa, sb, ss;
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
sa.pb(a[i]);
sb.pb(b[i]);
ss.pb(a[i] + b[i]);
}
vector<query> que(m);
for (int i = 0; i < m; i++) {
cin >> que[i].a >> que[i].b >> que[i].sum;
que[i].id = i;
sa.pb(que[i].a);
sb.pb(que[i].b);
ss.pb(que[i].sum);
}
sort(all(sa));
sort(all(sb));
sort(all(ss));
sa.resize(unique(all(sa)) - sa.begin());
sb.resize(unique(all(sb)) - sb.begin());
ss.resize(unique(all(ss)) - ss.begin());
vector<vector<pair<int, int>>> add(SZ(ss));
for (int i = 0; i < n; i++) {
int cs = lower_bound(all(ss), a[i] + b[i]) - ss.begin();
a[i] = upper_bound(all(sa), a[i]) - sa.begin();
b[i] = upper_bound(all(sb), b[i]) - sb.begin();
//cerr << a[i] << ' ' << b[i] << ' ' << cs << '\n';
add[cs].pb({a[i], b[i]});
}
vector<vector<pair<pair<int, int>, int>>> check(SZ(ss));
for (int i = 0; i < m; i++) {
que[i].sum = lower_bound(all(ss), que[i].sum) - ss.begin();
que[i].a = upper_bound(all(sa), que[i].a) - sa.begin();
que[i].b = upper_bound(all(sb), que[i].b) - sb.begin();
//cout << que[i].a << ' ' << que[i].b << ' ' << que[i].sum << '\n';
check[que[i].sum].pb(make_pair(make_pair(que[i].a, que[i].b), que[i].id));
}
sz1 = SZ(sa), sz2 = SZ(sb);
for (int i = SZ(ss) - 1; i >= 0; i--) {
for (auto it : add[i]) {
update(SZ(sa) - it.F + 1, SZ(sb) - it.S + 1);
}
for (auto it : check[i]) {
ans[it.S] = get(SZ(sa) - it.F.F + 1, SZ(sb) - it.F.S + 1);
}
}
for (int i = 0; i < m; i++) {
cout << ans[i] << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5880 KB |
Output is correct |
2 |
Correct |
8 ms |
5752 KB |
Output is correct |
3 |
Correct |
8 ms |
5752 KB |
Output is correct |
4 |
Correct |
9 ms |
5880 KB |
Output is correct |
5 |
Correct |
8 ms |
5752 KB |
Output is correct |
6 |
Correct |
9 ms |
5752 KB |
Output is correct |
7 |
Correct |
51 ms |
13048 KB |
Output is correct |
8 |
Correct |
49 ms |
13048 KB |
Output is correct |
9 |
Correct |
51 ms |
13048 KB |
Output is correct |
10 |
Correct |
18 ms |
7452 KB |
Output is correct |
11 |
Correct |
21 ms |
7416 KB |
Output is correct |
12 |
Correct |
12 ms |
6136 KB |
Output is correct |
13 |
Correct |
44 ms |
12796 KB |
Output is correct |
14 |
Correct |
50 ms |
12792 KB |
Output is correct |
15 |
Correct |
43 ms |
12792 KB |
Output is correct |
16 |
Correct |
14 ms |
6524 KB |
Output is correct |
17 |
Correct |
16 ms |
6904 KB |
Output is correct |
18 |
Correct |
11 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3092 ms |
278464 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3092 ms |
278464 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
5880 KB |
Output is correct |
2 |
Correct |
8 ms |
5752 KB |
Output is correct |
3 |
Correct |
8 ms |
5752 KB |
Output is correct |
4 |
Correct |
9 ms |
5880 KB |
Output is correct |
5 |
Correct |
8 ms |
5752 KB |
Output is correct |
6 |
Correct |
9 ms |
5752 KB |
Output is correct |
7 |
Correct |
51 ms |
13048 KB |
Output is correct |
8 |
Correct |
49 ms |
13048 KB |
Output is correct |
9 |
Correct |
51 ms |
13048 KB |
Output is correct |
10 |
Correct |
18 ms |
7452 KB |
Output is correct |
11 |
Correct |
21 ms |
7416 KB |
Output is correct |
12 |
Correct |
12 ms |
6136 KB |
Output is correct |
13 |
Correct |
44 ms |
12796 KB |
Output is correct |
14 |
Correct |
50 ms |
12792 KB |
Output is correct |
15 |
Correct |
43 ms |
12792 KB |
Output is correct |
16 |
Correct |
14 ms |
6524 KB |
Output is correct |
17 |
Correct |
16 ms |
6904 KB |
Output is correct |
18 |
Correct |
11 ms |
6136 KB |
Output is correct |
19 |
Execution timed out |
3092 ms |
278464 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |