题解

326 条题解

  • 0
    @ 2016-11-04 16:54:38

    数组模拟就好了
    #include <cstdio>
    #include <iostream>
    using namespace std;
    int L, N, i, x, y;
    int A[10001] = {0};
    int ans=0, j;
    int main() {
    cin>>L>>N;
    for(i=0; i<N; i++) {
    cin>>x>>y;
    for(j=x; j<=y; j++) {
    A[j] = 1;
    }
    }
    for(i=0; i<=L; i++)
    if(A[i] == 0) ans++;
    cout<<ans;
    return 0;
    }

  • 0
    @ 2016-10-27 01:34:21

    #include <iostream>

    using namespace std;

    bool road[10001];
    int mapb[101],mape[101],n,p,x;

    int main()
    {
    int i,j;
    cin>>n>>p;
    x=n+1;
    for(i=0;i<p;++i)
    cin>>mapb[i]>>mape[i];
    for(i=0;i<p;++i)
    for(j=mapb[i];j<=mape[i];++j)
    if(!road[j])road[j]=true,x--;
    cout<<x;
    return 0;
    }

  • 0
    @ 2016-09-06 21:16:32

    lishi

  • 0
    @ 2016-09-06 21:16:24

    本题为继陶陶摘苹果后noip历时第二水题
    我61秒就写完啦哈哈

    • @ 2016-10-27 01:34:55

      顶~~~~~~~~

  • 0
    @ 2016-09-06 21:14:32
    #include<iostream>
    using namespace std;
    int a[101],b[101],l[10001];
    int main()
    {
        int lon,m,sum=0;
        cin>>lon>>m;
        for(int i=0;i<=lon;i++) l[i]=1;
        for(int i=0;i<m;i++) cin>>a[i]>>b[i];
        for(int i=0;i<m;i++)
        {
            for(int k=a[i];k<=b[i];k++)    l[k]=0;
        }
        for(int i=0;i<=lon;i++)
        {
            if(l[i]==1)
            sum++;
        }
        cout<<sum;
        return 0;
    }
    
  • 0
    @ 2016-08-21 15:33:46

    水题,差分可秒杀
    ```c++
    #include <bits/stdc++.h>
    using namespace std;

    int L[10005], l, m, a, b;

    int main()
    {
    memset(L, 0, sizeof L);
    scanf("%d%d", &l, &m);
    for (int i = 1; i <= m; i++) {
    scanf("%d%d", &a, &b);
    L[a]++;
    L[b+1]--;
    }
    int ans = 0;
    for (int i = 0; i <= l; i++) {
    if (i) L[i] += L[i-1];
    if (L[i] == 0)
    ans++;
    }
    cout << ans << endl;
    return 0;
    }

  • 0
    @ 2016-08-16 23:29:32

    线段树
    ```c++
    #include <bits/stdc++.h>
    #define maxN 10010

    using namespace std ;
    typedef long long QAQ ;

    struct Tree {int l , r ;QAQ sum ,dec;};

    Tree tr[maxN<<2];

    void Build_Tree ( int x ,int y , int i ){
    tr[i].l = x ;
    tr[i].r = y ;
    if(x==y)tr[i].sum = 1 ;
    else {
    QAQ mid = ( tr[i].l + tr[i].r ) >> 1 ;
    Build_Tree ( x , mid , i<<1);
    Build_Tree ( mid+1 , y , i<<1|1);
    tr[i].sum = tr[i<<1].sum + tr[i<<1|1].sum ;
    }
    }

    void Update_Tree ( int q , int w , int i){
    if(!tr[i].sum)return ;
    if( q<=tr[i].l && w>=tr[i].r )tr[i].sum = 0 ;
    else {
    QAQ mid = (tr[i].l + tr[i].r ) >> 1 ;
    if ( q>mid )Update_Tree ( q , w , i<<1|1);
    else if ( w<=mid )Update_Tree ( q , w , i<<1);
    else{
    Update_Tree ( q , w , i<<1|1);
    Update_Tree ( q , w , i<<1);
    }
    tr[i].sum = tr[i<<1|1].sum + tr[i<<1].sum ;
    }
    }

    int main ( ){
    int N,M,l,r;
    scanf("%d %d",&N,&M);
    Build_Tree ( 0 , N , 1 );
    while ( M-- ){
    scanf("%d %d",&l,&r);
    Update_Tree ( l , r , 1 );
    }
    printf("%I64d",tr[1].sum);
    return 0 ;
    }
    ```

  • 0
    @ 2016-08-16 22:53:06

    数据太弱了不过我们还是写一个强一点的代码吧。codevs上可过500000的n和m数据
    ```c++
    测试数据 #0: Accepted, time = 0 ms, mem = 2512 KiB, score = 10
    测试数据 #1: Accepted, time = 0 ms, mem = 2512 KiB, score = 10
    测试数据 #2: Accepted, time = 0 ms, mem = 2508 KiB, score = 10
    测试数据 #3: Accepted, time = 0 ms, mem = 2512 KiB, score = 10
    测试数据 #4: Accepted, time = 15 ms, mem = 2512 KiB, score = 10
    测试数据 #5: Accepted, time = 0 ms, mem = 2512 KiB, score = 10
    测试数据 #6: Accepted, time = 0 ms, mem = 2508 KiB, score = 10
    测试数据 #7: Accepted, time = 0 ms, mem = 2508 KiB, score = 10
    测试数据 #8: Accepted, time = 0 ms, mem = 2508 KiB, score = 10
    测试数据 #9: Accepted, time = 0 ms, mem = 2516 KiB, score = 10
    Accepted, time = 15 ms, mem = 2516 KiB, score = 100
    #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);
    }
    ```

  • 0
    @ 2016-08-16 14:06:44

    编译成功

    测试数据 #0: Accepted, time = 0 ms, mem = 596 KiB, score = 10

    测试数据 #1: Accepted, time = 0 ms, mem = 600 KiB, score = 10

    测试数据 #2: Accepted, time = 15 ms, mem = 600 KiB, score = 10

    测试数据 #3: Accepted, time = 0 ms, mem = 600 KiB, score = 10

    测试数据 #4: Accepted, time = 0 ms, mem = 600 KiB, score = 10

    测试数据 #5: Accepted, time = 15 ms, mem = 600 KiB, score = 10

    测试数据 #6: Accepted, time = 0 ms, mem = 600 KiB, score = 10

    测试数据 #7: Accepted, time = 0 ms, mem = 596 KiB, score = 10

    测试数据 #8: Accepted, time = 0 ms, mem = 596 KiB, score = 10

    测试数据 #9: Accepted, time = 0 ms, mem = 600 KiB, score = 10

    Accepted, time = 30 ms, mem = 600 KiB, score = 100

    代码

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

  • 0
    @ 2016-08-16 14:05:14

    int i,j,k,l,n,m;
    cin>>j>>k;
    for(i=1;i<=k;i++){
    cin>>l>>n;
    m=n-l+1;
    j-=m;
    }
    cout<<j;

  • 0
    @ 2016-08-15 22:00:33
    program vijosP1103;
    
    type   segment_t=record
                    l,r:longint;
                    sl,sr:longint;
                    data:boolean;
                    mid:longint;
            end;
    
    var
            t:array[1..20005] of segment_t;
            size:longint;
            long,m:longint;
            tmp:longint;
    
    procedure init;
    begin
            t[1].l:=0;
            t[1].r:=long;
            size:=1;
    end;
    
    procedure build(id:longint);
    begin
            t[id].data:=false;
            if t[id].l=t[id].r then
                    exit;
            t[id].mid:=(t[id].l+t[id].r) div 2;
    
            inc(size);
            t[id].sl:=size;
            t[size].l:=t[id].l;
            t[size].r:=t[id].mid;
            build(size);
    
            inc(size);
            t[id].sr:=size;
            t[size].l:=t[id].mid+1;
            t[size].r:=t[id].r;
            build(size);
    end;
    
    procedure insert(s,d,id:longint);
    begin
            if (s=t[id].l) and (d=t[id].r) then
            begin
                    t[id].data:=true;
                    exit;
            end;
            if (s>t[id].mid) then
            begin
                    insert(s,d,t[id].sr);
                    exit;
            end;
    
            if (d<t[id].mid+1) then
            begin
                    insert(s,d,t[id].sl);
                    exit;
            end;
    
            insert(s,t[id].mid,t[id].sl);
            insert(t[id].mid+1,d,t[id].sr);
    end;
    
    procedure query(id:longint);
    begin
            if t[id].data=true then
            begin
                    inc(tmp,t[id].r-t[id].l+1);
                    exit;
            end;
            if t[id].l=t[id].r then exit;
            query(t[id].sl);
            query(t[id].sr);
    end;
    
    procedure inputdata;
    begin
            readln(long,m);
    end;
    
    procedure work1;
    var
            i:longint;
            s,d:longint;
    begin
            for i:=1 to m do
            begin
                    readln(s,d);
                    insert(s,d,1);
            end;
    end;
    
    procedure work2;
    begin
            query(1);
            writeln(long-tmp+1);
    end;
    
    begin
            tmp:=0;
            inputdata;
            init;
            build(1);
           work1;
            work2;
    end.
    
  • 0
    @ 2016-07-22 19:39:20
    #include<stdio.h>
    
    int main()
    {
        int l,m,i,j;
        int arr[10001]={0};
        int area[100][2];
        scanf("%d %d",&l,&m);
        for(i=0;i<m;i++)
        {
            scanf("%d %d",&area[i][0],&area[i][1]);
        }
        for(i=0;i<m;i++)
            for(j=area[i][0];j<=area[i][1];j++)
                arr[j]=1;
        j=0;
        for(i=0;i<=l;i++)
            if(arr[i]==0)
                j++;
        printf("%d",j);
        return 0;
    }
    
  • 0
    @ 2016-07-15 19:39:38

    var
    a:array[0..20000]of boolean;
    b:array[1..200]of longint;
    c:array[1..200]of longint;
    l,m,i,j,n:longint;
    begin
    readln(l,m);
    fillchar(a,sizeof(a),true);
    n:=0;
    for i:=1 to m do readln(b[i],c[i]);
    for i:=1 to m do
    for j:=b[i] to c[i] do
    a[j]:=false;
    for i:=0 to l do if a[i]=true then inc(n);
    writeln(n);
    end.

  • 0
    @ 2016-07-05 20:22:18

    #include<stdio.h>
    int tr[10005];
    void del(int a, int b) {
    for(int i = a; i <= b; i++)
    tr[i] = 0;
    return;
    }
    int main() {
    int l, m, tot = 0;
    int a[10], b[10];
    while(scanf("%d%d", &l, &m)) {
    for(int i = 1; i <= l; i++)
    tr[i] = 1;
    for(int i = 0; i < m; i++) {
    scanf("%d%d", &a, &b);
    del(a[i], b[i]);
    }
    for(int i = 1; i <= l; i ++) {
    if(tr[i] == 1) tot++;
    }
    printf("%d", tot);
    }
    }

  • 0
    @ 2016-04-20 19:24:11

    每次将移走区域的数全部设为false,最后剩下的累加,然后输出
    var
    a:array [0..10000] of boolean;
    i,m,n,d,j,o,k:integer;
    begin
    readln(d,o);
    fillchar(a,sizeof(a),true);
    for i:=1 to o do

    begin

    readln(m,n);
    for j:=m to n do a[j]:=false;
    end;
    for i:=0 to d do
    if a[i] then k:=k+1;
    writeln(k);
    end.

  • 0
    @ 2016-03-02 14:15:57

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>

    using namespace std;

    int l,m;
    bool a[10005];

    int main(){
    int c;
    memset(a,1,sizeof(a));
    int tmp1,tmp2;
    scanf("%d%d",&l,&m);
    int cnt = l + 1;
    for(int i = 1;i <= m;i ++){
    scanf("%d%d",&tmp1,&tmp2);
    for(int j = tmp1;j <= tmp2;j ++){
    if(a[j]){
    a[j] = 0; cnt --;
    }
    }
    }
    printf("%d",cnt);
    return 0;
    }

    #include <iostream>
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <algorithm>
    #include <math.h>

    using namespace std;

    int l,m;
    bool a[10005];

    int main(){
    int c;
    memset(a,1,sizeof(a));
    int tmp1,tmp2;
    scanf("%d%d",&l,&m);
    for(int i = 1;i <= m;i ++){
    scanf("%d%d",&tmp1,&tmp2);
    for(int j = tmp1;j <= tmp2;j ++){
    a[j] = 0;
    }
    }
    int cnt = 0;
    for(int i = 0;i <= l;i ++){
    if(a[i]) cnt ++;
    }
    printf("%d",cnt);
    return 0;
    }

  • 0
    @ 2016-02-01 17:44:15

    #include<iostream>

    typedef bool* Arraybol;
    typedef int Temp_int;

    int main(int argc,char** argv)
    {
    using std::cin;
    using std::cout;
    int n,m,i,j,ans;
    cin>>n>>m;
    ans=n+1;
    Arraybol a=new bool[n];
    bool** p=new bool;
    for(p=a;p!=a+n;p++)
    *p=true;
    for(int i=1;i<=m;i++)
    {
    Temp_int x,y;
    cin>>x>>y;
    for(int j=x;j<=y;j++)
    if(a[j])
    {
    ans--;
    a[j]=false;
    }
    }
    cout<<ans<<'\n';
    return 0;
    }

  • 0
    @ 2016-01-13 11:40:07

    main()
    {
    int a[10001]={0};
    int l,m;
    int x;
    int i,j;
    int n1,n2;
    scanf("%d%d",&l,&m);

    for(i=1;i<=m;i++)
    {
    scanf("%d%d",&n1,&n2);
    for (j=n1;j<=n2;j++)
    a[j]=1;
    }
    x=0;
    for(i=0;i<=l;i++)
    if(a[i]==0)
    x++;
    printf("%d",x);
    }
    数组定义10000会出错好几个点过不了= =

  • 0
    @ 2015-11-02 13:41:58

    var
    p:array[0..10000] of integer;
    x,y:array[1..100] of integer;
    j,e,l,m,g:integer;
    begin
    readln(l,m);
    for j:=1 to m do
    readln(x[j],y[j]);
    for j:=0 to l do
    p[j]:=1;
    for e:=1 to m do
    for j:=x[e] to y[e] do
    p[j]:=0;
    g:=0;
    for e:=0 to l do
    if p[e]=1 then inc(g);
    writeln(g);
    end.

  • 0
    @ 2015-10-30 23:05:22

    数据太弱……
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int tree[10010]={0};
    int main() {
    int l,m,t1,t2;
    cin >> l >> m;
    for (int i=1; i<=m; i++) {
    cin >> t1 >> t2;
    for (int j=t1; j<=t2; j++)
    if (!tree[j]) {
    tree[j]=1;
    l--;
    }
    }
    cout << ++l;
    return 0;
    }

信息

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