#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
// #define double long double
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define setmin(x, y) x = min((x), (y))
#define setmax(x, y) x = max((x), (y))
#define sqr(x) ((x) * (x))
#define fi first
#define se second
#define all(x) x.begin(), x.end()
// mt19937 hdp(chrono::high_resolution_clock::now().time_since_epoch().count());
// int rand(int l, int r){return l + ((hdp() % (r - l + 1)) + r - l + 1) % (r - l + 1);}
const int NM = 2e5 + 5;
const int mod = 1e9 + 7;
struct BIT
{
int b[NM], n = 2e5;
void upd(int i, int v)
{
for (; i <= n; i += i & -i)
b[i] += v;
}
int get(int i)
{
int res = 0;
for (; i; i -= i & -i)
res += b[i];
return res;
}
}bit;
struct o{
int x, y, z, i, w;
o(int x, int y, int z, int i, int w) : x(x), y(y), z(z), i(i), w(w){}
};
vector<o> v;
vector<int> ans;
void cdq(int l, int r)
{
if (l == r)
return;
int m = l + r >> 1;
cdq(l, m);
cdq(m + 1, r);
int a = l, b = m + 1, sum = 0;
vector<pair<int, int>> record;
vector<o> tmp;
while (a <= m && b <= r)
{
if (v[a].y > v[b].y)
bit.upd(v[a].z, v[a].w), sum += v[a].w, record.push_back({v[a].z, v[a].w}), tmp.push_back(v[a++]);
else
ans[v[b].i] += sum - bit.get(v[b].z), tmp.push_back(v[b++]);
}
while (a <= m)
tmp.push_back(v[a++]);
while (b <= r)
ans[v[b].i] += sum - bit.get(v[b].z), tmp.push_back(v[b++]);
for (int i = l; i <= r; i++)
v[i] = tmp[i - l];
for (auto i : record)
bit.upd(i.fi, -i.se);
vector<pair<int, int>> ().swap(record);
vector<o> ().swap(tmp);
}
vector<int> nen;
int mp(int x)
{
return lower_bound(nen.begin(), nen.end(), x) - nen.begin() + 1;
}
void solve()
{
int n = v.size();
sort(v.begin(), v.end(),[&](const o& a, const o& b){return a.x != b.x ? a.x > b.x : (a.y != b.y ? a.y < b.y : a.z < b.z);});
for (auto t : v)
nen.push_back(t.z);
sort(nen.begin(), nen.end());
nen.erase(unique(nen.begin(), nen.end()), nen.end());
for (auto& t : v)
t.z = mp(t.z);
ans.resize(n);
cdq(0, n - 1);
}
signed main()
{
fastIO
if (fopen("in.txt", "r"))
{
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
}
int n, q;
cin >> n >> q;
for (int i = 1; i <= n; i++)
{
int s, t;
cin >> s >> t;
v.emplace_back(s, t, s + t, v.size(), 1);
}
for (int i = 1; i <= q; i++)
{
int x, y, z;
cin >> x >> y >> z;
v.emplace_back(x - 1, y - 1, z - 1, v.size(), 0);
}
solve();
for (int i = n; i < n + q; i++)
cout << ans[i] << endl;
}
Compilation message
examination.cpp: In function 'void cdq(int, int)':
examination.cpp:53:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
53 | int m = l + r >> 1;
| ~~^~~
examination.cpp: In function 'int main()':
examination.cpp:104:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
104 | freopen("in.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
examination.cpp:105:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | freopen("out.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
6 ms |
1060 KB |
Output is correct |
8 |
Correct |
6 ms |
1060 KB |
Output is correct |
9 |
Correct |
6 ms |
1216 KB |
Output is correct |
10 |
Correct |
5 ms |
1056 KB |
Output is correct |
11 |
Correct |
4 ms |
1056 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
5 ms |
1084 KB |
Output is correct |
14 |
Correct |
5 ms |
1060 KB |
Output is correct |
15 |
Correct |
5 ms |
944 KB |
Output is correct |
16 |
Correct |
4 ms |
912 KB |
Output is correct |
17 |
Correct |
5 ms |
1048 KB |
Output is correct |
18 |
Correct |
3 ms |
828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
19132 KB |
Output is correct |
2 |
Correct |
200 ms |
18112 KB |
Output is correct |
3 |
Correct |
193 ms |
18344 KB |
Output is correct |
4 |
Correct |
180 ms |
17592 KB |
Output is correct |
5 |
Correct |
123 ms |
18348 KB |
Output is correct |
6 |
Correct |
81 ms |
16824 KB |
Output is correct |
7 |
Correct |
197 ms |
17856 KB |
Output is correct |
8 |
Correct |
188 ms |
18104 KB |
Output is correct |
9 |
Correct |
181 ms |
19028 KB |
Output is correct |
10 |
Correct |
101 ms |
17844 KB |
Output is correct |
11 |
Correct |
148 ms |
17332 KB |
Output is correct |
12 |
Correct |
74 ms |
16712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
19132 KB |
Output is correct |
2 |
Correct |
200 ms |
18112 KB |
Output is correct |
3 |
Correct |
193 ms |
18344 KB |
Output is correct |
4 |
Correct |
180 ms |
17592 KB |
Output is correct |
5 |
Correct |
123 ms |
18348 KB |
Output is correct |
6 |
Correct |
81 ms |
16824 KB |
Output is correct |
7 |
Correct |
197 ms |
17856 KB |
Output is correct |
8 |
Correct |
188 ms |
18104 KB |
Output is correct |
9 |
Correct |
181 ms |
19028 KB |
Output is correct |
10 |
Correct |
101 ms |
17844 KB |
Output is correct |
11 |
Correct |
148 ms |
17332 KB |
Output is correct |
12 |
Correct |
74 ms |
16712 KB |
Output is correct |
13 |
Correct |
213 ms |
18716 KB |
Output is correct |
14 |
Correct |
213 ms |
18668 KB |
Output is correct |
15 |
Correct |
199 ms |
19088 KB |
Output is correct |
16 |
Correct |
189 ms |
18100 KB |
Output is correct |
17 |
Correct |
142 ms |
18100 KB |
Output is correct |
18 |
Correct |
91 ms |
16312 KB |
Output is correct |
19 |
Correct |
218 ms |
18872 KB |
Output is correct |
20 |
Correct |
208 ms |
18624 KB |
Output is correct |
21 |
Correct |
211 ms |
18872 KB |
Output is correct |
22 |
Correct |
97 ms |
18000 KB |
Output is correct |
23 |
Correct |
153 ms |
17248 KB |
Output is correct |
24 |
Correct |
71 ms |
17400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 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 |
6 ms |
1060 KB |
Output is correct |
8 |
Correct |
6 ms |
1060 KB |
Output is correct |
9 |
Correct |
6 ms |
1216 KB |
Output is correct |
10 |
Correct |
5 ms |
1056 KB |
Output is correct |
11 |
Correct |
4 ms |
1056 KB |
Output is correct |
12 |
Correct |
3 ms |
852 KB |
Output is correct |
13 |
Correct |
5 ms |
1084 KB |
Output is correct |
14 |
Correct |
5 ms |
1060 KB |
Output is correct |
15 |
Correct |
5 ms |
944 KB |
Output is correct |
16 |
Correct |
4 ms |
912 KB |
Output is correct |
17 |
Correct |
5 ms |
1048 KB |
Output is correct |
18 |
Correct |
3 ms |
828 KB |
Output is correct |
19 |
Correct |
205 ms |
19132 KB |
Output is correct |
20 |
Correct |
200 ms |
18112 KB |
Output is correct |
21 |
Correct |
193 ms |
18344 KB |
Output is correct |
22 |
Correct |
180 ms |
17592 KB |
Output is correct |
23 |
Correct |
123 ms |
18348 KB |
Output is correct |
24 |
Correct |
81 ms |
16824 KB |
Output is correct |
25 |
Correct |
197 ms |
17856 KB |
Output is correct |
26 |
Correct |
188 ms |
18104 KB |
Output is correct |
27 |
Correct |
181 ms |
19028 KB |
Output is correct |
28 |
Correct |
101 ms |
17844 KB |
Output is correct |
29 |
Correct |
148 ms |
17332 KB |
Output is correct |
30 |
Correct |
74 ms |
16712 KB |
Output is correct |
31 |
Correct |
213 ms |
18716 KB |
Output is correct |
32 |
Correct |
213 ms |
18668 KB |
Output is correct |
33 |
Correct |
199 ms |
19088 KB |
Output is correct |
34 |
Correct |
189 ms |
18100 KB |
Output is correct |
35 |
Correct |
142 ms |
18100 KB |
Output is correct |
36 |
Correct |
91 ms |
16312 KB |
Output is correct |
37 |
Correct |
218 ms |
18872 KB |
Output is correct |
38 |
Correct |
208 ms |
18624 KB |
Output is correct |
39 |
Correct |
211 ms |
18872 KB |
Output is correct |
40 |
Correct |
97 ms |
18000 KB |
Output is correct |
41 |
Correct |
153 ms |
17248 KB |
Output is correct |
42 |
Correct |
71 ms |
17400 KB |
Output is correct |
43 |
Correct |
229 ms |
21172 KB |
Output is correct |
44 |
Correct |
232 ms |
21576 KB |
Output is correct |
45 |
Correct |
227 ms |
21012 KB |
Output is correct |
46 |
Correct |
194 ms |
20144 KB |
Output is correct |
47 |
Correct |
148 ms |
19380 KB |
Output is correct |
48 |
Correct |
98 ms |
16708 KB |
Output is correct |
49 |
Correct |
223 ms |
21432 KB |
Output is correct |
50 |
Correct |
223 ms |
21176 KB |
Output is correct |
51 |
Correct |
221 ms |
21444 KB |
Output is correct |
52 |
Correct |
137 ms |
19856 KB |
Output is correct |
53 |
Correct |
148 ms |
18356 KB |
Output is correct |