zoukankan      html  css  js  c++  java
  • 寒假集训日志(七)——数据结构

      今天主要内容:1.RMQ算法

             2.树状数组

             3.线段树

    (一)RMQ算法

    A - RMQ
    Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

    Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

    Input

    Line 1: Two space-separated integers, N and Q.
    Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i
    Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

    Output

    Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

    Sample Input

    6 3
    1
    7
    3
    4
    2
    5
    1 5
    4 6
    2 2

    Sample Output

    6
    3
    0
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    
    using namespace std;
    typedef long long LL;
    const int maxN = 200010;
    
    int f1[maxN][32];
    int f2[maxN][32];
    int n,q;
    int logN[maxN];
    
    int Max(int a, int b){
        return a>b?a:b;
    }
    int Min(int a, int b){
        return a>b?b:a;
    }
    void init(){
        memset(logN, 0, sizeof(logN));
        for(int i=0 ;i<maxN; ++i){
            for(int j= 0 ;j<32; ++j){
                f1[i][j]=0; f2[i][j]=0;
            }
        }
    }
    
    void init_RMQ(){
        logN[1]= 0;
        for(int i =2 ;i<=maxN;++i){
            logN[i] =((i&(i-1))==0)?logN[i-1]+1:logN[i-1];
        }
        for(int j=1; j<=logN[n]; ++j){
            for(int i=1; i+(1<<j)-1<=n;++i){
                f1[i][j] = Max(f1[i][j-1],f1[i+(1<<(j-1))][j-1]);
            }
        }
        for(int j=1; j<=logN[n];++j){
            for(int i=1; i+(1<<j)-1<=n;++i){
                f2[i][j] = Min(f2[i][j-1],f2[i+(1<<(j-1))][j-1]);
            }
        }
    }
    
    int ANS(int x, int y){
        int k = logN[y-x+1];
        return Max(f1[x][k],f1[y-(1<<k)+1][k]) - Min(f2[x][k],f2[y-(1<<k)+1][k]);
    }
    int main(){
        scanf("%d %d",&n,&q);
        init();
        for(int i=1 ;i<=n;++i){
            scanf("%d",&f1[i][0]);
            f2[i][0] =f1[i][0];
        }
        init_RMQ();
      /*  for(int i=0 ;i<=n;++i){
            for(int j=0 ;j<logN[n];++j)
                cout<<f[i][j]<<" ";
            cout<<endl;
            }*/
        for(int j=1; j<=q; ++j){
            int a, b;
            scanf("%d%d", &a, &b);
            printf("%d
    ",ANS(a,b));
        }
    }

     (二)树状数组

      超牛逼的算法好吗!!!不知道创造这种方法的人的脑子是怎么长的!!!

      如果不记得了可以看看:http://blog.csdn.net/int64ago/article/details/7429868

    B - 树状数组or线段树
    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营 地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况。由于采取了某种先进的监测手段,所以每个工兵营地的人数C国都掌握的一清二楚,每个工 兵营地的人数都有可能发生变动,可能增加或减少若干人手,但这些都逃不过C国的监视。
    中央情报局要研究敌人究竟演习什么战术,所以Tidy要随时向Derek汇报某一段连续的工兵营地一共有多少人,例如Derek 问:“Tidy,马上汇报第3个营地到第10个营地共有多少人!”Tidy就要马上开始计算这一段的总人数并汇报。但敌兵营地的人数经常变动,而 Derek每次询问的段都不一样,所以Tidy不得不每次都一个一个营地的去数,很快就精疲力尽了,Derek对Tidy的计算速度越来越不满:"你个死 肥仔,算得这么慢,我炒你鱿鱼!”Tidy想:“你自己来算算看,这可真是一项累人的工作!我恨不得你炒我鱿鱼呢!”无奈之下,Tidy只好打电话向计算 机专家Windbreaker求救,Windbreaker说:“死肥仔,叫你平时做多点acm题和看多点算法书,现在尝到苦果了吧!”Tidy说:"我 知错了。。。"但Windbreaker已经挂掉电话了。Tidy很苦恼,这么算他真的会崩溃的,聪明的读者,你能写个程序帮他完成这项工作吗?不过如果 你的程序效率不够高的话,Tidy还是会受到Derek的责骂的.
     

    Input

    第一行一个整数T,表示有T组数据。
    每组数据第一行一个正整数N(N<=50000),表示敌人有N个工兵营地,接下来有N个正整数,第i个正整数ai代表第i个工兵营地里开始时有ai个人(1<=ai<=50)。
    接下来每行有一条命令,命令有4种形式:
    (1) Add i j,i和j为正整数,表示第i个营地增加j个人(j不超过30)
    (2)Sub i j ,i和j为正整数,表示第i个营地减少j个人(j不超过30);
    (3)Query i j ,i和j为正整数,i<=j,表示询问第i到第j个营地的总人数;
    (4)End 表示结束,这条命令在每组数据最后出现;
    每组数据最多有40000条命令
     

    Output

    对第i组数据,首先输出“Case i:”和回车,
    对于每个Query询问,输出一个整数并回车,表示询问的段中的总人数,这个数保持在int以内。
     

    Sample Input

    1 10 1 2 3 4 5 6 7 8 9 10 Query 1 3 Add 3 6 Query 2 7 Sub 10 2 Add 6 3 Query 3 10 End
     

    Sample Output

    Case 1: 6 33 59
    //判断输入语句可以用strcmp来判断
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<cstring>
    
    using namespace std;
    typedef long long LL;
    const int maxN = 50010;
    
    int N;
    int tree[maxN];
    
    int lowbit(int x){
        return x&-x;
    }
    
    void add(int k, int num){
        while(k<=N){
            tree[k]+=num;
            k+=lowbit(k);
        }
    }
    
    LL sum(int k){
        LL ans = 0;
        while(k){
            ans+=tree[k];
            k-=lowbit(k);
        }
        return ans;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int i=1; i<=T;++i){
            memset(tree,0,sizeof(tree));
            printf("Case %d:
    ",i);
            scanf("%d", &N);
            for(int i = 1 ; i<=N; ++i){
                int num;
                scanf("%d",&num);
                add(i,num);
            }
            char s[10];
            memset(s,0,sizeof(s));
            while(scanf("%s",s)!=EOF){
                if(!strcmp(s,"End"))    break;
                if(!strcmp(s,"Add")){
                    int a, b;
                    scanf("%d%d", &a, &b);
                    add(a,b);
                }
                if(!strcmp(s,"Sub")){
                    int a, b;
                    scanf("%d%d", &a, &b);
                    add(a,-b);
                }
                if(!strcmp(s,"Query")){
                    int a, b;
                    scanf("%d%d", &a, &b);
                    printf("%lld
    ", sum(b)-sum(a-1));
                }
            }
        }
        return 0;
    }
    (三)线段树
      有点类似于二分的算法。
      精髓在于区间更新的Lazy数组。
      转两个写的比较好的博客:1.http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html
    2.http://blog.csdn.net/acceptedxukai/article/details/6933446
    C - 线段树单点更新
    Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
    这让很多学生很反感。

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
     

    Input

    本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
    学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
    接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
     

    Output

    对于每一次询问操作,在一行里面输出最高成绩。
     

    Sample Input

    5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
     

    Sample Output

    5 6 5 9

    Hint

    Huge input,the C function scanf() will work better than cin 
    //这题又让我知道了一点,就算是读入单个字符也不要用scanf("%c")会超时,另外,scanf("%d,&tree[root])如果少了&的话,或者无限循环,都会出现段错误
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    
    const int maxN = 800005;
    const int INF = 0;
    int tree[maxN];
    
    void build_tree(int left, int right, int root){
        if(left==right){
            scanf("%d",&tree[root]);
            return ;
        }
        int mid = (left+right)>>1;
        build_tree(left,mid,root<<1);
        build_tree(mid+1,right,root<<1|1);
        tree[root] = max(tree[root<<1],tree[root<<1|1]);
    }
    
    void update(int id, int grade, int left, int right, int root){
        if(left==right){
            tree[root] = grade;
            return ;
        }
        int mid = (left+right)>>1;
        if(id<=mid)  update(id,grade,left,mid,root<<1);
        else        update(id,grade,mid+1,right,root<<1|1);
        tree[root] = max(tree[root<<1], tree[root<<1|1]);
    }
    
    int Query(int tleft, int tright, int left, int right, int root){
        if(tleft<=left && tright>=right){
            return tree[root];
        }
        else{
            int ans = INF;  int mid = (left+right)>>1;
            if(tleft<=mid)  ans= max(ans, Query(tleft,tright,left,mid,root<<1));
            if(tright>mid) ans= max(ans, Query(tleft,tright,mid+1,right,root<<1|1));
            return ans;
        }
    
    }
    int main(){
        int n, m;
        while(scanf("%d %d", &n, &m)!=EOF){
            build_tree(1,n,1);
            while(m--){
                int a, b;
                char s[3];
                scanf("%s%d%d", s, &a, &b);
                if(s[0]=='Q'){
                    printf("%d
    ", Query(a,b,1,n,1));
                }
                if(s[0]=='U')
                update(a,b,1,n,1);
            }
        }
        return 0;
    }
    D - 线段树区间更新
    Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
    Submit Status

    Description

    给出了一个序列,你需要处理如下两种询问。

    "C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

    "Q a b" 询问[a, b]区间中所有值的和。

    Input

    第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

    第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

    接下来Q行询问,格式如题目描述。

    Output

    对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

    Sample Input

    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4

    Sample Output

    4
    55
    9
    15
    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    
    const int maxN = 500005;
    
    LL sum[maxN], add[maxN];
    
    struct node{
        int l, r;
        int mid(){
            return (l+r)>>1;
        }
    }tree[maxN];
    
    void PushUp(int rt){
        sum[rt] = sum[rt<<1] + sum[rt<<1|1];
    }
    
    void PushDown(int rt, int m){
        if(add[rt]){
            add[rt<<1] += add[rt];
            add[rt<<1|1] += add[rt];
            sum[rt<<1] +=   add[rt]*(m - (m>>1));
            sum[rt<<1|1] += add[rt]*(m>>1);
            add[rt] = 0;
        }
    }
    
    void build (int l, int r, int rt){
        tree[rt].l = l ;
        tree[rt].r = r ;
        add[rt] = 0;
        if(l == r){
            scanf("%lld", &sum[rt]);
            return ;
        }
        int m = tree[rt].mid();
        build(l,m, rt<<1);
        build(m+1, r, rt<<1|1);
        PushUp(rt);
    }
    
    void update(int c, int l, int r, int rt){
        if(tree[rt].l ==l && tree[rt].r ==r){
            add[rt] += c;   sum[rt] += c*(r-l+1) ; //cichu
            return ;
        }
        if(tree[rt].l == tree[rt].r)    return ;
        PushDown(rt, tree[rt].r - tree[rt].l +1);
        int m = tree[rt].mid();
        if( r<= m)  update(c, l, r, rt<<1);
        else if(l > m)    update(c, l, r, rt<<1|1);
        else{
            update(c, l, m, rt<<1);
            update(c, m+1, r, rt<<1|1);
        }
        PushUp(rt);
    }
    
    LL query(int l, int r, int rt){
        if(l == tree[rt].l && r==tree[rt].r){
            return sum[rt];
        }
        PushDown(rt, tree[rt].r - tree[rt].l +1);
        int m = tree[rt].mid();
        LL res = 0 ;
        if(r<=m)    res+= query(l, r, rt<<1);
        else if(l>m)    res+=   query(l, r, rt<<1|1);
        else {
            res +=  query(l, m, rt<<1);
            res +=  query(m+1, r, rt<<1|1);
        }
        return res;
    }
    int main(){
        int n, m;
        while(scanf("%d %d", &n, &m)!=EOF){
            build(1,n,1);
            while(m--){
                char ch[2];
                scanf("%s", ch);
                int a, b, c;
                if(ch[0] == 'Q'){
                    scanf("%d %d", &a, &b);
                    printf("%lld
    ", query(a, b, 1));
                }
                else {
                    scanf("%d %d %d", &a, &b, &c);
                    update(c, a, b, 1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    C语言I博客作业06
    C语言I博客作业05
    C语言I博客作业04
    C语言I博客作业03
    C语言I博客作业02
    第一次作业
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
  • 原文地址:https://www.cnblogs.com/topW2W/p/5166030.html
Copyright © 2011-2022 走看看