zoukankan      html  css  js  c++  java
  • 题解 P1047 【校门外的树】

    此题解分两部分,请选手对号入座

    1.普及组初学者

    我们可以通过for循环来实现操作,用一个数组来记录是否有树。当修改时,嵌套一个for循环可以把这一部分标记下。

    代码如下:

    #include <iostream>
    using namespace std;
    int main()
    {
        int a[10010],b,c,d,e,f;
        cin>>b>>c;
        for(int i=0;i<=b;i++)
        {
            a[i]=1;//把有树的标记上
        }
        for(int j=0;j<=c-1;j++)
        {
            cin>>d>>e;
            for(int k=d;k<=e;k++)
            {
                a[k]=0;//被砍的记得改为0
            }
        }
        for(int l=0;l<=b;l++)
        {
            if (a[l]==1)
                f++;//统计剩余的树
        }
        cout<<f;
        return 0;
    }
    

    2.提高组无脑做法

    这么简单,一定是线段树

    很简单,我们建一棵区间修改区间查询的线段树,然后,,,就操作就可以了。

    不会线段树的可以去这里这里

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<ctime>
    using namespace std;
    #define go(i,j,n,k) for(int i=j;i<=n;i+=k)
    #define fo(i,j,n,k) for(int i=j;i>=n;i-=k)
    #define rep(i,x) for(int i=h[x];i;i=e[i].nxt)
    #define mn 100010
    #define inf 2147483637
    #define ll long long
    //#define LOCAL
    #define Debug(...) fprintf(stderr, __VA_ARGS__)
    #define root 0,n,1
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define bson l,r,rt
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	while(ch>'9'||ch<'0'){if(ch=='-')f=-f;ch=getchar();}
    	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    	return x*f;
    }
    ll z[mn<<2],col[mn<<2];
    inline void update(int rt){
    	z[rt]=z[rt<<1]+z[rt<<1|1];
    }
    inline void color(int l,int r,int rt,ll v){
    	z[rt]=(r-l+1)*v;
    	col[rt]=v;
    }
    inline void push_col(int l,int r,int rt){
    	if(col[rt]){
    		int m=(l+r)>>1;
    		color(lson,col[rt]);
    		color(rson,col[rt]);
    		col[rt]=0;
    	}
    }
    inline void build(int l,int r,int rt){
    	if(l==r){z[rt]=0;return;}
    	int m=(l+r)>>1;
    	build(lson);
    	build(rson);
    	update(rt);
    }
    inline void modify(int l,int r,int rt,int nowl,int nowr,ll v){
    	if(nowl<=l && r<=nowr){color(bson,v);return;}
    	int m=(l+r)>>1;
    	push_col(bson);
    	if(nowl<=m)	modify(lson,nowl,nowr,v);
    	if(m<nowr)  modify(rson,nowl,nowr,v);
    	update(rt);
    }
    inline ll query(int l,int r,int rt,int nowl,int nowr){
    	if(nowl<=l && r<=nowr)	return z[rt];
    	int m=(l+r)>>1;
    	push_col(bson);
    	if(nowl<=m){
    		if(m<nowr)
    			return query(lson,nowl,nowr)+query(rson,nowl,nowr);
    		else
    			return query(lson,nowl,nowr);
    	}else{
    		return query(rson,nowl,nowr);
    	}
    }
    int n,m;
    int main(){
    	n=read();m=read();
    	build(0,n,1);
    	go(i,1,m,1){
    		int a=read(),b=read();
    		modify(root,a,b,1);
    	}
    	cout << n - query(root,0,n) + 1 << "
    ";
        //我们统计的是被砍的个数,所以我们最后要把它变成剩的树的数目
    	#ifdef LOCAL
        	Debug("
    My Time: %.3lfms
    ", (double)clock() / CLOCKS_PER_SEC);
    	#endif
    	return 0;
    }
    
    

    第12次发题解,希望可以兼顾所有的选手

    NOIP2018并不是结束,而是开始
  • 相关阅读:
    文本框只能填数字
    Win7 IIS 配置错误:不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的
    C#中小数点后保留两位小数,四舍五入的函数及使用方法(转)
    数据库中where与having区别
    NET Runtime version 2.0.50727.42
    C盘文件过大,C盘空间莫名丢失,pagefile.sys文件
    VS2008水晶报表变两页(重装系统后)
    win8 IIS配置
    从程序员到项目经理(5):程序员加油站 -- 懂电脑更要懂人脑
    从程序员到项目经理(4):程序员加油站 -- 不是人人都懂的学习要点
  • 原文地址:https://www.cnblogs.com/yizimi/p/10056259.html
Copyright © 2011-2022 走看看