嘟嘟嘟


这应该算是整体二分入门题吧。


唯一的区别就是这次是区间修改,树状数组可能不太好做,换成线段树就好了。
然后每一次别忘了清空。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e4 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, m, cnt = 0;
struct Node
{
  int id, L, R; ll d;
}t[maxn], tl[maxn], tr[maxn];
int ans[maxn];

int l[maxn << 2], r[maxn << 2];
ll sum[maxn << 2], lzy[maxn << 2];
inline void build(int L, int R, int now)
{
  l[now] = L; r[now] = R;
  if(L == R) return;
  int mid = (L + R) >> 1;
  build(L, mid, now << 1);
  build(mid + 1, R, now << 1 | 1);
}
inline void pushdown(int now)
{
  if(lzy[now])
    {
      sum[now << 1] += (ll)(r[now << 1] - l[now << 1] + 1) * lzy[now];
      sum[now << 1 | 1] += (ll)(r[now << 1 | 1] - l[now << 1 | 1] + 1) * lzy[now];
      lzy[now << 1] += lzy[now]; lzy[now << 1 | 1] += lzy[now];
      lzy[now] = 0;
    }
}
inline void update(int L, int R, int now, int d)
{
  if(L == l[now] && R == r[now])
    {
      sum[now] += (R - L + 1) * d; lzy[now] += d;
      return;
    }
  pushdown(now);
  int mid = (l[now] + r[now]) >> 1;
  if(R <= mid) update(L, R, now << 1, d);
  else if(L > mid) update(L, R, now << 1 | 1, d);
  else update(L, mid, now << 1, d), update(mid + 1, R, now << 1 | 1, d);
  sum[now] = sum[now << 1] + sum[now << 1 | 1];
}
inline ll query(int L, int R, int now)
{
  if(L == l[now] && R == r[now]) return sum[now];
  pushdown(now);
  int mid = (l[now] + r[now]) >> 1;
  if(R <= mid) return query(L, R, now << 1);
  else if(L > mid) return query(L, R, now << 1 | 1);
  else return query(L, mid, now << 1) + query(mid + 1, R, now << 1 | 1);
}

inline void solve(int vl, int vr, int ql, int qr)
{
  if(ql > qr) return;
  if(vl == vr)
    {
      for(int i = ql; i <= qr; ++i)
	if(t[i].id) ans[t[i].id] = vl;
      return;
    }
  int mid = (vl + vr) >> 1;
  int id1 = 0, id2 = 0;
  for(int i = ql; i <= qr; ++i)
    {
      if(!t[i].id)
	{
	  if(t[i].d > mid) update(t[i].L, t[i].R, 1, 1), tr[++id2] = t[i];
	  else tl[++id1] = t[i];
	}
      else
	{
	  ll tp = query(t[i].L, t[i].R, 1);
	  if(tp < t[i].d) t[i].d -= tp, tl[++id1] = t[i];
	  else tr[++id2] = t[i];
	}
    }
  for(int i = 1; i <= id2; ++i) if(!tr[i].id) update(tr[i].L, tr[i].R, 1, -1);
  for(int i = 1; i <= id1; ++i) t[ql + i - 1] = tl[i];
  for(int i = 1; i <= id2; ++i) t[ql + id1 + i - 1] = tr[i];
  solve(vl, mid, ql, ql + id1 - 1);
  solve(mid + 1, vr, ql + id1, qr);
}

int main()
{
  n = read(); m = read();
  build(1, n, 1);
  for(int i = 1; i <= m; ++i)
    {
      int op = read(), L = read(), R = read(), d = read();
      t[i] = (Node){op == 2 ? ++cnt : 0, L, R, d};
    }
  solve(-n, n, 1, m);
  for(int i = 1; i <= cnt; ++i) write(ans[i]), enter;
  return 0;
}