tysgk @ 2022-10-05 20:00:53
#include<bits/stdc++.h>
using namespace std;
int main()
{
int l,m,cutdown=0,t=0;
cin>>l>>m;
int tree=l+1;
int a[m],b[m];
memset(a,0,sizeof(a));
for(int i=0;i<m;i++)
{
cin>>a[i]>>b[i];
}
for(int j=0;j<m;j++)
{
if((a[j+1]<=b[j]&&a[j+1]>=a[j])&&b[j+1]>=b[j])
{
t+=b[j]-a[j+1]+1;
}
if((b[j+1]<=b[j]&&b[j+1]>=a[j])&&a[j+1]<=a[j])
{
t+=b[j+1]-a[j]+1;
}
if((a[j+1]<=b[j]&&a[j+1]>=a[j])&&(b[j+1]<=b[j]&&b[j+1]>=a[j]))
{
t+=b[j]-a[j]+1;
}
}
for(int k=0;k<m;k++)
{
if(b[k]-a[k]==0)
{
cutdown+=0;
}
else cutdown+=b[k]-a[k]+1;
}
cout<<tree-(cutdown-t);
return 0;
}
by zhongshizhao1 @ 2022-10-07 13:25:32
区间有重复,这样做不行,重复部分你这样无法计算,你算得相当于砍了两遍一个地方的树
by zhongshizhao1 @ 2022-10-07 13:27:02
试试数组统计,被砍掉就标记为1,被砍掉两遍就是覆盖了一次罢了,然后统计有多少个1
by tysgk @ 2022-10-08 18:25:21
@zhongshizhao1 谢谢大佬