P1276 校门外的树(增强版)(模拟)

题目传送门

P1276 校门外的树(增强版)

题目大意

给你一个0~l的区间初始为有树,每次0砍伐x到y的树或者树苗,每次1在x到y栽种树苗
求n次操作后,校门外的树苗的数量,和被砍伐的树苗

思路

又是想练习线段树的一题,但是线段树的解法又是非常的麻烦,可以参考大佬的写法
校门外的树(增强版)(线段树写法)
我闲麻烦就直接模拟了

AC Code

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
#define endl '\n'
#define INF 0x3f3f3f3f
#define int long long
#define debug(a) cout<<#a<<"="<<a<<endl;
// #define TDS_ACM_LOCAL
const int N=1e5 +9;
int l, n;
int op, x, y;
int a[N];
void solve(){
    cin>>l>>n;
    int ans=0;  
    // 0为树,-1为空,1为树苗
    for(int i=1; i<=n; i++){
        cin>>op>>x>>y;
        if(op==0){
            for(int j=x; j<=y; j++){
                if(a[j]==0) a[j]=-1;
                if(a[j]==1) a[j]=-1, ans++;   
            }
        }   
        if(op==1){
            for(int j=x; j<=y; j++){
                if(a[j]==-1)    a[j]=1;
            }
        }
    }
    int res=0;
    for(int i=0; i<=l; i++) if(a[i]==1) res++;
    cout<<res<<endl<<ans<<endl;
    return ;
}

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
#ifdef TDS_ACM_LOCAL
    freopen("D:\\VS code\\.vscode\\testall\\in.txt", "r", stdin);
    freopen("D:\\VS code\\.vscode\\testall\\out.txt", "w", stdout);
#endif
    solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值