码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

时间:2016-04-11 10:20:01      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

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

  这道题额,对于每个点建一棵线段树,表示以它为左端点的所有区间……口胡不清。
 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn=200010;
 7 int a[maxn],Hash[maxn],ok[maxn];
 8 int pre[maxn],nxt[maxn],head[maxn],fa[maxn],son[maxn];
 9 int sum[maxn*30],ch[maxn*30][2],rt[maxn],cnt;
10 void Insert(int pre,int &rt,int l,int r,int g,int d){
11     rt=++cnt;
12     ch[rt][0]=ch[pre][0];
13     ch[rt][1]=ch[pre][1];
14     sum[rt]=sum[pre]+d;
15     if(l==r)return;
16     int mid=(l+r)>>1;
17     if(mid>=g)Insert(ch[pre][0],ch[rt][0],l,mid,g,d);
18     else Insert(ch[pre][1],ch[rt][1],mid+1,r,g,d);
19 }
20 
21 int Query(int rt,int l,int r,int a,int b){
22     if(l>=a&&r<=b)return sum[rt];
23     int mid=(l+r)>>1,ret=0;
24     if(mid>=a)ret=Query(ch[rt][0],l,mid,a,b);
25     if(mid<b)ret+=Query(ch[rt][1],mid+1,r,a,b);
26     return ret;
27 }
28 void Init(){
29     memset(rt,0,sizeof(rt));
30     memset(head,0,sizeof(head));
31     memset(pre,0,sizeof(pre));
32     memset(son,0,sizeof(son));
33     memset(nxt,0,sizeof(nxt));
34     memset(fa,0,sizeof(fa));
35     memset(sum,0,sizeof(sum));
36     memset(ok,0,sizeof(ok));cnt=0;
37 }
38 int main(){
39     int T,Q,n,l,r;a[0]=-1;
40     scanf("%d",&T);
41     while(T--){
42         Init();
43         scanf("%d",&n);
44         for(int i=1;i<=n;i++)
45             scanf("%d",&a[i]);
46         for(int i=1;i<=n;i++)
47             Hash[i]=a[i];
48         sort(Hash+1,Hash+n+1);
49         for(int i=1;i<=n;i++)
50             a[i]=lower_bound(Hash+1,Hash+n+1,a[i])-Hash;
51         for(int i=2;i<n;i++)
52             if(a[i-1]<=a[i]&&a[i]<=a[i+1])
53                 ok[i]=1;
54         for(int i=1;i<=n;i++){
55             pre[i]=head[a[i]];
56             nxt[pre[i]]=i;
57             head[a[i]]=i;
58         }
59         for(int i=2;i<n;i++)
60             if(ok[i]){
61                 int j=pre[i];
62                 while(j&&(a[j-1]!=a[i-1]||a[j+1]!=a[i+1])){j=pre[j];}
63                 fa[i]=j;son[j]=i;
64             }
65         for(int i=2;i<n;i++){
66             if(!fa[i]&&ok[i])    
67                 Insert(rt[1],rt[1],1,n,i,1);    
68         }
69         
70         for(int i=2;i<n;i++){
71             if(!ok[i]){
72                 rt[i]=rt[i-1];
73                 continue;
74             }
75             Insert(rt[i-1],rt[i],1,n,i,-1);    
76             if(son[i])Insert(rt[i],rt[i],1,n,son[i],1);
77         }
78         scanf("%d",&Q);
79         while(Q--){
80             scanf("%d%d",&l,&r);
81             printf("%d\n",r-1>=l?Query(rt[l],1,n,1,r-1):0);
82         }
83     }
84     return 0;
85 }   

 

数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

标签:

原文地址:http://www.cnblogs.com/TenderRun/p/5377083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!