#include <bits/stdc++.h>
// #ifndef ONLINE_JUDGE
// #include "/Users/quocbaonguyentran/Documents/VOI25/template/debug.cpp"
// #else
// #define debug(...) ;
// #endif
using namespace std;
#define endl "\n"
#define ll long long
struct Fenwick
{
vector<int> bit;
Fenwick() {};
Fenwick(int n) : bit(n + 1, 0) {}
void update(int id, int val)
{
for (; id < int(bit.size()); id += id & (-id))
{
bit[id] += val;
}
}
int sum(int id)
{
int sum = 0;
for (; id > 0; id -= id & (-id))
{
sum += bit[id];
}
return sum;
}
int query(int l, int r)
{
return sum(r) - sum(l - 1);
}
} tree;
vector<int> d;
int res[200005];
int n, s[100005], t[100005], q;
int qe[200005];
int ans[200005];
vector<tuple<int, int, int, int, char>> p;
vector<tuple<int, int, char>> query;
bool cmp(tuple<int, int, int, char> &a, tuple<int, int, int, char> &b)
{
int l1 = get<0>(a);
int l2 = get<0>(b);
char v1 = get<3>(a);
char v2 = get<3>(b);
return l1 > l2 || l1 == l2 && v1 < v2;
}
bool cmp2(tuple<int, int, int, int, char> &a, tuple<int, int, int, int, char> &b)
{
int l1 = get<0>(a);
int l2 = get<0>(b);
char v1 = get<4>(a);
char v2 = get<4>(b);
return l1 > l2 || l1 == l2 && v1 < v2;
}
void Cal(int l, int r)
{
if (l >= r)
return;
int mid = (l + r) / 2;
vector<tuple<int, int, int, char>> c;
for (int i = l; i <= mid; i++)
{
auto [l, r, w] = query[i];
if (w == '+')
{
c.push_back(make_tuple(l, r, i, w));
}
}
for (int i = mid + 1; i <= r; i++)
{
auto [l, r, w] = query[i];
if (w == '?')
{
c.push_back(make_tuple(l, r, i, w));
}
}
sort(c.begin(), c.end(), cmp);
vector<int> t;
for (auto [l, r, i, w] : c)
{
if (w == '+')
{
t.push_back(r);
tree.update(r, 1);
}
else
{
int val = tree.query(r, int(d.size()));
res[i] += val;
}
}
for (int i : t)
{
tree.update(i, -1);
}
Cal(l, mid);
Cal(mid + 1, r);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
cin >> s[i] >> t[i];
p.push_back({s[i] + t[i], s[i], t[i], i, '+'});
}
for (int i = 1; i <= q; i++)
{
int a, b, c;
cin >> a >> b >> c;
p.push_back({c, a, b, i, '?'});
}
sort(p.begin(), p.end(), cmp2);
query.push_back({0, 0, '0'});
d.push_back(1);
d.push_back(1e9);
for (auto [w, l, r, id, type] : p)
{
query.push_back({l, r, type});
if (type == '?')
qe[query.size() - 1] = id;
d.push_back(r);
}
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
for (auto &[l, r, w] : query)
{
r = lower_bound(d.begin(), d.end(), r) - d.begin() + 1;
}
tree = Fenwick(int(d.size()));
Cal(1, n + q);
for (int i = 1; i <= n + q; i++)
{
auto [l, r, w] = query[i];
if (w == '?')
{
ans[qe[i]] = res[i];
}
}
for (int i = 1; i <= q; i++)
{
cout << ans[i] << endl;
}
}
Compilation message
examination.cpp: In function 'bool cmp(std::tuple<int, int, int, char>&, std::tuple<int, int, int, char>&)':
examination.cpp:50:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
50 | return l1 > l2 || l1 == l2 && v1 < v2;
| ~~~~~~~~~^~~~~~~~~~
examination.cpp: In function 'bool cmp2(std::tuple<int, int, int, int, char>&, std::tuple<int, int, int, int, char>&)':
examination.cpp:58:32: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
58 | return l1 > l2 || l1 == l2 && v1 < v2;
| ~~~~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
964 KB |
Output is correct |
8 |
Correct |
6 ms |
824 KB |
Output is correct |
9 |
Correct |
6 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
828 KB |
Output is correct |
11 |
Correct |
5 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
828 KB |
Output is correct |
13 |
Correct |
6 ms |
880 KB |
Output is correct |
14 |
Correct |
6 ms |
828 KB |
Output is correct |
15 |
Correct |
6 ms |
1024 KB |
Output is correct |
16 |
Correct |
5 ms |
828 KB |
Output is correct |
17 |
Correct |
4 ms |
968 KB |
Output is correct |
18 |
Correct |
3 ms |
828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
16888 KB |
Output is correct |
2 |
Correct |
253 ms |
16816 KB |
Output is correct |
3 |
Correct |
256 ms |
16812 KB |
Output is correct |
4 |
Correct |
184 ms |
16568 KB |
Output is correct |
5 |
Correct |
221 ms |
17072 KB |
Output is correct |
6 |
Correct |
158 ms |
16396 KB |
Output is correct |
7 |
Correct |
256 ms |
16816 KB |
Output is correct |
8 |
Correct |
258 ms |
16852 KB |
Output is correct |
9 |
Correct |
249 ms |
16816 KB |
Output is correct |
10 |
Correct |
183 ms |
16816 KB |
Output is correct |
11 |
Correct |
163 ms |
16316 KB |
Output is correct |
12 |
Correct |
125 ms |
16564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
16888 KB |
Output is correct |
2 |
Correct |
253 ms |
16816 KB |
Output is correct |
3 |
Correct |
256 ms |
16812 KB |
Output is correct |
4 |
Correct |
184 ms |
16568 KB |
Output is correct |
5 |
Correct |
221 ms |
17072 KB |
Output is correct |
6 |
Correct |
158 ms |
16396 KB |
Output is correct |
7 |
Correct |
256 ms |
16816 KB |
Output is correct |
8 |
Correct |
258 ms |
16852 KB |
Output is correct |
9 |
Correct |
249 ms |
16816 KB |
Output is correct |
10 |
Correct |
183 ms |
16816 KB |
Output is correct |
11 |
Correct |
163 ms |
16316 KB |
Output is correct |
12 |
Correct |
125 ms |
16564 KB |
Output is correct |
13 |
Correct |
266 ms |
15796 KB |
Output is correct |
14 |
Correct |
274 ms |
16300 KB |
Output is correct |
15 |
Correct |
258 ms |
16816 KB |
Output is correct |
16 |
Correct |
204 ms |
15280 KB |
Output is correct |
17 |
Correct |
236 ms |
15772 KB |
Output is correct |
18 |
Correct |
161 ms |
15616 KB |
Output is correct |
19 |
Correct |
277 ms |
15796 KB |
Output is correct |
20 |
Correct |
270 ms |
16560 KB |
Output is correct |
21 |
Correct |
269 ms |
16048 KB |
Output is correct |
22 |
Correct |
179 ms |
16908 KB |
Output is correct |
23 |
Correct |
161 ms |
16564 KB |
Output is correct |
24 |
Correct |
130 ms |
16824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
8 ms |
964 KB |
Output is correct |
8 |
Correct |
6 ms |
824 KB |
Output is correct |
9 |
Correct |
6 ms |
828 KB |
Output is correct |
10 |
Correct |
5 ms |
828 KB |
Output is correct |
11 |
Correct |
5 ms |
980 KB |
Output is correct |
12 |
Correct |
4 ms |
828 KB |
Output is correct |
13 |
Correct |
6 ms |
880 KB |
Output is correct |
14 |
Correct |
6 ms |
828 KB |
Output is correct |
15 |
Correct |
6 ms |
1024 KB |
Output is correct |
16 |
Correct |
5 ms |
828 KB |
Output is correct |
17 |
Correct |
4 ms |
968 KB |
Output is correct |
18 |
Correct |
3 ms |
828 KB |
Output is correct |
19 |
Correct |
252 ms |
16888 KB |
Output is correct |
20 |
Correct |
253 ms |
16816 KB |
Output is correct |
21 |
Correct |
256 ms |
16812 KB |
Output is correct |
22 |
Correct |
184 ms |
16568 KB |
Output is correct |
23 |
Correct |
221 ms |
17072 KB |
Output is correct |
24 |
Correct |
158 ms |
16396 KB |
Output is correct |
25 |
Correct |
256 ms |
16816 KB |
Output is correct |
26 |
Correct |
258 ms |
16852 KB |
Output is correct |
27 |
Correct |
249 ms |
16816 KB |
Output is correct |
28 |
Correct |
183 ms |
16816 KB |
Output is correct |
29 |
Correct |
163 ms |
16316 KB |
Output is correct |
30 |
Correct |
125 ms |
16564 KB |
Output is correct |
31 |
Correct |
266 ms |
15796 KB |
Output is correct |
32 |
Correct |
274 ms |
16300 KB |
Output is correct |
33 |
Correct |
258 ms |
16816 KB |
Output is correct |
34 |
Correct |
204 ms |
15280 KB |
Output is correct |
35 |
Correct |
236 ms |
15772 KB |
Output is correct |
36 |
Correct |
161 ms |
15616 KB |
Output is correct |
37 |
Correct |
277 ms |
15796 KB |
Output is correct |
38 |
Correct |
270 ms |
16560 KB |
Output is correct |
39 |
Correct |
269 ms |
16048 KB |
Output is correct |
40 |
Correct |
179 ms |
16908 KB |
Output is correct |
41 |
Correct |
161 ms |
16564 KB |
Output is correct |
42 |
Correct |
130 ms |
16824 KB |
Output is correct |
43 |
Correct |
290 ms |
16280 KB |
Output is correct |
44 |
Correct |
287 ms |
16308 KB |
Output is correct |
45 |
Correct |
285 ms |
16900 KB |
Output is correct |
46 |
Correct |
205 ms |
15280 KB |
Output is correct |
47 |
Correct |
253 ms |
16052 KB |
Output is correct |
48 |
Correct |
155 ms |
14088 KB |
Output is correct |
49 |
Correct |
285 ms |
16824 KB |
Output is correct |
50 |
Correct |
277 ms |
16316 KB |
Output is correct |
51 |
Correct |
304 ms |
16804 KB |
Output is correct |
52 |
Correct |
222 ms |
16060 KB |
Output is correct |
53 |
Correct |
169 ms |
16560 KB |
Output is correct |