#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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
828 KB |
Output is correct |
9 |
Correct |
5 ms |
780 KB |
Output is correct |
10 |
Correct |
5 ms |
828 KB |
Output is correct |
11 |
Correct |
5 ms |
828 KB |
Output is correct |
12 |
Correct |
4 ms |
824 KB |
Output is correct |
13 |
Correct |
5 ms |
824 KB |
Output is correct |
14 |
Correct |
5 ms |
828 KB |
Output is correct |
15 |
Correct |
6 ms |
828 KB |
Output is correct |
16 |
Correct |
3 ms |
816 KB |
Output is correct |
17 |
Correct |
5 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
16312 KB |
Output is correct |
2 |
Correct |
212 ms |
16288 KB |
Output is correct |
3 |
Correct |
182 ms |
15628 KB |
Output is correct |
4 |
Correct |
185 ms |
16172 KB |
Output is correct |
5 |
Correct |
126 ms |
16420 KB |
Output is correct |
6 |
Correct |
90 ms |
16308 KB |
Output is correct |
7 |
Correct |
173 ms |
16296 KB |
Output is correct |
8 |
Correct |
188 ms |
15544 KB |
Output is correct |
9 |
Correct |
184 ms |
16468 KB |
Output is correct |
10 |
Correct |
103 ms |
16568 KB |
Output is correct |
11 |
Correct |
141 ms |
15780 KB |
Output is correct |
12 |
Correct |
69 ms |
15732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
16312 KB |
Output is correct |
2 |
Correct |
212 ms |
16288 KB |
Output is correct |
3 |
Correct |
182 ms |
15628 KB |
Output is correct |
4 |
Correct |
185 ms |
16172 KB |
Output is correct |
5 |
Correct |
126 ms |
16420 KB |
Output is correct |
6 |
Correct |
90 ms |
16308 KB |
Output is correct |
7 |
Correct |
173 ms |
16296 KB |
Output is correct |
8 |
Correct |
188 ms |
15544 KB |
Output is correct |
9 |
Correct |
184 ms |
16468 KB |
Output is correct |
10 |
Correct |
103 ms |
16568 KB |
Output is correct |
11 |
Correct |
141 ms |
15780 KB |
Output is correct |
12 |
Correct |
69 ms |
15732 KB |
Output is correct |
13 |
Correct |
214 ms |
16052 KB |
Output is correct |
14 |
Correct |
192 ms |
16056 KB |
Output is correct |
15 |
Correct |
199 ms |
15664 KB |
Output is correct |
16 |
Correct |
189 ms |
16304 KB |
Output is correct |
17 |
Correct |
133 ms |
16052 KB |
Output is correct |
18 |
Correct |
88 ms |
16380 KB |
Output is correct |
19 |
Correct |
218 ms |
16564 KB |
Output is correct |
20 |
Correct |
211 ms |
15776 KB |
Output is correct |
21 |
Correct |
222 ms |
16668 KB |
Output is correct |
22 |
Correct |
100 ms |
15804 KB |
Output is correct |
23 |
Correct |
150 ms |
16304 KB |
Output is correct |
24 |
Correct |
70 ms |
15224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
828 KB |
Output is correct |
9 |
Correct |
5 ms |
780 KB |
Output is correct |
10 |
Correct |
5 ms |
828 KB |
Output is correct |
11 |
Correct |
5 ms |
828 KB |
Output is correct |
12 |
Correct |
4 ms |
824 KB |
Output is correct |
13 |
Correct |
5 ms |
824 KB |
Output is correct |
14 |
Correct |
5 ms |
828 KB |
Output is correct |
15 |
Correct |
6 ms |
828 KB |
Output is correct |
16 |
Correct |
3 ms |
816 KB |
Output is correct |
17 |
Correct |
5 ms |
828 KB |
Output is correct |
18 |
Correct |
2 ms |
828 KB |
Output is correct |
19 |
Correct |
194 ms |
16312 KB |
Output is correct |
20 |
Correct |
212 ms |
16288 KB |
Output is correct |
21 |
Correct |
182 ms |
15628 KB |
Output is correct |
22 |
Correct |
185 ms |
16172 KB |
Output is correct |
23 |
Correct |
126 ms |
16420 KB |
Output is correct |
24 |
Correct |
90 ms |
16308 KB |
Output is correct |
25 |
Correct |
173 ms |
16296 KB |
Output is correct |
26 |
Correct |
188 ms |
15544 KB |
Output is correct |
27 |
Correct |
184 ms |
16468 KB |
Output is correct |
28 |
Correct |
103 ms |
16568 KB |
Output is correct |
29 |
Correct |
141 ms |
15780 KB |
Output is correct |
30 |
Correct |
69 ms |
15732 KB |
Output is correct |
31 |
Correct |
214 ms |
16052 KB |
Output is correct |
32 |
Correct |
192 ms |
16056 KB |
Output is correct |
33 |
Correct |
199 ms |
15664 KB |
Output is correct |
34 |
Correct |
189 ms |
16304 KB |
Output is correct |
35 |
Correct |
133 ms |
16052 KB |
Output is correct |
36 |
Correct |
88 ms |
16380 KB |
Output is correct |
37 |
Correct |
218 ms |
16564 KB |
Output is correct |
38 |
Correct |
211 ms |
15776 KB |
Output is correct |
39 |
Correct |
222 ms |
16668 KB |
Output is correct |
40 |
Correct |
100 ms |
15804 KB |
Output is correct |
41 |
Correct |
150 ms |
16304 KB |
Output is correct |
42 |
Correct |
70 ms |
15224 KB |
Output is correct |
43 |
Correct |
233 ms |
17080 KB |
Output is correct |
44 |
Correct |
212 ms |
16420 KB |
Output is correct |
45 |
Correct |
225 ms |
16228 KB |
Output is correct |
46 |
Correct |
199 ms |
16540 KB |
Output is correct |
47 |
Correct |
150 ms |
17080 KB |
Output is correct |
48 |
Correct |
87 ms |
15796 KB |
Output is correct |
49 |
Correct |
224 ms |
15940 KB |
Output is correct |
50 |
Correct |
226 ms |
16308 KB |
Output is correct |
51 |
Correct |
217 ms |
17040 KB |
Output is correct |
52 |
Correct |
137 ms |
17076 KB |
Output is correct |
53 |
Correct |
152 ms |
16056 KB |
Output is correct |