This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 25e4 + 7;
struct Query{
int t, l, r, c;
ll k;
};
struct Node{
ll val;
Node *Left, *Right;
Node(ll _val){
val = _val;
Left = Right = nullptr;
}
};
struct BIT{
ll d[N];
void update(int x, int val){
for(; x < N; x += x & -x)
d[x] += val;
}
ll get(int x){
ll res = 0;
for(; x > 0; x -= x & -x)
res += d[x];
return res;
}
void updateRange(int l, int r, int val){
update(l, val);
update(r + 1, -val);
}
} bit;
struct SegTree{
int n;
ll d[N * 4], lazyVal[N * 4], lazyMax[N * 4];
void init(int _n){
n = _n;
}
void doLazy(int id, int l, int r){
d[id] += lazyVal[id];
d[id] = max(d[id], lazyMax[id]);
if (l < r){
lazyVal[id << 1] += lazyVal[id];
lazyMax[id << 1] += lazyVal[id];
lazyMax[id << 1] = max(lazyMax[id << 1], lazyMax[id]);
lazyVal[id << 1 | 1] += lazyVal[id];
lazyMax[id << 1 | 1] += lazyVal[id];
lazyMax[id << 1 | 1] = max(lazyMax[id << 1 | 1], lazyMax[id]);
}
lazyVal[id] = lazyMax[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val){
doLazy(id, l, r);
if (v < l || r < u)
return;
if (u <= l && r <= v){
lazyVal[id] += val;
doLazy(id, l, r);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
}
ll get(int id, int l, int r, int pos){
doLazy(id, l, r);
if (pos < l || r < pos)
return 0;
if (l == r)
return d[id];
int mid = (l + r) >> 1;
return max(get(id << 1, l, mid, pos), get(id << 1 | 1, mid + 1, r, pos));
}
void update(int l, int r, int k){
update(1, 1, n, l, r, k);
}
ll get(int pos){
return get(1, 1, n, pos);
}
} st;
struct SegTreeMin{
int n;
ll d[N * 4], lazy[N * 4];
void init(int _n){
n = _n;
}
void doLazy(int id, int l, int r){
d[id] += lazy[id];
if (l < r){
lazy[id << 1] += lazy[id];
lazy[id << 1 | 1] += lazy[id];
}
lazy[id] = 0;
}
void update(int id, int l, int r, int u, int v, ll val){
doLazy(id, l, r);
if (v < l || r < u)
return;
if (u <= l && r <= v){
lazy[id] += val;
doLazy(id, l, r);
return;
}
int mid = (l + r) >> 1;
update(id << 1, l, mid, u, v, val);
update(id << 1 | 1, mid + 1, r, u, v, val);
d[id] = min(d[id << 1], d[id << 1 | 1]);
}
int findPos(int id, int l, int r){
doLazy(id, l, r);
if (d[id] > 0)
return -1;
if (l == r)
return l;
int mid = (l + r) >> 1;
int res = findPos(id << 1, l, mid);
if (res == -1)
res = findPos(id << 1 | 1, mid + 1, r);
return res;
}
void update(int l, int r, ll val){
update(1, 1, n, l, r, val);
}
int findPos(){
return findPos(1, 1, n);
}
} stMin;
int n, m, q, answer[N];
Query queries[N];
vector<pair<ll, int>> data[N];
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> q;
for(int i = 1; i <= q; i++){
cin >> queries[i].t;
if (queries[i].t == 1)
cin >> queries[i].l >> queries[i].r >> queries[i].c >> queries[i].k;
else if (queries[i].t == 2)
cin >> queries[i].l >> queries[i].r >> queries[i].k;
else
cin >> queries[i].l >> queries[i].k;
}
st.init(n);
for(int i = 1; i <= q; i++)
if (queries[i].t == 1) {
st.update(queries[i].l, queries[i].r, queries[i].k);
bit.updateRange(queries[i].l, queries[i].r, queries[i].k);
} else if (queries[i].t == 2) {
st.update(queries[i].l, queries[i].r, -queries[i].k);
} else {
if (st.get(queries[i].l) < queries[i].k){
answer[i] = 0;
} else {
ll x = bit.get(queries[i].l) - (st.get(queries[i].l) - queries[i].k);
data[queries[i].l].push_back({x, i});
}
}
stMin.init(n);
for(int i = 1; i <= n; i++){
sort(data[i].begin(), data[i].end(), greater<pair<ll, int>>());
if (data[i].empty())
stMin.update(i, i, 1e18);
else
stMin.update(i, i, data[i].back().first);
}
for(int i = 1; i <= q; i++)
if (queries[i].t == 1){
stMin.update(queries[i].l, queries[i].r, -queries[i].k);
while (stMin.findPos() != -1){
int pos = stMin.findPos();
answer[data[pos].back().second] = queries[i].c;
ll pre = data[pos].back().first;
data[pos].pop_back();
if (data[pos].empty())
stMin.update(pos, pos, 1e18);
else
stMin.update(pos, pos, data[pos].back().first - pre);
}
}
for(int i = 1; i <= q; i++)
if (queries[i].t == 3)
cout << answer[i] << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |