#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l; i>=(r); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
#define fi first
#define se second
#define C make_pair
using dt = pair<ii, int>;
const int N = 2e5+5;
const int INF = 1e9;
const int SQRT = sqrt(N);
int debug = 0;
struct trie {
struct node {
int nxt[2], cnt = 0;
node() { nxt[0] = nxt[1] = -1; cnt = 0; }
};
vector<node> trie;
void built() {
trie.push_back(node());
}
void add(int num) {
for(int i=30, cur=0; i>=0; --i) {
int bit = num>>i&1;
if (trie[cur].nxt[bit] == -1) {
trie[cur].nxt[bit] = trie.size();
trie.push_back(node());
}
cur = trie[cur].nxt[bit];
trie[cur].cnt += 1;
}
}
int query(int val) {
int ans = 0, cur = 0;
for(int i=30; i>=0; --i) {
int bit = val>>i&1;
if (bit == 1) cur = trie[cur].nxt[1];
else {
if (trie[cur].nxt[1] != -1) ans += trie[trie[cur].nxt[1]].cnt;
cur = trie[cur].nxt[0];
}
if (cur == -1) return ans;
}
assert(cur != -1);
return ans + trie[cur].cnt;
}
};
int n, ny, q, ret[N];
int qx[N], qy[N], qz[N];
vector<int> vy;
vector<ii> a(N);
vector<dt> evts;
vector<trie> bit(N);
bool cmp(dt a, dt b) {
if (a.fi.fi != b.fi.fi) return a.fi.fi > b.fi.fi;
return a.se < b.se;
}
int pos(int val) {
int id = upper_bound(all(vy), val) - vy.begin();
return id;
}
void upd(int i, int v) {
for(i=ny-i+1; i<=ny; i+=i&-i) bit[i].add(v);
}
int query(int i, int v) {
int ans = 0;
for(i=ny-i+1; i>0; i-=i&-i) ans += bit[i].query(v);
return ans;
}
void solve() {
cin >> n >> q;
for(int i=1; i<=n; ++i) {
cin >> a[i].fi >> a[i].se;
vy.push_back(a[i].se);
evts.push_back(C(C(a[i].fi, i), 0));
}
for(int i=1; i<=q; ++i) {
int x, y, z;
cin >> x >> y >> z;
qx[i] = x; qy[i] = y; qz[i] = z;
vy.push_back(y);
evts.push_back(C(C(x, i), 1));
}
sort(all(evts), cmp);
sort(all(vy));
vy.resize(unique(all(vy)) - vy.begin());
ny = vy.size();
// for(int x : vy) cout << x << " ";
// cout << "\n";
for(int i=0; i<=ny; ++i) {
bit[i].built();
}
// upd(3, 120);
// upd(6, 140);
// cout << bit[3].query(80);
// cout << query(5, 80) << "\n";
for(auto e : evts) {
// cout << pos(10) << "\n";
// cout << e.fi.fi << " " << e.se << "\n";
if (e.se == 0) {
int i = e.fi.se;
upd(pos(a[i].se), a[i].fi + a[i].se);
// cout << "update pos " << pos(a[i].se) << " value " << a[i].fi + a[i].se << "\n";
} else {
int i = e.fi.se;
int val = qx[i] + qy[i] + max(0, qz[i] - (qx[i] + qy[i]));
// assert(pos(qy[i]) <= ny);
ret[i] = query(pos(qy[i]), val);
// cout << "query pos " << pos(qy[i]) << " greater than " << val << "\n";
}
}
for(int i=1; i<=q; ++i) {
cout << ret[i] << "\n";
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
solve();
}
Compilation message
examination.cpp: In function 'int main()':
examination.cpp:148:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
examination.cpp:149:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6564 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
15 ms |
14348 KB |
Output is correct |
8 |
Correct |
16 ms |
14252 KB |
Output is correct |
9 |
Correct |
19 ms |
14288 KB |
Output is correct |
10 |
Correct |
9 ms |
9028 KB |
Output is correct |
11 |
Correct |
15 ms |
13616 KB |
Output is correct |
12 |
Correct |
6 ms |
6876 KB |
Output is correct |
13 |
Correct |
16 ms |
14280 KB |
Output is correct |
14 |
Correct |
15 ms |
14336 KB |
Output is correct |
15 |
Correct |
16 ms |
14316 KB |
Output is correct |
16 |
Correct |
14 ms |
13564 KB |
Output is correct |
17 |
Correct |
6 ms |
7824 KB |
Output is correct |
18 |
Correct |
6 ms |
6868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
659 ms |
157636 KB |
Output is correct |
2 |
Correct |
680 ms |
159916 KB |
Output is correct |
3 |
Correct |
657 ms |
159960 KB |
Output is correct |
4 |
Correct |
125 ms |
27596 KB |
Output is correct |
5 |
Correct |
553 ms |
78548 KB |
Output is correct |
6 |
Correct |
89 ms |
13008 KB |
Output is correct |
7 |
Correct |
681 ms |
159932 KB |
Output is correct |
8 |
Correct |
657 ms |
155564 KB |
Output is correct |
9 |
Correct |
645 ms |
155044 KB |
Output is correct |
10 |
Correct |
452 ms |
69940 KB |
Output is correct |
11 |
Correct |
84 ms |
17584 KB |
Output is correct |
12 |
Correct |
55 ms |
12612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
659 ms |
157636 KB |
Output is correct |
2 |
Correct |
680 ms |
159916 KB |
Output is correct |
3 |
Correct |
657 ms |
159960 KB |
Output is correct |
4 |
Correct |
125 ms |
27596 KB |
Output is correct |
5 |
Correct |
553 ms |
78548 KB |
Output is correct |
6 |
Correct |
89 ms |
13008 KB |
Output is correct |
7 |
Correct |
681 ms |
159932 KB |
Output is correct |
8 |
Correct |
657 ms |
155564 KB |
Output is correct |
9 |
Correct |
645 ms |
155044 KB |
Output is correct |
10 |
Correct |
452 ms |
69940 KB |
Output is correct |
11 |
Correct |
84 ms |
17584 KB |
Output is correct |
12 |
Correct |
55 ms |
12612 KB |
Output is correct |
13 |
Correct |
747 ms |
160876 KB |
Output is correct |
14 |
Correct |
722 ms |
160604 KB |
Output is correct |
15 |
Correct |
684 ms |
160040 KB |
Output is correct |
16 |
Correct |
138 ms |
26788 KB |
Output is correct |
17 |
Correct |
548 ms |
78520 KB |
Output is correct |
18 |
Correct |
91 ms |
13032 KB |
Output is correct |
19 |
Correct |
730 ms |
160668 KB |
Output is correct |
20 |
Correct |
721 ms |
160176 KB |
Output is correct |
21 |
Correct |
758 ms |
159120 KB |
Output is correct |
22 |
Correct |
429 ms |
70092 KB |
Output is correct |
23 |
Correct |
84 ms |
17640 KB |
Output is correct |
24 |
Correct |
57 ms |
12704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6564 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6604 KB |
Output is correct |
4 |
Correct |
3 ms |
6604 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
15 ms |
14348 KB |
Output is correct |
8 |
Correct |
16 ms |
14252 KB |
Output is correct |
9 |
Correct |
19 ms |
14288 KB |
Output is correct |
10 |
Correct |
9 ms |
9028 KB |
Output is correct |
11 |
Correct |
15 ms |
13616 KB |
Output is correct |
12 |
Correct |
6 ms |
6876 KB |
Output is correct |
13 |
Correct |
16 ms |
14280 KB |
Output is correct |
14 |
Correct |
15 ms |
14336 KB |
Output is correct |
15 |
Correct |
16 ms |
14316 KB |
Output is correct |
16 |
Correct |
14 ms |
13564 KB |
Output is correct |
17 |
Correct |
6 ms |
7824 KB |
Output is correct |
18 |
Correct |
6 ms |
6868 KB |
Output is correct |
19 |
Correct |
659 ms |
157636 KB |
Output is correct |
20 |
Correct |
680 ms |
159916 KB |
Output is correct |
21 |
Correct |
657 ms |
159960 KB |
Output is correct |
22 |
Correct |
125 ms |
27596 KB |
Output is correct |
23 |
Correct |
553 ms |
78548 KB |
Output is correct |
24 |
Correct |
89 ms |
13008 KB |
Output is correct |
25 |
Correct |
681 ms |
159932 KB |
Output is correct |
26 |
Correct |
657 ms |
155564 KB |
Output is correct |
27 |
Correct |
645 ms |
155044 KB |
Output is correct |
28 |
Correct |
452 ms |
69940 KB |
Output is correct |
29 |
Correct |
84 ms |
17584 KB |
Output is correct |
30 |
Correct |
55 ms |
12612 KB |
Output is correct |
31 |
Correct |
747 ms |
160876 KB |
Output is correct |
32 |
Correct |
722 ms |
160604 KB |
Output is correct |
33 |
Correct |
684 ms |
160040 KB |
Output is correct |
34 |
Correct |
138 ms |
26788 KB |
Output is correct |
35 |
Correct |
548 ms |
78520 KB |
Output is correct |
36 |
Correct |
91 ms |
13032 KB |
Output is correct |
37 |
Correct |
730 ms |
160668 KB |
Output is correct |
38 |
Correct |
721 ms |
160176 KB |
Output is correct |
39 |
Correct |
758 ms |
159120 KB |
Output is correct |
40 |
Correct |
429 ms |
70092 KB |
Output is correct |
41 |
Correct |
84 ms |
17640 KB |
Output is correct |
42 |
Correct |
57 ms |
12704 KB |
Output is correct |
43 |
Correct |
962 ms |
329880 KB |
Output is correct |
44 |
Correct |
941 ms |
328728 KB |
Output is correct |
45 |
Correct |
908 ms |
327408 KB |
Output is correct |
46 |
Correct |
193 ms |
80780 KB |
Output is correct |
47 |
Correct |
855 ms |
238832 KB |
Output is correct |
48 |
Correct |
97 ms |
12852 KB |
Output is correct |
49 |
Correct |
995 ms |
329392 KB |
Output is correct |
50 |
Correct |
927 ms |
325600 KB |
Output is correct |
51 |
Correct |
994 ms |
325292 KB |
Output is correct |
52 |
Correct |
792 ms |
238884 KB |
Output is correct |
53 |
Correct |
105 ms |
38548 KB |
Output is correct |