#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define OPT ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define endl "\n"
#define all(v) v.begin(), v.end()
#define mpr make_pair
#define pb push_back
#define ts to_string
#define fi first
#define se second
#define inf 0x3F3F3F3F
#define bpc __builtin_popcount
#define print(v) for(int i = 0; i < v.size(); i++) \
cout << v[i] << " "; \
cout<<endl;
struct MTree
{
int size;
vector<vector<int>> tree;
void init(int n)
{
size = 1;
while (size < n) size *= 2;
tree.resize(2*size);
}
void merge(vector<int> &a, vector<int> &b, vector<int> &f)
{
int i = 0;
int j = 0;
while (i < a.size() && j < b.size())
{
if (a[i] <= b[j])
{
f.pb(a[i]);
i++;
}
else
{
f.pb(b[j]);
j++;
}
}
while (i < a.size())
{
f.pb(a[i]);
i++;
}
while (j < b.size())
{
f.pb(b[j]);
j++;
}
}
void _build(int l, int r, int x, vector<pii> &arr)
{
if (l == r) return;
if (l + 1 == r)
{
if (l >= arr.size()) tree[x].pb(inf);
else tree[x].pb(arr[l].se);
return;
}
int mid = (l + r) >> 1;
_build(l, mid, 2*x+1, arr);
_build(mid, r, 2*x+2, arr);
merge(tree[2*x+1], tree[2*x+2], tree[x]);
}
void build(vector<pii> &arr)
{
_build(0, size, 0, arr);
}
int get(int lx, int rx, int l, int r, int x, int val)
{
if (l >= rx || r <= lx) return 0;
if (l >= lx && r <= rx)
{
return lower_bound(tree[x].begin(), tree[x].end(), val) - tree[x].begin();
}
int mid = (l + r) >> 1;
return get(lx, rx, l, mid, 2*x+1, val) + get(lx, rx, mid, r, 2*x+2, val);
}
int get(int l, int r, int val)
{
return get(l, r, 0, size, 0, val);
}
};
struct MergeTree
{
int size;
vector<vector<pii>> tree;
vector<MTree> sub;
void init(int &n)
{
size = 1;
while (size < n) size *= 2;
tree.resize(2*size);
sub.resize(2*size);
}
void merge(vector<pii> &a, vector<pii> &b, vector<pii> &f)
{
int i = 0;
int j = 0;
while (i < a.size() && j < b.size())
{
if (a[i].fi <= b[j].fi)
{
f.pb(a[i]);
i++;
}
else
{
f.pb(b[j]);
j++;
}
}
while (i < a.size())
{
f.pb(a[i]);
i++;
}
while (j < b.size())
{
f.pb(b[j]);
j++;
}
}
void _build(int l, int r, int x, vector<pii> &arr)
{
if (l == r) return;
if (l + 1 == r)
{
if (l >= arr.size())
{
tree[x].pb(mpr(inf, inf));
sub[x].init(tree[x].size());
sub[x].build(tree[x]);
}
else
{
tree[x].pb(arr[l]);
sub[x].init(tree[x].size());
sub[x].build(tree[x]);
}
return;
}
int mid = (l + r) >> 1;
_build(l, mid, 2*x+1, arr);
_build(mid, r, 2*x+2, arr);
merge(tree[2*x+1], tree[2*x+2], tree[x]);
sub[x].init(tree[x].size());
sub[x].build(tree[x]);
}
void build(vector<pii> &arr)
{
_build(0, size, 0, arr);
}
int get(int lx, int rx, int l, int r, int x, pii val)
{
if (l >= rx || r <= lx) return 0;
if (l >= lx && r <= rx)
{
int ind = lower_bound(tree[x].begin(), tree[x].end(), mpr(val.fi, 0)) - tree[x].begin();
return sub[x].get(ind, tree[x].size(), val.se) + ind;
}
int mid = (l + r) >> 1;
return get(lx, rx, l, mid, 2*x+1, val) + get(lx, rx, mid, r, 2*x+2, val);
}
int get(int l, int r, pii val)
{
return get(l, r, 0, size, 0, val);
}
};
int n;
int main()
{
int q;
cin >> n >> q;
vector<pii> v(n);
for (int i = 0; i < n; i++)
{
cin >> v[i].fi >> v[i].se;
}
sort(all(v));
vector<pii> arr;
for (int i = 0; i < n; i++)
{
arr.pb(mpr(v[i].se, v[i].fi + v[i].se));
}
MergeTree st;
st.init(n);
st.build(arr);
while (q--)
{
int a, b, c;
cin >> a >> b >> c;
int ind = lower_bound(v.begin(), v.end(), mpr(a, b)) - v.begin();
int rem = n - ind;
rem -= st.get(ind, n, mpr(b, c));
cout << rem << endl;
}
}
Compilation message
examination.cpp: In member function 'void MTree::merge(std::vector<int>&, std::vector<int>&, std::vector<int>&)':
examination.cpp:45:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while (i < a.size() && j < b.size())
| ~~^~~~~~~~~~
examination.cpp:45:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | while (i < a.size() && j < b.size())
| ~~^~~~~~~~~~
examination.cpp:59:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | while (i < a.size())
| ~~^~~~~~~~~~
examination.cpp:65:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | while (j < b.size())
| ~~^~~~~~~~~~
examination.cpp: In member function 'void MTree::_build(int, int, int, std::vector<std::pair<int, int> >&)':
examination.cpp:77:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if (l >= arr.size()) tree[x].pb(inf);
| ~~^~~~~~~~~~~~~
examination.cpp: In member function 'void MergeTree::merge(std::vector<std::pair<int, int> >&, std::vector<std::pair<int, int> >&, std::vector<std::pair<int, int> >&)':
examination.cpp:136:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | while (i < a.size() && j < b.size())
| ~~^~~~~~~~~~
examination.cpp:136:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
136 | while (i < a.size() && j < b.size())
| ~~^~~~~~~~~~
examination.cpp:150:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
150 | while (i < a.size())
| ~~^~~~~~~~~~
examination.cpp:156:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | while (j < b.size())
| ~~^~~~~~~~~~
examination.cpp: In member function 'void MergeTree::_build(int, int, int, std::vector<std::pair<int, int> >&)':
examination.cpp:168:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
168 | if (l >= arr.size())
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
244 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
31 ms |
7740 KB |
Output is correct |
8 |
Correct |
30 ms |
7764 KB |
Output is correct |
9 |
Correct |
32 ms |
7764 KB |
Output is correct |
10 |
Correct |
29 ms |
7764 KB |
Output is correct |
11 |
Correct |
27 ms |
7864 KB |
Output is correct |
12 |
Correct |
24 ms |
7764 KB |
Output is correct |
13 |
Correct |
31 ms |
7824 KB |
Output is correct |
14 |
Correct |
29 ms |
7764 KB |
Output is correct |
15 |
Correct |
30 ms |
7860 KB |
Output is correct |
16 |
Correct |
24 ms |
7764 KB |
Output is correct |
17 |
Correct |
31 ms |
7928 KB |
Output is correct |
18 |
Correct |
20 ms |
7836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1678 ms |
349836 KB |
Output is correct |
2 |
Correct |
1629 ms |
349868 KB |
Output is correct |
3 |
Correct |
1639 ms |
349852 KB |
Output is correct |
4 |
Correct |
1415 ms |
349776 KB |
Output is correct |
5 |
Correct |
1291 ms |
349792 KB |
Output is correct |
6 |
Correct |
1106 ms |
349824 KB |
Output is correct |
7 |
Correct |
1563 ms |
349736 KB |
Output is correct |
8 |
Correct |
1512 ms |
349904 KB |
Output is correct |
9 |
Correct |
1398 ms |
349940 KB |
Output is correct |
10 |
Correct |
1044 ms |
349544 KB |
Output is correct |
11 |
Correct |
1290 ms |
349588 KB |
Output is correct |
12 |
Correct |
1119 ms |
349492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1678 ms |
349836 KB |
Output is correct |
2 |
Correct |
1629 ms |
349868 KB |
Output is correct |
3 |
Correct |
1639 ms |
349852 KB |
Output is correct |
4 |
Correct |
1415 ms |
349776 KB |
Output is correct |
5 |
Correct |
1291 ms |
349792 KB |
Output is correct |
6 |
Correct |
1106 ms |
349824 KB |
Output is correct |
7 |
Correct |
1563 ms |
349736 KB |
Output is correct |
8 |
Correct |
1512 ms |
349904 KB |
Output is correct |
9 |
Correct |
1398 ms |
349940 KB |
Output is correct |
10 |
Correct |
1044 ms |
349544 KB |
Output is correct |
11 |
Correct |
1290 ms |
349588 KB |
Output is correct |
12 |
Correct |
1119 ms |
349492 KB |
Output is correct |
13 |
Correct |
1796 ms |
349820 KB |
Output is correct |
14 |
Correct |
1649 ms |
349900 KB |
Output is correct |
15 |
Correct |
1579 ms |
349888 KB |
Output is correct |
16 |
Correct |
1452 ms |
349768 KB |
Output is correct |
17 |
Correct |
1357 ms |
349840 KB |
Output is correct |
18 |
Correct |
1126 ms |
349980 KB |
Output is correct |
19 |
Correct |
1707 ms |
349792 KB |
Output is correct |
20 |
Correct |
1715 ms |
349860 KB |
Output is correct |
21 |
Correct |
1696 ms |
350012 KB |
Output is correct |
22 |
Correct |
1112 ms |
349624 KB |
Output is correct |
23 |
Correct |
1123 ms |
349528 KB |
Output is correct |
24 |
Correct |
971 ms |
349444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
244 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
31 ms |
7740 KB |
Output is correct |
8 |
Correct |
30 ms |
7764 KB |
Output is correct |
9 |
Correct |
32 ms |
7764 KB |
Output is correct |
10 |
Correct |
29 ms |
7764 KB |
Output is correct |
11 |
Correct |
27 ms |
7864 KB |
Output is correct |
12 |
Correct |
24 ms |
7764 KB |
Output is correct |
13 |
Correct |
31 ms |
7824 KB |
Output is correct |
14 |
Correct |
29 ms |
7764 KB |
Output is correct |
15 |
Correct |
30 ms |
7860 KB |
Output is correct |
16 |
Correct |
24 ms |
7764 KB |
Output is correct |
17 |
Correct |
31 ms |
7928 KB |
Output is correct |
18 |
Correct |
20 ms |
7836 KB |
Output is correct |
19 |
Correct |
1678 ms |
349836 KB |
Output is correct |
20 |
Correct |
1629 ms |
349868 KB |
Output is correct |
21 |
Correct |
1639 ms |
349852 KB |
Output is correct |
22 |
Correct |
1415 ms |
349776 KB |
Output is correct |
23 |
Correct |
1291 ms |
349792 KB |
Output is correct |
24 |
Correct |
1106 ms |
349824 KB |
Output is correct |
25 |
Correct |
1563 ms |
349736 KB |
Output is correct |
26 |
Correct |
1512 ms |
349904 KB |
Output is correct |
27 |
Correct |
1398 ms |
349940 KB |
Output is correct |
28 |
Correct |
1044 ms |
349544 KB |
Output is correct |
29 |
Correct |
1290 ms |
349588 KB |
Output is correct |
30 |
Correct |
1119 ms |
349492 KB |
Output is correct |
31 |
Correct |
1796 ms |
349820 KB |
Output is correct |
32 |
Correct |
1649 ms |
349900 KB |
Output is correct |
33 |
Correct |
1579 ms |
349888 KB |
Output is correct |
34 |
Correct |
1452 ms |
349768 KB |
Output is correct |
35 |
Correct |
1357 ms |
349840 KB |
Output is correct |
36 |
Correct |
1126 ms |
349980 KB |
Output is correct |
37 |
Correct |
1707 ms |
349792 KB |
Output is correct |
38 |
Correct |
1715 ms |
349860 KB |
Output is correct |
39 |
Correct |
1696 ms |
350012 KB |
Output is correct |
40 |
Correct |
1112 ms |
349624 KB |
Output is correct |
41 |
Correct |
1123 ms |
349528 KB |
Output is correct |
42 |
Correct |
971 ms |
349444 KB |
Output is correct |
43 |
Correct |
1763 ms |
349816 KB |
Output is correct |
44 |
Correct |
1793 ms |
349764 KB |
Output is correct |
45 |
Correct |
1737 ms |
349796 KB |
Output is correct |
46 |
Correct |
1499 ms |
349748 KB |
Output is correct |
47 |
Correct |
1413 ms |
349792 KB |
Output is correct |
48 |
Correct |
1164 ms |
349544 KB |
Output is correct |
49 |
Correct |
1779 ms |
349760 KB |
Output is correct |
50 |
Correct |
1677 ms |
349676 KB |
Output is correct |
51 |
Correct |
1733 ms |
349784 KB |
Output is correct |
52 |
Correct |
1204 ms |
349568 KB |
Output is correct |
53 |
Correct |
1203 ms |
349616 KB |
Output is correct |