题目链接:
题解:本题我们不能一上来就用树状数组来统计每一位的贡献,我们需要先对区间进行一个排序,先按照区间的右端点由小到大排序,再按照区间的左端点从小到大排序,再按照区间的k值从大到小排序。坑点是再加入值的时候不能用连续的区间修改,因为我们要加入的一些位置可能离散而不连续
#include<bits/stdc++.h>
typedef long long LL;
using namespace std;
const int maxn=1e6+10;
const LL mod=1e9+7;
const double pi=acos(-1.0);
inline LL read()
{
LL X=0; bool flag=1; char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') flag=0; ch=getchar();}
while(ch>='0'&&ch<='9') {X=(X<<1)+(X<<3)+ch-'0'; ch=getchar();}
if(flag) return X;
return ~(X-1);
}
LL n,m;
LL c[maxn];
LL vis[maxn];
struct node{
LL l,r,k;
}p[maxn];
bool cmp(node x,node y)
{
if(x.r!=y.r){
return x.r<y.r;
}
if(x.l!=y.l){
return x.l<y.l;
}
return x.k>y.k;
//return x.r<y.r;
}
void add(LL x,LL d)
{
while(x<=n){
c[x]+=d;
x+=(x&(-x));
}
}
LL query(LL x)
{
LL res=0;
while(x>0){
res+=c[x];
x-=(x&(-x));
}
return res;
}
int main()
{
scanf("%lld%lld",&n,&m);
for(LL i=1;i<=m;i++){
scanf("%lld%lld%lld",&p[i].l,&p[i].r,&p[i].k);
}
sort(p+1,p+1+m,cmp);
memset(vis,0,sizeof(vis));
LL res=0;
for(LL i=1;i<=m;i++){
LL l=p[i].l,r=p[i].r;
LL pre=query(r)-query(l-1);
if(pre<p[i].k){
LL ko=p[i].k-pre;
LL s=0;
for(LL j=p[i].r;j>=p[i].l;j--){
if(vis[j]==0){
add(j,1);
s++;
res++;
vis[j]=1;
if(s>=ko){
break;
}
}
}
}
else{
continue;
}
}
printf("%lld\n",res);
return 0;
}