题目链接http://begin.lydsy.com/JudgeOnline/problem.php?id=2867
(sb没读看清题wa了一下午...)
线段树区间更新区间查询问题,需要lazy优化一下。
这道题需要判断请求是否能被满足,即涉及到线段状态的询问。当要求被满足的时候要减去相应线段上的座位数,因此涉及到线段的动态修改。
用 seat 这个域来记录线段[a,b]上的座位数,因此在建立线段树的时候,所有节点的 Seat 初始状态为 m。当[a,b]的左儿子或右儿子的状态 发生改变时,我们通过[a,b].seat = Min{左儿子.seat , 右儿子.seat}来对线段[a,b] 的座位数进行更新,即取[a,b]整条线段上所有座位数。
seat[rt]=min(seat[rt<<1],seat[rt<<1|1]);
判断请求能否通过:
若请求区域覆盖了该线段,则: 若座位数大于等于要求值,返回 True,否则返回 False
若请求区域跨越该线段的中点,则返回值 = Query(左儿子)&&Query(右儿子)
若请求区域在该线段中点的左方,则返回值 = Query(左儿子)
若请求区域在该线段中点的右方,则返回值 = Query(右儿子)
(Ps:注意k2-1,在这里wa了无数次)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e5+10;
int T,n,m,r;
int seat[maxn<<2],lazy[maxn<<2];
void PushUp(int rt){
seat[rt]=min(seat[rt<<1],seat[rt<<1|1]);
}
void PushDown(int rt){
if(lazy[rt]!=0){
seat[rt<<1]+=lazy[rt];
seat[rt<<1|1]+=lazy[rt];
lazy[rt<<1]+=lazy[rt];
lazy[rt<<1|1]+=lazy[rt];
lazy[rt]=0;
}
}
void build(int rt,int l,int r){
seat[rt]=m;
lazy[rt]=0;
if(l==r) return ;
int mid=(l+r)>>1;
build(lson);
build(rson);
}
void Update(int rt,int l,int r,int L,int R,int v){
if(L<=l&&r<=R){
seat[rt]-=v;
lazy[rt]-=v;
return ;
}
PushDown(rt);
int mid=(l+r)>>1;
if(L<=mid) Update(lson,L,R,v);
if(R>mid) Update(rson,L,R,v);
PushUp(rt);
}
bool Query(int rt,int l,int r,int L,int R,int v){
if(L<=l&&r<=R) return seat[rt]>=v;
PushDown(rt);
int mid=(l+r)>>1;
if(L<=mid&&R>mid) return Query(lson,L,R,v)&&Query(rson,L,R,v);
else if(L<=mid) return Query(lson,L,R,v);
else if(R>mid) return Query(rson,L,R,v);
}
int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
while(~scanf("%d%d%d",&n,&m,&r)){
build(1,1,n);
int k1,k2,v;
for(int i=0;i<r;i++){
scanf("%d%d%d",&k1,&k2,&v);
k2--;
if(Query(1,1,n,k1,k2,v)){
printf("T\n");
Update(1,1,n,k1,k2,v);
}
else printf("N\n");
}
}
return 0;
}