1 solutions

  • 0
    @ 2025-11-5 14:58:36

    C :

    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int l,m,a[10006],x,y;
        while(scanf("%d%d",&l,&m)!=EOF)
        {
            for(int i = 0 ; i <= l ; i++)
                a[i]=1;
            while(m--)
            {
                scanf("%d%d",&x,&y);
                for(int i = x ; i <= y ; i++)
                    if(a[i]==1)
                        a[i]=0;
            }
            int sum = 0 ;
            for(int i = 0 ; i <= l ; i++)
                if(a[i]==1)
                sum++;
            printf("%d\n",sum);
        }
        return 0;
    }
    
    

    C++ :

    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    
    int main()
    {
        int l,m;
        cin>>l>>m;
        int i,a[10001];
        for(i=0;i<=l;i++)
            a[i]=1;
        while(m--)
        {
            int p,q;
            cin>>p>>q;
            for(i=p;i<=q;i++)
                a[i]=0;
        }
        int y=0;
        for(i=0;i<=l;i++)
            if(a[i])
               y++;
        cout<<y<<endl;
    }
    
    

    Pascal :

    type
     rec=record
      x,y,l,r,c:longint;
     end;
    var
     ans,l,m,i,x,y,tot:longint;
     tree:array[0..40000] of rec;
    procedure make(l,r:longint);
    var
     now:longint;
    begin
     inc(tot);
     now:=tot;
     tree[now].x:=l;
     tree[now].y:=r;
     if l+1<=r then
     begin
      tree[now].l:=tot+1;
      make(l,(l+r) div 2);
      tree[now].r:=tot+1;
      make((l+r) div 2+1,r);
     end;
    end;
    procedure insert(v,x,y:longint);
    begin
     if (x<=tree[v].x)and(tree[v].y<=y) then
     begin
      inc(tree[v].c);
      exit;
     end;
     if x<=(tree[v].x+tree[v].y) div 2 then insert(tree[v].l,x,y);
     if y>(tree[v].x+tree[v].y) div 2 then insert(tree[v].r,x,y);
    end;
    procedure find(v:longint);
    begin
     if v=0 then exit;
     if tree[v].c>0 then
     begin
      inc(ans,tree[v].y-tree[v].x+1);
      exit;
     end;
     find(tree[v].l);
     find(tree[v].r);
    end;
    begin
     readln(l,m);
     make(1,l+1);
     for i:=1 to m do
     begin
      readln(x,y);
      insert(1,x+1,y+1);
     end;
     find(1);
     //writeln(ans,' ',l);
     writeln(l+1-ans);
    end.
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner sc = new Scanner(System.in);
    		int L =sc.nextInt();
    		int n=sc.nextInt();
    		boolean[] flag = new boolean[L+1];
    		for(int i=0;i<n;i++) {
    			int x=sc.nextInt();
    			int y=sc.nextInt();
    			for(int j=x;j<=y;j++) {
    				flag[j]=true;
    			}
    		}
    		int num=L+1;
    		for(int i=0;i<=L;i++) {
    			if(flag[i]) {
    				num--;
    			}
    		}
    		System.out.println(num);
    	}
    
    }
    
    
    
    • 1

    Information

    ID
    16287
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By