/*
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
*/
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ll long long
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vii vector<pii>
using namespace std;
const int mxN = 250000 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
struct node {
ll sum_plus, sum_minus, idx;
pll pref_min;
node() {
sum_plus = sum_minus = idx = 0;
pref_min = {0, mod};
}
node operator + (const node &o) {
node res;
res.sum_plus = sum_plus + o.sum_plus;
res.sum_minus = sum_minus + o.sum_minus;
res.pref_min = min(pref_min, {sum_plus - sum_minus + o.pref_min.ff, o.pref_min.ss});
return res;
}
} segtree[4 * mxN];
int n, m, q, ans[mxN];
vector<vector<ll>> query;
vi query_time[mxN], all_query;
vii actual_query[mxN];
void update(int l, int r, int idx, int pos, int val) {
if(l == r) {
if(val > 0) {
if(query[pos][0] == 1) segtree[idx].sum_plus = query[pos][2];
else if(query[pos][0] == 2) segtree[idx].sum_minus = query[pos][1];
segtree[idx].pref_min = {min(segtree[idx].sum_plus, -segtree[idx].sum_minus), pos};
segtree[idx].idx = pos;
}
else {
segtree[idx] = node();
}
return;
}
int mid = (l + r) >> 1;
if(pos <= mid) update(l, mid, idx << 1, pos, val);
else update(mid + 1, r, idx << 1 | 1, pos, val);
segtree[idx] = segtree[idx << 1] + segtree[idx << 1 | 1];
}
node get(int l, int r, int idx, int u, int v) {
if(v < u) return node();
if(u <= l && r <= v) {
return segtree[idx];
}
int mid = (l + r) >> 1;
if(v <= mid) return get(l, mid, idx << 1, u, v);
else if(mid + 1 <= u) return get(mid + 1, r, idx << 1 | 1, u, v);
else return get(l, mid, idx << 1, u, v) + get(mid + 1, r, idx << 1 | 1, u, v);
}
int huutuannguvailon(int cur_l, int cur_r, ll k) {
int l = 1, r = q, idx = 1;
while(l < r) {
int mid = (l + r) >> 1;
if(k > segtree[idx << 1].sum_plus) {
k -= segtree[idx << 1].sum_plus;
idx = idx << 1 | 1, l = mid + 1;
}
else {
idx <<= 1, r = mid;
}
}
// assert(cur_l <= cur_r);
// assert(cur_l <= l && l <= cur_r);
// assert(l == r && query[l][0] == 1);
return query[l][1];
}
void solve() {
cin >> n >> m >> q;
query.pb({});
for(int i = 1; i <= q; ++i) {
int type; cin >> type;
vector<ll> tmp = {type};
if(type == 1) {
int l, r; cin >> l >> r;
query_time[l].emplace_back(i);
query_time[r + 1].emplace_back(-i);
int k, c; cin >> c >> k;
tmp.emplace_back(c);
tmp.emplace_back(k);
}
else if(type == 2) {
int l, r; cin >> l >> r;
query_time[l].emplace_back(i);
query_time[r + 1].emplace_back(-i);
int k; cin >> k; tmp.emplace_back(k);
}
else {
all_query.emplace_back(i);
int cur, k; cin >> cur >> k;
actual_query[cur].emplace_back(i, k);
}
query.pb(tmp);
}
for(int i = 1; i <= n; ++i) {
for(int &val : query_time[i]) {
if(val > 0) update(1, q, 1, val, 1);
else update(1, q, 1, -val, -1);
}
// cout << segtree[1].sum_plus << " ";
// cout << segtree[1].sum_minus << " ";
// cout << segtree[1].pref_min.ff << " ";
// cout << segtree[1].pref_min.ss << "\n";
int cope = segtree[1].pref_min.ss;
if(cope == mod) {
for(auto &[idx, k] : actual_query[i])
ans[idx] = 0;
continue;
}
for(auto &[idx, k] : actual_query[i]) {
auto cur = get(1, q, 1, 1, idx);
cope = cur.pref_min.ss;
if(cope == mod || cope == idx) {
ans[idx] = 0;
continue;
}
auto cur1 = get(1, q, 1, cope, idx);
if(query[cope][0] == 2)
cur1.sum_minus -= query[cope][1];
// assert(cur1.sum_plus >= 0 && cur1.sum_minus >= 0 && cur1.sum_plus - cur1.sum_minus >= 0);
// cout << "time = " << idx << "\n";
// cout << cur1.sum_plus << " " << cur1.sum_minus << endl;
ll actual_k = k + cur1.sum_minus;
// cout << actual_k << " " << endl;
if(actual_k > cur1.sum_plus) {
ans[idx] = 0;
continue;
}
ans[idx] = huutuannguvailon(cope, idx, actual_k + cur.sum_plus - cur1.sum_plus);
}
// cout << endl;
}
for(int &idx : all_query) cout << ans[idx] << endl;
}
signed main() {
#ifndef CDuongg
if(fopen(taskname".inp", "r"))
assert(freopen(taskname".inp", "r", stdin)), assert(freopen(taskname".out", "w", stdout));
#else
freopen("bai3.inp", "r", stdin);
freopen("bai3.out", "w", stdout);
auto start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1; //cin >> t;
while(t--) solve();
#ifdef CDuongg
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
51296 KB |
Output is correct |
2 |
Correct |
23 ms |
51412 KB |
Output is correct |
3 |
Correct |
22 ms |
51292 KB |
Output is correct |
4 |
Correct |
26 ms |
51340 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
22 ms |
51356 KB |
Output is correct |
7 |
Correct |
24 ms |
51420 KB |
Output is correct |
8 |
Correct |
23 ms |
51412 KB |
Output is correct |
9 |
Correct |
24 ms |
51424 KB |
Output is correct |
10 |
Correct |
24 ms |
51420 KB |
Output is correct |
11 |
Correct |
23 ms |
51332 KB |
Output is correct |
12 |
Correct |
23 ms |
51308 KB |
Output is correct |
13 |
Correct |
24 ms |
51356 KB |
Output is correct |
14 |
Correct |
24 ms |
51368 KB |
Output is correct |
15 |
Correct |
23 ms |
51328 KB |
Output is correct |
16 |
Correct |
23 ms |
51328 KB |
Output is correct |
17 |
Correct |
23 ms |
51268 KB |
Output is correct |
18 |
Correct |
24 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51272 KB |
Output is correct |
20 |
Correct |
23 ms |
51372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
51296 KB |
Output is correct |
2 |
Correct |
23 ms |
51412 KB |
Output is correct |
3 |
Correct |
22 ms |
51292 KB |
Output is correct |
4 |
Correct |
26 ms |
51340 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
22 ms |
51356 KB |
Output is correct |
7 |
Correct |
24 ms |
51420 KB |
Output is correct |
8 |
Correct |
23 ms |
51412 KB |
Output is correct |
9 |
Correct |
24 ms |
51424 KB |
Output is correct |
10 |
Correct |
24 ms |
51420 KB |
Output is correct |
11 |
Correct |
23 ms |
51332 KB |
Output is correct |
12 |
Correct |
23 ms |
51308 KB |
Output is correct |
13 |
Correct |
24 ms |
51356 KB |
Output is correct |
14 |
Correct |
24 ms |
51368 KB |
Output is correct |
15 |
Correct |
23 ms |
51328 KB |
Output is correct |
16 |
Correct |
23 ms |
51328 KB |
Output is correct |
17 |
Correct |
23 ms |
51268 KB |
Output is correct |
18 |
Correct |
24 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51272 KB |
Output is correct |
20 |
Correct |
23 ms |
51372 KB |
Output is correct |
21 |
Incorrect |
24 ms |
51376 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
58168 KB |
Output is correct |
2 |
Correct |
99 ms |
58100 KB |
Output is correct |
3 |
Correct |
113 ms |
58364 KB |
Output is correct |
4 |
Correct |
101 ms |
58128 KB |
Output is correct |
5 |
Correct |
98 ms |
58124 KB |
Output is correct |
6 |
Correct |
97 ms |
58096 KB |
Output is correct |
7 |
Correct |
68 ms |
55920 KB |
Output is correct |
8 |
Correct |
74 ms |
56080 KB |
Output is correct |
9 |
Correct |
95 ms |
58200 KB |
Output is correct |
10 |
Correct |
94 ms |
58044 KB |
Output is correct |
11 |
Correct |
96 ms |
58056 KB |
Output is correct |
12 |
Correct |
94 ms |
58144 KB |
Output is correct |
13 |
Correct |
105 ms |
57160 KB |
Output is correct |
14 |
Correct |
101 ms |
58040 KB |
Output is correct |
15 |
Correct |
89 ms |
57652 KB |
Output is correct |
16 |
Correct |
94 ms |
58036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
337 ms |
67848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
51296 KB |
Output is correct |
2 |
Correct |
23 ms |
51412 KB |
Output is correct |
3 |
Correct |
22 ms |
51292 KB |
Output is correct |
4 |
Correct |
26 ms |
51340 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
22 ms |
51356 KB |
Output is correct |
7 |
Correct |
24 ms |
51420 KB |
Output is correct |
8 |
Correct |
23 ms |
51412 KB |
Output is correct |
9 |
Correct |
24 ms |
51424 KB |
Output is correct |
10 |
Correct |
24 ms |
51420 KB |
Output is correct |
11 |
Correct |
23 ms |
51332 KB |
Output is correct |
12 |
Correct |
23 ms |
51308 KB |
Output is correct |
13 |
Correct |
24 ms |
51356 KB |
Output is correct |
14 |
Correct |
24 ms |
51368 KB |
Output is correct |
15 |
Correct |
23 ms |
51328 KB |
Output is correct |
16 |
Correct |
23 ms |
51328 KB |
Output is correct |
17 |
Correct |
23 ms |
51268 KB |
Output is correct |
18 |
Correct |
24 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51272 KB |
Output is correct |
20 |
Correct |
23 ms |
51372 KB |
Output is correct |
21 |
Correct |
97 ms |
58168 KB |
Output is correct |
22 |
Correct |
99 ms |
58100 KB |
Output is correct |
23 |
Correct |
113 ms |
58364 KB |
Output is correct |
24 |
Correct |
101 ms |
58128 KB |
Output is correct |
25 |
Correct |
98 ms |
58124 KB |
Output is correct |
26 |
Correct |
97 ms |
58096 KB |
Output is correct |
27 |
Correct |
68 ms |
55920 KB |
Output is correct |
28 |
Correct |
74 ms |
56080 KB |
Output is correct |
29 |
Correct |
95 ms |
58200 KB |
Output is correct |
30 |
Correct |
94 ms |
58044 KB |
Output is correct |
31 |
Correct |
96 ms |
58056 KB |
Output is correct |
32 |
Correct |
94 ms |
58144 KB |
Output is correct |
33 |
Correct |
105 ms |
57160 KB |
Output is correct |
34 |
Correct |
101 ms |
58040 KB |
Output is correct |
35 |
Correct |
89 ms |
57652 KB |
Output is correct |
36 |
Correct |
94 ms |
58036 KB |
Output is correct |
37 |
Correct |
104 ms |
57436 KB |
Output is correct |
38 |
Correct |
82 ms |
56772 KB |
Output is correct |
39 |
Correct |
58 ms |
55244 KB |
Output is correct |
40 |
Correct |
67 ms |
56060 KB |
Output is correct |
41 |
Correct |
101 ms |
58104 KB |
Output is correct |
42 |
Correct |
105 ms |
58060 KB |
Output is correct |
43 |
Correct |
103 ms |
58108 KB |
Output is correct |
44 |
Correct |
103 ms |
58072 KB |
Output is correct |
45 |
Correct |
100 ms |
58052 KB |
Output is correct |
46 |
Correct |
103 ms |
58024 KB |
Output is correct |
47 |
Correct |
82 ms |
56836 KB |
Output is correct |
48 |
Correct |
99 ms |
57172 KB |
Output is correct |
49 |
Correct |
83 ms |
55936 KB |
Output is correct |
50 |
Correct |
88 ms |
56996 KB |
Output is correct |
51 |
Correct |
105 ms |
57832 KB |
Output is correct |
52 |
Correct |
101 ms |
57760 KB |
Output is correct |
53 |
Correct |
77 ms |
56432 KB |
Output is correct |
54 |
Correct |
91 ms |
57836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
104 ms |
55676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
51296 KB |
Output is correct |
2 |
Correct |
23 ms |
51412 KB |
Output is correct |
3 |
Correct |
22 ms |
51292 KB |
Output is correct |
4 |
Correct |
26 ms |
51340 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
22 ms |
51356 KB |
Output is correct |
7 |
Correct |
24 ms |
51420 KB |
Output is correct |
8 |
Correct |
23 ms |
51412 KB |
Output is correct |
9 |
Correct |
24 ms |
51424 KB |
Output is correct |
10 |
Correct |
24 ms |
51420 KB |
Output is correct |
11 |
Correct |
23 ms |
51332 KB |
Output is correct |
12 |
Correct |
23 ms |
51308 KB |
Output is correct |
13 |
Correct |
24 ms |
51356 KB |
Output is correct |
14 |
Correct |
24 ms |
51368 KB |
Output is correct |
15 |
Correct |
23 ms |
51328 KB |
Output is correct |
16 |
Correct |
23 ms |
51328 KB |
Output is correct |
17 |
Correct |
23 ms |
51268 KB |
Output is correct |
18 |
Correct |
24 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51272 KB |
Output is correct |
20 |
Correct |
23 ms |
51372 KB |
Output is correct |
21 |
Incorrect |
24 ms |
51376 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
51296 KB |
Output is correct |
2 |
Correct |
23 ms |
51412 KB |
Output is correct |
3 |
Correct |
22 ms |
51292 KB |
Output is correct |
4 |
Correct |
26 ms |
51340 KB |
Output is correct |
5 |
Correct |
23 ms |
51412 KB |
Output is correct |
6 |
Correct |
22 ms |
51356 KB |
Output is correct |
7 |
Correct |
24 ms |
51420 KB |
Output is correct |
8 |
Correct |
23 ms |
51412 KB |
Output is correct |
9 |
Correct |
24 ms |
51424 KB |
Output is correct |
10 |
Correct |
24 ms |
51420 KB |
Output is correct |
11 |
Correct |
23 ms |
51332 KB |
Output is correct |
12 |
Correct |
23 ms |
51308 KB |
Output is correct |
13 |
Correct |
24 ms |
51356 KB |
Output is correct |
14 |
Correct |
24 ms |
51368 KB |
Output is correct |
15 |
Correct |
23 ms |
51328 KB |
Output is correct |
16 |
Correct |
23 ms |
51328 KB |
Output is correct |
17 |
Correct |
23 ms |
51268 KB |
Output is correct |
18 |
Correct |
24 ms |
51412 KB |
Output is correct |
19 |
Correct |
23 ms |
51272 KB |
Output is correct |
20 |
Correct |
23 ms |
51372 KB |
Output is correct |
21 |
Incorrect |
24 ms |
51376 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |