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>
#define ll long long
#define vt vector
#define pb push_back
#define sz(v) (int)v.size()
#define pii pair<int, int>
using namespace std;
const ll base = 107, mod = 1e9 + 7;
const int mxn = 2e5 + 5, inf = 1e9, sq = 400;
struct th{
int type, val, colour, id;
};
struct Node{
int sm, suf, idsuf;
};
int n, m, q;
vt<th>events[mxn + 1];
vt<pii>ask[mxn + 1];
int ans[mxn + 1];
Node st[4 * mxn + 1];
int mn[4 * mxn + 1], lz[4 * mxn + 1], co[mxn + 1];
void pull(int nd){
st[nd].sm = st[nd << 1].sm + st[nd << 1 | 1].sm;
ll cand1 = st[nd << 1 | 1].suf, cand2 = st[nd << 1 | 1].sm + st[nd << 1].suf;
if(cand1 > cand2){
st[nd].suf = cand1; st[nd].idsuf = st[nd << 1 | 1].idsuf;
}else{
st[nd].suf = cand2; st[nd].idsuf = st[nd << 1].idsuf;
}
mn[nd] = min(mn[nd << 1], mn[nd << 1 | 1]);
return;
}
Node comb(Node a, Node b){
Node res; res.sm = a.sm + b.sm;
ll cand1 = b.suf, cand2 = b.sm + a.suf;
if(cand1 > cand2){
res.suf = cand1; res.idsuf = b.idsuf;
}else{
res.suf = cand2; res.idsuf = a.idsuf;
}
return(res);
}
void push(int nd){
int &v = lz[nd];
lz[nd << 1] += v; lz[nd << 1 | 1] += v; mn[nd << 1] += v; mn[nd << 1 | 1] += v;
v = 0;
}
void upd(int nd, int l, int r, int id, int v){
if(id < l || id > r)return;
if(l == r){
st[nd].sm += v;
if(st[nd].sm > 0){
st[nd].suf = st[nd].sm; st[nd].idsuf = l;
}else{
st[nd].suf = 0; st[nd].idsuf = l + 1;
}
return;
}
push(nd);
int mid = (l + r) >> 1;
upd(nd << 1, l, mid, id, v); upd(nd << 1 | 1, mid + 1, r, id, v);
pull(nd);
}
void upd2(int nd, int l, int r, int ql, int qr, int v){
if(ql > r || qr < l)return;
if(ql <= l && qr >= r){
mn[nd] += v; lz[nd] += v;
return;
}
push(nd);
int mid = (l + r) >> 1;
upd2(nd << 1, l, mid, ql, qr, v); upd2(nd << 1 | 1, mid + 1, r, ql, qr, v);
mn[nd] = min(mn[nd << 1], mn[nd << 1 | 1]);
}
Node get(int nd, int l, int r, int ql, int qr){
if(ql <= l && qr >= r)return(st[nd]);
int mid = (l + r) >> 1;
push(nd);
if(qr <= mid)return(get(nd << 1, l, mid, ql, qr));
else if(ql > mid)return(get(nd << 1 | 1, mid + 1,r, ql, qr));
else return(comb(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr)));
}
int getl(int nd, int l, int r, int qr, int k){
if(l > r || l > qr)return(q + 1);
if(r <= qr){
int ans = q + 1;
if(mn[nd] >= k)return(l);
while(1){
if(l == r){
if(mn[nd] >= k)return(l);
return(ans);
}
int mid = (l + r) >> 1;
push(nd);
if(mn[nd << 1 | 1] >= k){
ans = mid + 1; nd = nd << 1; r = mid;
}else{
nd = (nd << 1 | 1); l = mid + 1;
}
}
}
push(nd);
int mid = (l + r) >> 1;
if(mid + 1 > qr){
return(getl(nd << 1, l, mid, qr, k));
}else{
int val = getl(nd << 1 | 1, mid + 1, r, qr, k);
if(val == mid + 1){
return(min(val, getl(nd << 1, l, mid, qr, k)));
}else{
return(val);
}
}
}
int getp(int nd, int l, int r, int id){
if(id == 0)return(0);
if(l == r)return(mn[nd]);
int mid = (l + r) >> 1;
push(nd);
if(id <= mid)return(getp(nd << 1, l, mid, id));
return(getp(nd << 1 | 1, mid + 1, r, id));
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m >> q;
set<int>active;
for(int i = 1; i <= q; i++){
int idq; cin >> idq;
if(idq == 1){
int l, r, x, k; cin >> l >> r >> k >> x;
events[l].pb({1, x, k, i}); events[r + 1].pb({1, -x, k, i});
co[i] = k;
}else if(idq == 2){
int l, r, k; cin >> l >> r >> k;
events[l].pb({2, k, -1, i}); events[r + 1].pb({2, -k, -1, i});
}else{
int id, x; cin >> id >> x;
ask[id].pb({x, i});
}
}
for(int i = 1; i <= n; i++){
for(auto [type, val, colour, id]: events[i]){
if(type == 1){
upd(1, 1, q, id, val);
upd2(1, 1, q, id, q, val);
if(val > 0)active.insert(id);
else active.erase(id);
}else{
upd(1, 1, q, id, -val);
upd2(1, 1, q, id, q, -val);
}
}
for(auto [x, idq]: ask[i]){
Node cool = get(1, 1, q, 1, idq);
//cout << cool.idsuf << " ";
if(cool.suf < x){
ans[idq] = -1;
}else{
int currp = getp(1, 1, q, cool.idsuf - 1);
//cout << currp + x << " " << " " << getp(1, 1, q, idq) << " ";
int idl = getl(1, 1, q, idq, currp + x);
//cout << idl << " ";
auto it = active.lower_bound(idl);
ans[idq] = co[*it];
}
}
}
for(int i = 1; i <= q; i++){
if(ans[i]){
if(ans[i] == -1)cout << 0 << "\n";
else cout << ans[i] << "\n";
}
}
}
Compilation message (stderr)
foodcourt.cpp: In function 'int main()':
foodcourt.cpp:143:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
143 | for(auto [type, val, colour, id]: events[i]){
| ^
foodcourt.cpp:156:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
156 | for(auto [x, idq]: ask[i]){
| ^
# | 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... |