#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#define inf 1e9
using namespace std;
const int maxn=1100005;
struct node{
int a,b,sum;
int maxn,minn;
int lazy;
};
node tree[maxn*4];
int a[maxn];
void build_tree(int p,int x,int y){
tree[p].a=x;tree[p].b=y;
tree[p].lazy=0;
int mid=(x+y)/2;
if(x<y){
build_tree((p<<1),x,mid);
build_tree(((p<<1)+1),mid+1,y);
tree[p].maxn=max(tree[(p<<1)].maxn,tree[((p<<1)+1)].maxn);
tree[p].minn=min(tree[(p<<1)].minn,tree[((p<<1)+1)].minn);
tree[p].sum=tree[(p<<1)].sum+tree[((p<<1)+1)].sum;
}
else{
tree[p].maxn=a[x];
tree[p].minn=a[x];
tree[p].sum=a[x];
}
}
void putdown(int p){
tree[p*2].lazy+=tree[p].lazy;
tree[p*2].maxn+=tree[p].lazy;
tree[p*2].minn+=tree[p].lazy;
tree[p*2].sum+=tree[p].lazy*(tree[p*2].b-tree[p*2].a+1);
tree[p*2+1].lazy+=tree[p].lazy;
tree[p*2+1].maxn+=tree[p].lazy;
tree[p*2+1].minn+=tree[p].lazy;
tree[p*2+1].sum+=tree[p].lazy*(tree[p*2+1].b-tree[p*2+1].a+1);
tree[p].lazy=0;
}
int getsum(int p,int x,int y){
if(tree[p].lazy)putdown(p);
if(tree[p].b<x||tree[p].a>y)return 0;
if(tree[p].b<=y&&tree[p].a>=x)return tree[p].sum;
else{
int total=0;
total+=getsum((p<<1),x,y);
total+=getsum(((p<<1)+1),x,y);
return total;
}
}
int getmaxn(int p,int s,int t){
if(tree[p].lazy)putdown(p);
if(t<tree[p].a||s>tree[p].b)return -inf;
if(s<=tree[p].a&&tree[p].b<=t)return tree[p].maxn;
else{
int lmax=-inf,rmax=-inf,mid=(tree[p].a+tree[p].b)/2;
if(tree[p].a<=t&&mid>=s)lmax=getmaxn(p<<1,s,t);
if(t>mid&&s<=tree[p].b)rmax=getmaxn(p*2+1,s,t);
return max(lmax,rmax);
}
}
int getmin(int p,int x,int y){
if(tree[p].lazy)putdown(p);
if(tree[p].b<x||tree[p].a>y)return inf; //记住一定要返回inf,不能返回0
if(tree[p].b<=y&&tree[p].a>=x)return tree[p].minn;
else{
int lmin=inf,rmin=inf;
lmin=getmin((p<<1),x,y);
rmin=getmin(((p<<1)+1),x,y);
return min(lmin,rmin);
}
}
/*void insert(int p,int k){
if(tree[p].a<=k&&k<=tree[p].b)tree[p].sum++;
int mid=(tree[p].a+tree[p].b)/2;
if(tree[p].a<=k&&k<=mid)insert((p<<1),k);
if(k>mid&&k<=tree[p].b)insert(((p<<1)+1),k);
}
void del(int p,int k){
if(tree[p].a<=k&&k<=tree[p].b){
if(tree[p].a==tree[p].b&&tree[p].sum){
tree[p].sum--;return;
}
del((p<<1),k);
del(((p<<1)+1),k);
tree[p].sum=tree[(p<<1)].sum+tree[((p<<1)+1)].sum;
}
}*/
void change2(int p,int x,int y,int d){
//if(tree[p].a==tree[p].b){tree[p].minn+=d;tree[p].maxn+=d;tree[p].sum+=d;return;}
if(x>tree[p].b||y<tree[p].a)return;
if(tree[p].lazy)putdown(p);
if(x<=tree[p].a&&tree[p].b<=y){
tree[p].lazy+=d;
tree[p].maxn+=d;
tree[p].minn+=d;
tree[p].sum+=d*(tree[p].b-tree[p].a+1);
return;
}
if(tree[p].lazy)putdown(p);
int mid=(tree[p].a+tree[p].b)>>1;
if(x<=mid&&y>=tree[p<<1].a)change2(p<<1,x,y,d);
if(y>mid&&x<=tree[((p<<1)+1)].b)change2(((p<<1)+1),x,y,d);
tree[p].maxn=max(tree[(p<<1)].maxn,tree[((p<<1)+1)].maxn);
tree[p].minn=min(tree[(p<<1)].minn,tree[((p<<1)+1)].minn);
tree[p].sum=tree[(p<<1)].sum+tree[((p<<1)+1)].sum;
}
int main(){
int n,s,m,i,j,k;
cin>>n>>s>>m;
for(i=1;i<=n;i++)a[i]=s;
build_tree(1,1,n);
for(i=1;i<=m;i++){
int t,a,b,x,y;
scanf("%d%d%d",&a,&b,&t);
int minn=getmin(1,a,b-1);
if(minn<t){
puts("N");
}
else{
change2(1,a,b-1,-t);
puts("T");
}
}
}
线段数模板
最新推荐文章于 2024-03-01 00:18:20 发布