zoukankan      html  css  js  c++  java
  • HDOJ5875(线段树)

    Function

    Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
    Total Submission(s): 1701    Accepted Submission(s): 615


    Problem Description
    The shorter, the simpler. With this problem, you should be convinced of this truth.
      
      You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1lrN) is defined as:
    F(l,r)={AlF(l,r1) modArl=r;l<r.
    You job is to calculate F(l,r), for each query (l,r).
     
    Input
    There are multiple test cases.
      
      The first line of input contains a integer T, indicating number of test cases, and T test cases follow. 
      
      For each test case, the first line contains an integer N(1N100000).
      The second line contains N space-separated positive integers: A1,,AN (0Ai109).
      The third line contains an integer M denoting the number of queries. 
      The following M lines each contain two integers l,r (1lrN), representing a query.
     
    Output
    For each query(l,r), output F(l,r) on one line.
     
    Sample Input
    1
    3
    2 3 3
    1
    1 3
     
    Sample Output
    2
    思路:用val[l],每次%上在[l+1,r]中下一个最小的值。
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    const int MAXN = 100005;
    struct Node{
        int l, r, mn;
    }a[MAXN*3];
    int n, m, val[MAXN];
    void build(int rt, int l, int r)
    {
        a[rt].l = l;
        a[rt].r = r;
        if(l == r)
        {
            scanf("%d", &val[l]);
            a[rt].mn = val[l];
            return ;
        }
        int mid = (l + r) >> 1;
        build(rt << 1, l, mid);
        build((rt << 1) | 1, mid + 1, r);
        a[rt].mn = min(a[rt<<1].mn, a[(rt<<1)|1].mn);
    }
    void query(int rt, int l, int r, int &x)
    {
        if(x == 0)  return ;
        if(a[rt].mn > x)    return ;
        if(a[rt].l == a[rt].r)
        {
            x %= a[rt].mn;
            return ;
        }
        int mid = (a[rt].l + a[rt].r) >> 1;
        if(r <= mid)
        {
            query(rt << 1, l, r, x);
        }
        else if(mid < l)
        {
            query((rt << 1) | 1, l, r, x);
        }
        else
        {
            query(rt << 1, l, mid, x);
            query((rt << 1) | 1, mid + 1, r, x);
        }
    }
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            scanf("%d", &n);
            build(1, 1, n);
            scanf("%d", &m);
            while(m--)
            {
                int l, r;
                scanf("%d %d", &l, &r);
                if(l == r)
                {
                    printf("%d
    ", val[l]);
                }
                else
                {
                    int x = val[l];
                    query(1, l + 1, r, x);
                    printf("%d
    ", x);
                }
            }
        }
        return 0;
    }
     
  • 相关阅读:
    silverlight 会越来越好
    如何在C#里实现端口监视呢?
    我就这么活着
    无意间看到的两句话
    唉,心情
    有一种美丽的缘分,叫错过!
    HTML5之Canvas绘图——阴影效果呈现方法
    jQuery——动态给表格添加序号
    PHP代码——Curl实现网页代理proxy
    HTML5之Canvas绘图——Canvas画布调整之移动、缩放、旋转
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5874580.html
Copyright © 2011-2022 走看看