• HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数


    xiaoxin and his watermelon candy

    题目连接:

    http://acm.hdu.edu.cn/showproblem.php?pid=5654

    Description

    During his six grade summer vacation, xiaoxin got lots of watermelon candies from his leader when he did his internship at Tencent. Each watermelon candy has it's sweetness which denoted by an integer number.

    xiaoxin is very smart since he was a child. He arrange these candies in a line and at each time before eating candies, he selects three continuous watermelon candies from a specific range [L, R] to eat and the chosen triplet must satisfies:

    if he chooses a triplet (ai,aj,ak) then:

    1. j=i+1,k=j+1
    2. ai≤aj≤ak

    Your task is to calculate how many different ways xiaoxin can choose a triplet in range [L, R]?
    two triplets (a0,a1,a2) and (b0,b1,b2) are thought as different if and only if:
    a0≠b0 or a1≠b1 or a2≠b2

    Input

    This problem has multi test cases. First line contains a single integer T(T≤10) which represents the number of test cases.

    For each test case, the first line contains a single integer n(1≤n≤200,000)which represents number of watermelon candies and the following line contains n integer numbers which are given in the order same with xiaoxin arranged them from left to right.
    The third line is an integer Q(1≤200,000) which is the number of queries. In the following Q lines, each line contains two space seperated integers l,r(1≤l≤r≤n) which represents the range [l, r].

    Output

    For each query, print an integer which represents the number of ways xiaoxin can choose a triplet.

    Sample Input

    1
    5
    1 2 3 4 5
    3
    1 3
    1 4
    1 5

    Sample Output

    1
    2
    3

    Hint

    题意

    问你[l,r]区间内有多少个不同的三元组

    三元组的定义如下:

    i=j-1,j=k-1

    a[i]<=a[j],a[j]<=a[k]

    题解:

    一开始在莫队怼这道题,T成狗了……

    这道题没有修改操作,所以直接考虑离线,一开始可以把所有的三元组全部预处理出来

    然后用一个树状数组去维护就好了,等价于去计算区间不同数的个数,只需要记录一个nxt[i]表示下一个数在哪儿就好了

    可持久化线段树也可以随便搞这道题。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn = 2e5+7;
    
    map<pair<int,pair<int,int> >,int>H;
    pair<pair<int,int>,int>p[maxn];
    int tot=0;
    int a[maxn];
    int n;
    int nxt[maxn],las[maxn],flag[maxn];
    int ans[maxn];
    struct bit
    {
        int c[maxn];
        int lowbit(int x){return x&(-x);}
        void update(int x,int v)
        {
            if(x==0)return;
            for(int i=x;i<maxn;i+=lowbit(i))
                c[i]+=v;
        }
        int get(int x)
        {
            int ans = 0;
            for(int i=x;i;i-=lowbit(i))
                ans+=c[i];
            return ans;
        }
    }L;
    void init()
    {
        H.clear();
        tot=0;
        memset(L.c,0,sizeof(L.c));
        memset(a,0,sizeof(a));
        memset(p,0,sizeof(p));
        memset(nxt,0,sizeof(nxt));
        memset(las,0,sizeof(las));
        memset(flag,0,sizeof(flag));
        memset(ans,0,sizeof(ans));
    }
    void solve()
    {
        init();
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=n-2;i++)
        {
            if(a[i]<=a[i+1]&&a[i+1]<=a[i+2])
            {
                pair<int,pair<int,int> > C = make_pair(a[i],make_pair(a[i+1],a[i+2]));
                if(H[C]==0)H[C]=++tot;
            }
            else
                flag[i]=1;
        }
        for(int i=1;i<=tot;i++)las[i]=n+1;
        for(int i=n-2;i>=1;i--)
        {
            pair<int,pair<int,int> > C = make_pair(a[i],make_pair(a[i+1],a[i+2]));
            int id = H[C];
            if(id==0)nxt[i]=n+1;
            else
            {
                nxt[i]=las[id];
                las[id]=i;
            }
        }
        for(int i=1;i<=tot;i++)L.update(las[i],1);
        int m;scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int l,r;scanf("%d%d",&l,&r);
            p[i]=make_pair(make_pair(l,r-2),i);
        }
        sort(p+1,p+1+m);
        for(int i=1,j=1;i<=n;i++)
        {
            while(j<=m&&p[j].first.first==i)
            {
                int r = p[j].first.second;
                int id = p[j].second;
                if(r<i)ans[id]=0;
                else ans[id]=L.get(r);
                j++;
            }
            if(!flag[i])L.update(i,-1);
            if(nxt[i]!=n+1)L.update(nxt[i],1);
        }
        for(int i=1;i<=m;i++)
            printf("%d
    ",ans[i]);
    }
    int main()
    {
        int t;scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    Hadoop之MapReduce
    Hadoop之序列化
    Hadoop之mapreduce 实例五
    Hadoop之hive和hadoop的交互流程
    protel总结1
    将正数十进制转换为2~16进制数
    基于verilog分频器总结
    MATLAB设计FIR滤波器
    centos7+hadoop完全分布式集群搭建
    Linux下常用配置文件
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5325061.html
Copyright © 2020-2023  润新知