#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define forn(i, n) for(int i = 0; i < n; ++i)
#define forbn(i, b, n) for(int i = b; i < n; ++i)
#define sz(v) (int)v.size()
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define min3(a, b, c) min(a, min(b, c))
#define at(m, k) (m.find(k) != m.end() ? m[k] : 0)
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long long ll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
template <class T> inline void mineq(T &a, T b) { a = min(a, b); }
template <class T> inline void maxeq(T &a, T b) { a = max(a, b); }
const int MX = 100 * 1000 + 10;
int n, q;
int a[MX], b[MX], c[MX];
vi a2i[MX], b2i[MX];
int cl[MX];
int S = 450;
struct Fenw {
int mx = MX - 1;
vi bit;
Fenw() {
bit.assign(MX, 0);
}
void upd(int i, int v) {
for(i += 1; i <= mx; i += i & (-i))
bit[i] += v;
}
int psum(int i) {
int sum = 0;
for(; i > 0; i -= i & (-i))
sum += bit[i];
return sum;
}
int query(int cc) {
return psum(mx) - psum(cc);
}
};
bool cmp(pair<ii, ii> p1, pair<ii, ii> p2) {
ii a = p1.F, b = p2.F;
if(cl[a.F] != cl[b.F])
return a.F < b.F;
return (cl[a.F] % 2) ? a.S > b.S : a.S < b.S;
}
struct Mo {
int l, r;
Fenw fenw;
void init(int l, int r) {
this->l = l, this->r = r;
forn(i, n)
if(a[i] >= l && b[i] >= r)
fenw.upd(c[i], 1);
}
void add_left() {
l -= 1;
for(int i: a2i[l])
if(b[i] >= r)
fenw.upd(c[i], 1);
else
break;
}
void pop_left() {
for(int i: a2i[l])
if(b[i] >= r)
fenw.upd(c[i], -1);
else
break;
l += 1;
}
void add_right() {
for(int i: b2i[r])
if(a[i] >= l)
fenw.upd(c[i], -1);
else
break;
r += 1;
}
void pop_right() {
r -= 1;
for(int i: b2i[r])
if(a[i] >= l)
fenw.upd(c[i], 1);
else
break;
}
int get_ans(int cc) {
return fenw.query(cc);
}
};
void comp(vi& v) {
set<int> sl;
for(int el: v)
sl.insert(el);
int ct = 0;
map<int, int> v2i;
for(int el: sl)
v2i[el] = ct++;
for(int& el: v)
el = v2i[el];
}
bool cmp1(int i, int j) {
return b[i] > b[j];
}
bool cmp2(int i, int j) {
return a[i] > a[j];
}
void solve()
{
cin >> n >> q;
vi as, bs, cs;
set<int> sas, sbs, scs;
int mx_x = -1, mx_y = -1, mx_z = -1;
forn(i, n) {
int x, y;
cin >> x >> y;
as.pb(x);
bs.pb(y);
cs.pb(x + y);
sas.insert(x);
sbs.insert(y);
scs.insert(x + y);
maxeq(mx_x, x);
maxeq(mx_y, y);
maxeq(mx_z, x + y);
}
forn(_, q) {
int l, r, cc;
cin >> l >> r >> cc;
if(sas.lower_bound(l) != sas.end())
l = *sas.lower_bound(l);
else
l = mx_x + 1;
if(sbs.lower_bound(r) != sbs.end())
r = *sbs.lower_bound(r);
else
r = mx_y + 1;
if(scs.lower_bound(cc) != scs.end())
cc = *scs.lower_bound(cc);
else
cc = mx_z + 1;
as.pb(l);
bs.pb(r);
cs.pb(cc);
}
comp(as);
comp(bs);
comp(cs);
forn(i, n)
{
a[i] = as[i];
b[i] = bs[i];
c[i] = cs[i];
a2i[a[i]].pb(i);
b2i[b[i]].pb(i);
}
forn(i, MX) {
sort(all(a2i[i]), cmp1);
sort(all(b2i[i]), cmp2);
}
int ct = 0, i = 0;
forn(l, MX) {
if(ct != 0 && ct + sz(a2i[l]) > S) {
ct = sz(a2i[l]);
i += 1;
} else {
ct += sz(a2i[l]);
}
cl[l] = i;
}
vector<pair<ii, ii>> qs;
forn(ind, q) {
int l = as[ind + n], r = bs[ind + n], cc = cs[ind + n];
qs.pb({{l, r}, {cc, ind}});
}
sort(all(qs), cmp);
Mo mo;
vi ans(q);
forn(i, q) {
int l = qs[i].F.F, r = qs[i].F.S, cc = qs[i].S.F, ind = qs[i].S.S;
if(i == 0) {
mo.init(l, r);
}
else {
while(l != mo.l || r != mo.r) {
if(l < mo.l)
mo.add_left();
else if(r > mo.r)
mo.add_right();
else if(l > mo.l)
mo.pop_left();
else
mo.pop_right();
}
}
ans[ind] = mo.get_ans(cc);
// cout << l << ' ' << r << ' ' << ans[ind] << '\n';
}
for(int el: ans)
cout << el << '\n';
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
// freopen("haybales.in", "r", stdin);
// freopen("haybales.out", "w", stdout);
int t = 1;
// cin >> t;
while(t--)
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
3 ms |
5708 KB |
Output is correct |
7 |
Correct |
23 ms |
6744 KB |
Output is correct |
8 |
Correct |
24 ms |
6740 KB |
Output is correct |
9 |
Correct |
23 ms |
6744 KB |
Output is correct |
10 |
Correct |
22 ms |
6564 KB |
Output is correct |
11 |
Correct |
12 ms |
6476 KB |
Output is correct |
12 |
Correct |
7 ms |
6092 KB |
Output is correct |
13 |
Correct |
29 ms |
6720 KB |
Output is correct |
14 |
Correct |
31 ms |
6724 KB |
Output is correct |
15 |
Correct |
29 ms |
6728 KB |
Output is correct |
16 |
Correct |
11 ms |
6476 KB |
Output is correct |
17 |
Correct |
19 ms |
6460 KB |
Output is correct |
18 |
Correct |
5 ms |
6092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1862 ms |
27296 KB |
Output is correct |
2 |
Correct |
1851 ms |
27424 KB |
Output is correct |
3 |
Correct |
1990 ms |
27396 KB |
Output is correct |
4 |
Correct |
1038 ms |
22892 KB |
Output is correct |
5 |
Correct |
386 ms |
22888 KB |
Output is correct |
6 |
Correct |
159 ms |
13268 KB |
Output is correct |
7 |
Correct |
1002 ms |
27536 KB |
Output is correct |
8 |
Correct |
1173 ms |
27380 KB |
Output is correct |
9 |
Correct |
1081 ms |
27492 KB |
Output is correct |
10 |
Correct |
326 ms |
22692 KB |
Output is correct |
11 |
Correct |
940 ms |
22780 KB |
Output is correct |
12 |
Correct |
67 ms |
12928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1862 ms |
27296 KB |
Output is correct |
2 |
Correct |
1851 ms |
27424 KB |
Output is correct |
3 |
Correct |
1990 ms |
27396 KB |
Output is correct |
4 |
Correct |
1038 ms |
22892 KB |
Output is correct |
5 |
Correct |
386 ms |
22888 KB |
Output is correct |
6 |
Correct |
159 ms |
13268 KB |
Output is correct |
7 |
Correct |
1002 ms |
27536 KB |
Output is correct |
8 |
Correct |
1173 ms |
27380 KB |
Output is correct |
9 |
Correct |
1081 ms |
27492 KB |
Output is correct |
10 |
Correct |
326 ms |
22692 KB |
Output is correct |
11 |
Correct |
940 ms |
22780 KB |
Output is correct |
12 |
Correct |
67 ms |
12928 KB |
Output is correct |
13 |
Correct |
1911 ms |
27416 KB |
Output is correct |
14 |
Correct |
1746 ms |
27480 KB |
Output is correct |
15 |
Correct |
1680 ms |
27424 KB |
Output is correct |
16 |
Correct |
1032 ms |
22956 KB |
Output is correct |
17 |
Correct |
441 ms |
23176 KB |
Output is correct |
18 |
Correct |
118 ms |
13236 KB |
Output is correct |
19 |
Correct |
1767 ms |
27420 KB |
Output is correct |
20 |
Correct |
1758 ms |
27424 KB |
Output is correct |
21 |
Correct |
1323 ms |
27524 KB |
Output is correct |
22 |
Correct |
341 ms |
22824 KB |
Output is correct |
23 |
Correct |
861 ms |
22744 KB |
Output is correct |
24 |
Correct |
67 ms |
12944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
5708 KB |
Output is correct |
2 |
Correct |
4 ms |
5708 KB |
Output is correct |
3 |
Correct |
4 ms |
5708 KB |
Output is correct |
4 |
Correct |
4 ms |
5708 KB |
Output is correct |
5 |
Correct |
3 ms |
5708 KB |
Output is correct |
6 |
Correct |
3 ms |
5708 KB |
Output is correct |
7 |
Correct |
23 ms |
6744 KB |
Output is correct |
8 |
Correct |
24 ms |
6740 KB |
Output is correct |
9 |
Correct |
23 ms |
6744 KB |
Output is correct |
10 |
Correct |
22 ms |
6564 KB |
Output is correct |
11 |
Correct |
12 ms |
6476 KB |
Output is correct |
12 |
Correct |
7 ms |
6092 KB |
Output is correct |
13 |
Correct |
29 ms |
6720 KB |
Output is correct |
14 |
Correct |
31 ms |
6724 KB |
Output is correct |
15 |
Correct |
29 ms |
6728 KB |
Output is correct |
16 |
Correct |
11 ms |
6476 KB |
Output is correct |
17 |
Correct |
19 ms |
6460 KB |
Output is correct |
18 |
Correct |
5 ms |
6092 KB |
Output is correct |
19 |
Correct |
1862 ms |
27296 KB |
Output is correct |
20 |
Correct |
1851 ms |
27424 KB |
Output is correct |
21 |
Correct |
1990 ms |
27396 KB |
Output is correct |
22 |
Correct |
1038 ms |
22892 KB |
Output is correct |
23 |
Correct |
386 ms |
22888 KB |
Output is correct |
24 |
Correct |
159 ms |
13268 KB |
Output is correct |
25 |
Correct |
1002 ms |
27536 KB |
Output is correct |
26 |
Correct |
1173 ms |
27380 KB |
Output is correct |
27 |
Correct |
1081 ms |
27492 KB |
Output is correct |
28 |
Correct |
326 ms |
22692 KB |
Output is correct |
29 |
Correct |
940 ms |
22780 KB |
Output is correct |
30 |
Correct |
67 ms |
12928 KB |
Output is correct |
31 |
Correct |
1911 ms |
27416 KB |
Output is correct |
32 |
Correct |
1746 ms |
27480 KB |
Output is correct |
33 |
Correct |
1680 ms |
27424 KB |
Output is correct |
34 |
Correct |
1032 ms |
22956 KB |
Output is correct |
35 |
Correct |
441 ms |
23176 KB |
Output is correct |
36 |
Correct |
118 ms |
13236 KB |
Output is correct |
37 |
Correct |
1767 ms |
27420 KB |
Output is correct |
38 |
Correct |
1758 ms |
27424 KB |
Output is correct |
39 |
Correct |
1323 ms |
27524 KB |
Output is correct |
40 |
Correct |
341 ms |
22824 KB |
Output is correct |
41 |
Correct |
861 ms |
22744 KB |
Output is correct |
42 |
Correct |
67 ms |
12944 KB |
Output is correct |
43 |
Correct |
2437 ms |
34544 KB |
Output is correct |
44 |
Correct |
2352 ms |
39440 KB |
Output is correct |
45 |
Correct |
2364 ms |
39332 KB |
Output is correct |
46 |
Correct |
1117 ms |
31716 KB |
Output is correct |
47 |
Correct |
520 ms |
31544 KB |
Output is correct |
48 |
Correct |
117 ms |
14008 KB |
Output is correct |
49 |
Correct |
1271 ms |
39320 KB |
Output is correct |
50 |
Correct |
1405 ms |
39476 KB |
Output is correct |
51 |
Correct |
1261 ms |
39284 KB |
Output is correct |
52 |
Correct |
514 ms |
31388 KB |
Output is correct |
53 |
Correct |
976 ms |
30620 KB |
Output is correct |