题解 P7870 「Wdoi-4」兔已着陆

不用真的模拟一个个的蛋糕。直接将一个区间压入栈中即可。取出来时,注意将断的区间一分为二重新塞入。

#include <bits/stdc++.h>
using namespace std;
#define N 1000010
#define ll long long

template <class T>
inline void read(T& a){
	T x = 0, s = 1;
	char c = getchar();
	while(!isdigit(c)){ if(c == '-') s = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + (c ^ '0'); c = getchar(); }
	a = x * s;
	return ;
}

struct node{
  int l, r; 
} ; 

int n;
int Q; 
stack <node> stac; 
ll sum[N]; 

int main(){
  // freopen("hh.txt", "r", stdin); 
  read(Q);
  for(int i = 1; i <= 1e6; i++)
    sum[i] = sum[i-1] + i; 
  
  while(Q--){
    ll opt, x, y, k;
    read(opt);
    if(opt == 1){
      read(x), read(y);
      stac.push((node){x, y}); 
    }
    else{
      ll ans = 0; 
      read(k);
      while(k){
        node top = stac.top();  stac.pop(); 
        ll len = top.r - top.l + 1;
        if(len <= k){
         ans += sum[top.r] - sum[top.l - 1]; 
         k -= len; 
        } else{
          stac.push((node){top.l, top.r - k});
          ans += sum[top.r] - sum[top.r - k];
          break ; 
        }
      }
      cout << ans << endl; 
    }
  }

  return 0;
}

posted @ 2022-09-23 11:47  雪之下,树之旁  阅读(61)  评论(0编辑  收藏  举报