题解

318 条题解

  • 11
    @ 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
    @ 2020-07-22 11:38:37

    #include<bits/stdc++.h>
    using namespace std;
    int main(){
    int l;
    int m;
    int z,y;
    int ans=0;
    cin>>l>>m;
    int tree[l];
    int area_left[m]={0};
    int area_right[m]={0};
    for(int i=0;i<m;i++){
    cin>>area_left[i]>>area_right[i];
    }
    for(int j=0;j<=l;j++){
    tree[j]=1;
    }
    for(int k=0;k<m;k++){
    z=area_left[k];
    y=area_right[k];
    for(z=z;z<=y;z++){
    if(tree[z]==1)tree[z]=0;
    }
    }

    for(int n=0;n<=l;n++){
    if(tree[n]==1)ans++;
    }
    cout<<ans<<endl;
    }

  • 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
    @ 2020-11-28 19:58:33

    右手就行好吗?
    貌似是个红提呀,
    #include<iostream>
    #include<cstdio>
    #define lch now<<1
    #define rch ((now<<1)+1)
    #define smid ((l+r)>>1)
    #define MAXN 40000
    using namespace std;
    struct sgt_node{
    int sum;
    int l,r;
    }sgt[MAXN];
    int l,m;
    void build(int now,int l,int r){
    sgt[now].l=l;
    sgt[now].r=r;
    if(l==r){
    sgt[now].sum = 1;
    return;
    }
    build(lch,l,smid);
    build(rch,smid+1,r);
    sgt[now].sum=sgt[lch].sum+sgt[rch].sum;
    }
    void update(int now,int l,int r,int x,int y){ if(l>y||r<x||sgt[now].sum==0) return;
    if(x<=l&&y>=r){

    sgt[now].sum=0;
    return;
    }
    update(lch,l,smid,x,y);
    update(rch,smid+1,r,x,y);
    sgt[now].sum = sgt[lch].sum+sgt[rch].sum;
    }
    int main()
    {
    int left, right;
    cin>>l>>m;
    build(1,1,l+1);
    for(int i=1;i<=m;i++){
    cin>>left>>right;
    update(1,1,l+1,left+1,right+1);
    }
    cout<<sgt[1].sum;
    return 0;
    }

  • 1
    @ 2020-03-01 12:01:23

    水到炸

    var
      a:array[1..10000] of 0..1;
      l,n,i,x,y,j,s:longint;
    begin
      readln(l,n);
      for i:=0 to l do a[i]:=1;
      for i:=1 to n do
        begin
          readln(x,y);
          for j:=x to y do a[j]:=0;
        end;
      for i:=0 to l do if a[i]=1 then inc(s);
      writeln(s);
    end.
    
  • 0
    @ 2020-08-15 15:47:48

    蒟蒻用的诡异贪心法orz

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<algorithm>
    using namespace std;
    int l,m,st=0,sum=0;
    struct qwq{int s,e;}out[110];
    int read()
    {
        int x=0;
        char ch=getchar();
        while(ch<'0'||ch>'9') ch=getchar();
        while(ch>='0'&&ch<='9') {x=x*10+ch-48;ch=getchar();}
        return x;
    }
    bool cmp(qwq x,qwq y)
    {
        if(x.s==y.s) return x.e>y.e;
        else return x.s<y.s;
    }
    int main()
    {
        l=read();m=read();
        for(int i=1;i<=m;++i) {out[i].s=read();out[i].e=read();}
        sort(out+1,out+m+1,cmp);
        //for(int i=1;i<=m;++i) printf("%d %d\n",out[i].s,out[i].e);
        for(int i=1;i<=m;++i)
        {
            if(st<out[i].s) sum=sum+out[i].s-st;
            if(out[i].e>=st) st=out[i].e+1;
        }
        if(st<=l) sum=sum+l-st+1;
        printf("%d",sum);
        return 0;
    }
    
  • 0
    @ 2020-06-27 13:47:58
    #include<iostream>
    int line[10001] = { 0 };
    int main(void) {
        int L, M;
        scanf("%d %d", &L, &M);
        getchar();
        
        int b, e;
        for (int i = 0; i < M; i++) {
            scanf("%d %d", &b, &e);
            getchar();
            for (int t = b; t <= e; t++) {
                line[t] = 1;
            }
        }
        int c = 0;
        for (int i = 0; i <= L; i++) {
            if (line[i] == 0)
                c++;
        }
        printf("%d\n", c);
        return 0;
    }
    
  • 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-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.

  • 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.

  • 0
    @ 2018-07-22 12:50:20

    C++标程:
    #include <iostream>
    #define N 100010
    using namespace std;
    int l[N];//数组l代表马路的长度,标记为0
    int x,y;//区域的起始点和终止点
    int main()
    {
    int L,M;//变量L代表马路的长度,变量M代表区域的数目
    cin>>L>>M;
    for (int i=1;i<=M;i++)
    {
    cin>>x>>y;
    for (int j=x;j<=y;j++)//“挖地铁”
    l[j]=1;
    }
    int count=0;
    for (int i=0;i<=L;i++)//统计树的个数
    if (l[i]==0) count++;
    cout<<count<<endl;
    return 0;
    }

  • 0
    @ 2018-07-19 16:57:17

    #include<iostream>
    using namespace std;
    int main()
    {
    int s, e;
    int subn,tree;
    bool road[10001];
    for(int i=0;i<=10000;i++) road[i]=0;
    cin>>tree>>subn;
    for(int i=0;i<subn;i++)
    {
    cin>>s>>e;
    for(int j=s;j<=e;j++)
    if(!road[j])
    {
    road[j]=1;
    tree--;
    }
    }

    cout<<tree+1;
    return 0;
    }

信息

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