1. 程式人生 > >HDU 5654 xiaoxin and his watermelon candy 歸併樹

HDU 5654 xiaoxin and his watermelon candy 歸併樹

xiaoxin and his watermelon candy

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 161    Accepted Submission(s): 38


Problem 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.   aiajak

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:
a0b0 or a1b1 or
a2b2
 
Input This problem has multi test cases. First line contains a single integer T(T10) which represents the number of test cases.

For each test case, the first line contains a single integer n(1n200,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(1200,000) which is the number of queries. In the following Q lines, each line contains two space seperated integers l,r(1lrn) 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
   
  
 
   
  
 
Source BestCoder Round #77 (div.1)  參見官方題解。
處理以每個位置開始的滿足要求的三元組,a[i],a[i+1],a[i+2],(用pair<int,pair<int,int> >儲存)
然後對每個三元組map一下,用pre[i]表示和第i個三元組同的三元組前一次出現的位置,如果全面沒出現過就pre[i]=0,如果不滿足三元組要求的是pre[i]=n。
這樣對於查詢[L,R],,只需要計算區間[L,R-2]裡pre值小於L的個數就是答案。
用歸併樹維護查詢即可。
/****************
*PID:hdu5654
*Auth:Jonariguez
*****************
*/
#define lson k*2,l,m
#define rson k*2+1,m+1,r
#define rep(i,s,e) for(i=(s);i<=(e);i++)
#define For(j,s,e) For(j=(s);j<(e);j++)
#define sc(x) scanf("%d",&x)
#define In(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define pfn(x) printf("%d\n",(x))
#define Pf(x) printf("%I64d",(x))
#define Pfn(x) printf("%I64d\n",(x))
#define Pc printf(" ")
#define PY puts("YES")
#define PN puts("NO")
#include <stdio.h>
#include <string.h>
#include <string>
#include <math.h>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
typedef int Ll;
Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;}

const int maxn=200000+10;
int pre[maxn],a[maxn];
pair<int,pair<int,int> > pp;
map<pair<int,pair<int,int> >,int> mp;

struct Tree{
    vector<int> sum[maxn*4];
    void pushUp(int k,int l,int r){
        int lc=k*2,rc=k*2+1,m=(l+r)/2;
        sum[k].clear();
        sum[k].resize(r-l+1);
        merge(sum[lc].begin(),sum[lc].end(),sum[rc].begin(),sum[rc].end(),sum[k].begin());
    }
    void build(int k,int l,int r){
        if(l==r){
            sum[k].clear();
            sum[k].push_back(pre[l]);
            return ;
        }
        int m=(l+r)/2;
        build(lson);
        build(rson);
        pushUp(k,l,r);
    }
    int ask(int a,int b,int v,int k,int l,int r){
        if(a<=l && r<=b)
            return upper_bound(sum[k].begin(),sum[k].end(),v)-sum[k].begin();
        int m=(l+r)/2,res=0;
        if(a<=m)
            res+=ask(a,b,v,lson);
        if(b>m)
            res+=ask(a,b,v,rson);
        return res;
    }
}tree;

int main()
{
    int i,j,n,m,T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        mp.clear();
        for(i=1;i<=n;i++)
            sc(a[i]);
        if(n<3){
            scanf("%d",&m);
            while(m--){
                int x,y;
                sc(x);sc(y);
                puts("0");
            }
            continue;
        }
        for(i=1;i<=n-2;i++){
            if(!(a[i]<=a[i+1] && a[i+1]<=a[i+2]))
                pre[i]=n;
            else {
                pp=make_pair(a[i],make_pair(a[i+1],a[i+2]));
                if(mp[pp]!=0)
                    pre[i]=mp[pp];
                else pre[i]=0;
                mp[pp]=i;
            }
        }
        pre[n-1]=pre[n]=n;
        tree.build(1,1,n);
        scanf("%d",&m);
        while(m--){
            int x,y;
            scanf("%d%d",&x,&y);
            if(y-x<2)
                puts("0");
            else printf("%d\n",tree.ask(x,y-2,x-1,1,1,n));
        }
    }
    return 0;
}