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并不是结束,而是开始
  • 相关阅读:
    android 75 新闻列表页面
    android 74 下载文本
    android 73 下载图片
    android 72 确定取消对话框,单选对话框,多选对话框
    android 71 ArrayAdapter和SimpleAdapter
    android 70 使用ListView把数据显示至屏幕
    maven如何将本地jar安装到本地仓库
    Centos6.7搭建ISCSI存储服务器
    解决maven打包编译出现File encoding has not been set问题
    MySQL 解决 emoji表情 的方法,使用utf8mb4 字符集(4字节 UTF-8 Unicode 编码)
  • 原文地址:https://www.cnblogs.com/yizimi/p/10056259.html
Copyright © 2011-2022 走看看