#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int maxn = 250005;
const ll INF = 2e18;
pair<ll,int> segTree[4*maxn];
ll p[4*maxn];
vector<pair<ll,int>> que[maxn];
vector<ll> a;
ll ans[maxn];
void build(int l,int r,int indV)
{
if(l == r)
{
segTree[indV] = {a[l],l};
p[indV] = 0;
return ;
}
int m = (l+r)/2;
build(l,m,indV*2+1);
build(m+1,r,indV*2+2);
segTree[indV] = min(segTree[indV*2+1],segTree[indV*2+2]);
p[indV] = 0;
return ;
}
void push(int l,int r,int indV)
{
if(l == r)
{
segTree[indV].first += p[indV];
p[indV] = 0;
return ;
}
segTree[indV].first += p[indV];
p[indV*2+1] += p[indV];
p[indV*2+2] += p[indV];
p[indV] = 0;
return ;
}
void modify(int lx,int rx,ll x,int l,int r,int indV)
{
push(l,r,indV);
if(l > rx || r < lx)
return ;
else if(l >= lx && r <= rx)
{
p[indV] += x;
push(l,r,indV);
return ;
}
int m = (l+r)/2;
modify(lx,rx,x,l,m,indV*2+1);
modify(lx,rx,x,m+1,r,indV*2+2);
segTree[indV] = min(segTree[indV*2+1],segTree[indV*2+2]);
return ;
}
pair<ll,int> get(int lx,int rx,int l,int r,int indV)
{
push(l,r,indV);
if(l > rx || r < lx)
return {INF,-1};
else if(l >= lx && r <= rx)
return segTree[indV];
int m = (l+r)/2;
pair<ll,int> sl = get(lx,rx,l,m,indV*2+1);
pair<ll,int> sr = get(lx,rx,m+1,r,indV*2+2);
return min(sl,sr);
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int n,m,q;
cin >> n >> m >> q;
vector<vector<int>> jop;
vector<ll> tl(n,0),al(n,0);
for(int j = 0;j < q;++j)
{
ans[j] = -1;
int t;
cin >> t;
if(t == 1)
{
int l,r,c,k;
cin >> l >> r >> c >> k;
l--;
r--;
for(int u = l;u <= r;++u)
{
tl[u] += k;
al[u] += k;
}
jop.push_back({l,r,c,k,j});
}
else if(t == 2)
{
int l,r,k;
cin >> l >> r >> k;
l--;
r--;
for(int u = l;u <= r;++u)
{
tl[u] -= k;
tl[u] = max(tl[u],ll(0));
}
}
else
{
ans[j] = 0;
ll a,b;
cin >> a >> b;
a--;
b--;
ll b2 = b;
b = b + al[a] - tl[a];
if(b2 >= tl[a])
{
ans[j] = 0;
}
else
que[a].push_back({b,j});
}
}
for(int i = 0;i < n;++i)
{
sort(que[i].rbegin(),que[i].rend());
}
a.resize(n);
for(int j = 0;j < n;++j)
{
a[j] = (que[j].size() == 0 ? INF : que[j].back().first);
//cout << a[j] << ' ';
}
// cout << endl;
build(0,n-1,0);
for(int j = 0;j < jop.size();++j)
{
modify(jop[j][0],jop[j][1],-jop[j][3],0,n-1,0);
while(true)
{
pair<ll,int> cc = get(jop[j][0],jop[j][1],0,n-1,0);
//cout << j << ' ' << cc.first << ' ' << cc.second << endl;
if(cc.first < 0)
{
int pos = cc.second;
int qpos = que[pos].back().second;
ans[qpos] = jop[j][2];
if(que[pos].size() == 1)
modify(pos,pos,INF,0,n-1,0);
else
modify(pos,pos,que[pos].rbegin()[1].first-que[pos].back().first,0,n-1,0);
que[pos].pop_back();
}
else
break;
}
}
for(int j = 0;j < q;++j)
{
if(ans[j] != -1)
cout << ans[j] << "\n";
}
}
# | 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... |