题解

313 条题解

  • 8
    @ 2018-04-18 20:43:19
    #include<iostream>
    using namespace std;
    int main()
    {
        int l,m,i,s=0,j;
        cin>>l>>m;
        int a,b,c[l+1]={0};//搞数值和线段
        for(i=1;i<=m;i++)
        {
            cin>>a>>b;
            for(j=a;j<=b;j++) c[j]=1;//如果是区域的就设置为1
        }
        for(i=0;i<=l;i++)
        {
            if(c[i]==0) s++;//如果是0的话就加
        }
        cout<<s;//最后输出
        
        return 0;
    }
    

    请各位大哥别默默的复制,有帮助就扣个1行不

  • 2
    @ 2018-08-18 14:03:18

    线段树什么的都是不存在的。。。
    数据范围很小,暴力AC。。。

    #include<iostream>
    using namespace std;
    bool a[10001];
    int main()
    {
        int n,m;
        cin>>n>>m;
        for(int i=0;i<=n;i++)
        {
            a[i]=1;
        }
        n++;
        for(int i=1;i<=m;i++)
        {
            int x,y;
            cin>>x>>y;
            for(int j=x;j<=y;j++)
            {
                n-=a[j];
                a[j]=0;
            }
        }
        cout<<n;
        return 0;
    }
    
  • 2
    @ 2018-03-21 23:13:48
    #include<iostream>
    using namespace std;
    int main()
    {
        int n;
        cin >> n;
        int *a = new int[n+1];
        for (int i = 0; i < n+1; i++)
            a[i] = 1;
        int begin, end;
        int q;
        cin >> q;
        while (q--)
        {
            cin >> begin >> end;
            if (begin > end)
                swap(begin, end);
            for (int i = begin; i <= end; i++)
                a[i] = 0;
        }
        int sum = 0;
        for (int i = 0; i < n+1; i++)
            sum += a[i];
        cout << sum;
        return 0;
    }
    
  • 1
    @ 2018-11-03 21:07:55

    Pascal代码
    var
    l,m,i,j,x,y,s:longint;
    a:array[0..10000] of longint;
    begin
    read(l,m);
    for i:=1 to m do
    begin
    read(x,y);
    for j:=x to y do a[j]:=1;
    end;
    for i:=0 to l do
    if a[i]=0 then inc(s);
    write(s);
    end.

  • 1
    @ 2018-01-24 21:04:01

    #include <stdio.h>
    const int N=10005;
    int a[N];
    int main()
    {
    int i,j,k,l,m,n,sum=0;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++)
    {
    scanf("%d%d",&k,&l);
    for(j=k;j<=l;j++)
    a[j]++;
    }
    for(i=0;i<=n;i++)
    if(a[i]==0)sum++;
    printf("%d",sum);
    }

  • 1
    @ 2018-01-19 20:48:49
  • 1
    @ 2017-10-10 21:54:00

    #include<iostream>
    using namespace std;
    int a[10001];
    int main()
    {
    int sum=0,m,x,y,l,i,j,k;
    cin>>l>>m;
    for(i=0;i<m;i++)
    {
    cin>>x>>y;
    for(j=x;j<=y;j++)
    a[j]=1;
    }
    for(k=0;k<=l;k++)
    {
    if(a[k]!=1)
    sum++;
    }
    cout<<sum;
    }

  • 1
    @ 2017-10-01 09:08:33

    #include<iostream>
    using namespace std;
    int a[110],b[110],c[10010];
    int main()
    {
    int L,M,sum=0;
    cin>>L>>M;
    for(int i=0;i<M;i++)
    {
    cin>>a[i]>>b[i];
    for(int j=a[i];j<=b[i];j++)
    {
    c[j]=1;
    }
    }
    for(int i=0;i<=L;i++)
    {
    if(c[i]!=1)
    sum++;
    }
    cout<<sum<<endl;
    return 0;
    }

  • 1
    @ 2017-09-18 10:23:47

    线段树。。。。。。

    #include<cstdio>
    #include<iostream>
    using namespace std;
    
    struct node{
        int right,left,sum,lazy;
    }tree[10008*4];
    
    int n,m;
    
    void build(int now,int le,int ri){
        tree[now].left=le;tree[now].right=ri;
        if(le==ri){tree[now].sum=1;return;}
        int mid=(ri+le)>>1;
        build(now*2,le,mid);
        build(now*2+1,mid+1,ri);
        tree[now].sum=tree[now*2].sum+tree[now*2+1].sum;
        return;
    }
    
    void pushdown(int now){
        tree[now*2].sum=tree[now*2+1].sum=0;
        tree[now*2].lazy=tree[now*2+1].lazy=1;
        tree[now].lazy=0;return;
    }
    
    void update(int now,int le,int ri){
        if(tree[now].left==le&&tree[now].right==ri){
            tree[now].sum=0;
            tree[now].lazy=1;
            return;
        }
        if(tree[now].lazy)pushdown(now);
        int mid=(tree[now].left+tree[now].right)>>1;
        if(mid>=ri) update(now*2,le,ri);
        else if(mid<le) update(now*2+1,le,ri);
        else {update(now*2,le,mid);update(now*2+1,mid+1,ri);}
        tree[now].sum=tree[now*2].sum+tree[now*2+1].sum;
        return;
    }
    
    int search(int now){
        return tree[now].sum;
    }
    
    int main(){
        cin>>n>>m;
        build(1,0,n);
        for(int i=1;i<=m;i++){
            int a,b;
            cin>>a>>b;
            update(1,a,b);
        }
        cout<<search(1);
        return 0;
    }
    
    
  • 1
    @ 2017-05-08 07:55:48
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    #define maxn 500000+10
    int fa[maxn];
    int n, q;
    int ans ;
    
    int solve(int a,int b)
    {
        int i=b;
           for(int i = b ; i >= a ; )//从右向左扫描
        {
            if( fa[i] == i )
            {
                ans--;
                fa[i] = a - 1;//标记,同时用fa[i]来方便下次快速转换位置
                              //即直接免去查找此区间,跳到区间的一个左端点
                i--;
            }
            else//已经扫过
                i = fa[i];//直接跳到左区间端点向右一个位置(因为已经扫过)
        }
    }
    
    int main( )
    {
      scanf("%d %d",&n,&q);
         for(int i = 0 ; i <= n ; ++i)
           fa[i] = i;
        ans = n+1;
        for(int i = 1 ; i <= q ; i++)
        {
            int a, b;
             scanf("%d %d",&a,&b);
            if( a > b )
                swap( b, a );
            solve( a, b );
        }
            printf("%d\n",ans);
    }
    
  • 1
    @ 2010-04-15 15:31:03

    一次AC,奇迹!

  • 0
    @ 2020-02-09 11:11:28

    #include<stdio.h>
    bool s[10005];
    int main(){
    int l,m,j=0,a,b;
    scanf("%d%d",&l,&m);
    for(int i=0;i<m;i++){
    scanf("%d%d",&a,&b);
    for(int c=a;c<=b;c++) s[c]=1;
    }
    for(int i=0;i<=l;i++) if(!s[i]) j++;
    printf("%d",j);
    return 0;
    }

  • 0
    @ 2019-07-29 21:13:43

    给个行数很少的:
    #include<iostream>
    #include<memory.h>
    #include<algorithm>

    using namespace std;

    int main(){
    int L,M;
    cin>>L>>M;
    bool *tree = new bool[L+1];
    memset(tree,0,L+1);

    int start,end;
    for(int i=0;i<M;i++){
    cin>>start>>end;
    memset(tree+start,-1,end-start+1);
    }

    cout<<count(tree,tree+L+1,0)<<endl;
    return 0;
    }

  • 0
    @ 2019-06-17 15:21:08

    #include <iostream>
    using namespace std;
    int main(void)
    {
    int L,M;
    int M_begin[10000]={0},M_end[10000]={0};
    int tree[10001]={0};
    int i=0;
    cin>>L;
    cin>>M;
    for(i=0;i<=L;++i)tree[i]=1;
    i=0;
    while(i<M){
    cin>>M_begin[i];
    cin>>M_end[i];
    ++i;
    }
    for(i=0;i<M;++i){
    while(M_begin[i]<=M_end[i]){
    tree[M_begin[i]]=0;
    ++M_begin[i];
    }
    }
    M=0;
    for(i=0;i<=L;++i){
    if(tree[i])++M;
    }
    cout<<M;
    return 0;
    }

  • 0
    @ 2019-03-16 13:32:48
    #include<bits/stdc++.h>
    using namespace std;
    
    struct Sec {
        int l, r;
        Sec(int l_, int r_): l(l_), r(r_) {}
        friend bool operator<(const Sec& s1, const Sec& s2) {
            return s1.l < s2.l;
        }
    };
    vector<Sec> sec;
    
    int main() {
        int L, m;
        cin >> L >> m;
        for (int i = 0; i < m; i++) {
            int a, b;
            scanf("%d%d", &a, &b);
            sec.push_back(Sec(a, b));
        } 
        sort(sec.begin(), sec.end());
        int ans = 0, l = 0, r = -1;
        for (auto s: sec) {
            if (s.l <= r) {
                if (s.r <= r) continue;
                r = s.r;
            }
            else {
                ans += r - l + 1;
                l = s.l;
                r = s.r;
            }
        }
        ans += r - l + 1;
        cout << L - ans + 1 << endl;
        return 0;
    }
    
    
  • 0
    #include<iostream>  
    #include<cstdio>  
    #include<cstring>  
    using namespace std;  
    int l,m,x,y,num; bool b[10005];  
    int main(){  
        scanf("%d%d",&l,&m);  
        for (int i=1;i<=m;++i){  
            scanf("%d%d",&x,&y);  
            for (int j=x;j<=y;++j)  
              b[j]=true;  
        }  
        for (int i=0;i<=l;++i)  
          if (!b[i]) num++;  
        printf("%d",num);  
    }
    
  • 0
    @ 2018-12-30 14:10:03
    #Python
    L, M = map(int, input().split())
    
    treelist = set()
    
    for i in range(M):
        begin, end = map(int, input().split())
        tree = range(begin,end+1)
        treelist.update(tree)
        i += 1
    
    Llist = set(range(0, L + 1))
    Llist = Llist - treelist
    L = len(Llist)
    
    print(L)
    
    
  • 0
    @ 2018-10-25 08:16:27

    #include<cstdio>
    #include<iostream>
    using namespace std;
    #include<cstring>
    //以上不解释
    int L,M;
    //变量名称与原题相同
    int cnt=0;
    //剩余树的数目
    int vis[10000+10];
    //用来判断树木有没有被访问过
    int main()
    {
    memset(vis,0,sizeof(vis));//初始化该数组
    cin>>L>>M;//标准输入,不解释
    for(int i=0;i<=L;i++)
    vis[i]=0;//从0这个树开始,将这条路上所有的树标记为未访问过
    for(int i=1;i<=M;i++)
    {
    int head,tail;//定义区间的头和尾
    cin>>head>>tail;
    for(int j=head;j<=tail;j++)
    if(vis[j]==0)
    vis[j]=1;//从这个区间的头和尾开始,将所有区间内的树标记为访问过
    }
    for(int i=0;i<=L;i++)
    {
    if(vis[i]==0)
    cnt++;//计算未被访问过的树的数目
    }
    cout<<cnt<<endl;
    return 0;
    }

  • 0
    @ 2018-09-15 21:45:42

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    int tree[10010];//tree[n]为第n棵树
    int main()
    {
    int m,x,kai,jie,sum=0;
    cin>>m>>x;
    for(int i=1;i<=x;i++)//每个区域使用一次
    {
    cin>>kai>>jie;//开始和结束的坐标
    for(int t=kai;t<=jie;t++)
    {
    tree[t]=1;//在区域内的树给上标记,重复区域无影响
    }
    }
    for(int i=0;i<=m;++i)
    {
    if(tree[i]!=1) sum++;//输出没被标记的树
    }
    cout<<sum;
    }

  • 0
    @ 2018-08-08 13:48:09

    我来给Pascal党发题解,还是一句话,我是托儿索!
    var
    f:array[0..10000] of boolean;
    l,m,i,t,x,y,j:longint;
    begin
    fillchar(f,sizeof(f),true);
    t:=0;
    read(l,m);
    for i:=1 to m do
    begin
    read(x,y);
    for j:=x to y do
    f[j]:=false;
    end;
    for i:=0 to l do
    if f[i] then inc(t);
    write(t);
    end.

信息

ID
1103
难度
4
分类
模拟 点击显示
标签
递交数
13416
已通过
6151
通过率
46%
被复制
6
上传者