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

    科技2022-08-24  109

    题目传送门

    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; }
    Processed: 0.016, SQL: 9