#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = (int) 5e5 + 7;
const int B = 317;
int x[N], y[N], oldx[N], oldy[N], sum[N];
int n, q;
struct Query {
int a;
int b;
int c;
int id;
};
bool operator < (const Query &a, const Query &b) {
return a.a / B < b.a / B || (a.a / B == b.a / B && a.b < b.b);
}
Query queries[N];
int sol[N];
vector<int> inds[2][N];
bool inside[N];
int aib[N], total = 0;
void update(int x, int val) {
for (int i = x; i < N; i += i & -i) {
aib[i] += val;
}
}
int query(int x) {
int ret = 0;
for (int i = x; i >= 1; i -= i & -i) {
ret += aib[i];
}
return ret;
}
void contract(int a, bool where) {
for (auto &it : inds[where][a]) {
if (inside[it] == false) {
continue;
}
update(sum[it], -1);
inside[it] = false;
total--;
}
}
void expand(int a, bool where, int least) {
for (auto &it : inds[where][a]) {
if (inside[it] == true) {
continue;
}
int cur = (where == 0 ? y[it] : x[it]);
if (cur >= least) {
update(sum[it], +1);
inside[it] = true;
total++;
}
}
}
void Mo() {
int a = N - 1, b = N - 1;
for (int i = 1; i <= q; i++) {
int qa = queries[i].a;
int qb = queries[i].b;
while (a > qa) {
a--;
expand(a, 0, b);
}
while (b > qb) {
b--;
expand(b, 1, a);
}
while (a < qa) {
contract(a, 0);
a++;
}
while (b < qb) {
contract(b, 1);
b++;
}
sol[queries[i].id] = total - query(queries[i].c - 1);
}
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// freopen("input", "r", stdin);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> x[i] >> y[i];
}
for (int i = 1; i <= q; i++) {
cin >> queries[i].a >> queries[i].b >> queries[i].c;
queries[i].id = i;
}
{ /// normalizare
vector<int> vals;
map<int, int> mp;
for (int i = 1; i <= n; i++) {
vals.push_back(x[i] + y[i]);
}
for (int i = 1; i <= q; i++) {
vals.push_back(queries[i].c);
}
sort(vals.begin(), vals.end());
int cnt = 0;
for (auto &i : vals) {
if (mp[i] == 0) {
mp[i] = ++cnt;
}
}
for (int i = 1; i <= n; i++) {
sum[i] = mp[x[i] + y[i]];
}
for (int i = 1; i <= q; i++) {
queries[i].c = mp[queries[i].c];
}
vals.clear();
mp.clear();
for (int i = 1; i <= n; i++) {
vals.push_back(x[i]);
vals.push_back(y[i]);
}
for (int i = 1; i <= q; i++) {
vals.push_back(queries[i].a);
vals.push_back(queries[i].b);
}
sort(vals.begin(), vals.end());
cnt = 0;
for (auto &i : vals) {
if (mp[i] == 0) {
mp[i] = ++cnt;
}
}
for (int i = 1; i <= n; i++) {
oldx[i] = x[i];
oldy[i] = y[i];
x[i] = mp[x[i]];
y[i] = mp[y[i]];
}
for (int i = 1; i <= q; i++) {
queries[i].a = mp[queries[i].a];
queries[i].b = mp[queries[i].b];
}
for (int i = 1; i <= n; i++) {
inds[0][x[i]].push_back(i);
inds[1][y[i]].push_back(i);
}
}
sort(queries + 1, queries + q + 1);
Mo();
for (int i = 1; i <= q; i++) {
cout << sol[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23864 KB |
Output is correct |
2 |
Correct |
15 ms |
23856 KB |
Output is correct |
3 |
Correct |
15 ms |
23892 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
23820 KB |
Output is correct |
7 |
Correct |
34 ms |
25028 KB |
Output is correct |
8 |
Correct |
33 ms |
25000 KB |
Output is correct |
9 |
Correct |
33 ms |
24928 KB |
Output is correct |
10 |
Correct |
30 ms |
24588 KB |
Output is correct |
11 |
Correct |
57 ms |
24596 KB |
Output is correct |
12 |
Correct |
73 ms |
24176 KB |
Output is correct |
13 |
Correct |
32 ms |
24916 KB |
Output is correct |
14 |
Correct |
31 ms |
24976 KB |
Output is correct |
15 |
Correct |
32 ms |
24880 KB |
Output is correct |
16 |
Correct |
84 ms |
24476 KB |
Output is correct |
17 |
Correct |
24 ms |
24600 KB |
Output is correct |
18 |
Correct |
54 ms |
24240 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2592 ms |
39824 KB |
Output is correct |
2 |
Correct |
2660 ms |
41296 KB |
Output is correct |
3 |
Correct |
2599 ms |
41236 KB |
Output is correct |
4 |
Correct |
1072 ms |
39048 KB |
Output is correct |
5 |
Execution timed out |
3080 ms |
38580 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2592 ms |
39824 KB |
Output is correct |
2 |
Correct |
2660 ms |
41296 KB |
Output is correct |
3 |
Correct |
2599 ms |
41236 KB |
Output is correct |
4 |
Correct |
1072 ms |
39048 KB |
Output is correct |
5 |
Execution timed out |
3080 ms |
38580 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
23864 KB |
Output is correct |
2 |
Correct |
15 ms |
23856 KB |
Output is correct |
3 |
Correct |
15 ms |
23892 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
14 ms |
23892 KB |
Output is correct |
6 |
Correct |
15 ms |
23820 KB |
Output is correct |
7 |
Correct |
34 ms |
25028 KB |
Output is correct |
8 |
Correct |
33 ms |
25000 KB |
Output is correct |
9 |
Correct |
33 ms |
24928 KB |
Output is correct |
10 |
Correct |
30 ms |
24588 KB |
Output is correct |
11 |
Correct |
57 ms |
24596 KB |
Output is correct |
12 |
Correct |
73 ms |
24176 KB |
Output is correct |
13 |
Correct |
32 ms |
24916 KB |
Output is correct |
14 |
Correct |
31 ms |
24976 KB |
Output is correct |
15 |
Correct |
32 ms |
24880 KB |
Output is correct |
16 |
Correct |
84 ms |
24476 KB |
Output is correct |
17 |
Correct |
24 ms |
24600 KB |
Output is correct |
18 |
Correct |
54 ms |
24240 KB |
Output is correct |
19 |
Correct |
2592 ms |
39824 KB |
Output is correct |
20 |
Correct |
2660 ms |
41296 KB |
Output is correct |
21 |
Correct |
2599 ms |
41236 KB |
Output is correct |
22 |
Correct |
1072 ms |
39048 KB |
Output is correct |
23 |
Execution timed out |
3080 ms |
38580 KB |
Time limit exceeded |
24 |
Halted |
0 ms |
0 KB |
- |