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.  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
  这道题额,对于每个点建一棵线段树,表示以它为左端点的所有区间……口胡不清。
 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=;
int a[maxn],Hash[maxn],ok[maxn];
int pre[maxn],nxt[maxn],head[maxn],fa[maxn],son[maxn];
int sum[maxn*],ch[maxn*][],rt[maxn],cnt;
void Insert(int pre,int &rt,int l,int r,int g,int d){
rt=++cnt;
ch[rt][]=ch[pre][];
ch[rt][]=ch[pre][];
sum[rt]=sum[pre]+d;
if(l==r)return;
int mid=(l+r)>>;
if(mid>=g)Insert(ch[pre][],ch[rt][],l,mid,g,d);
else Insert(ch[pre][],ch[rt][],mid+,r,g,d);
} int Query(int rt,int l,int r,int a,int b){
if(l>=a&&r<=b)return sum[rt];
int mid=(l+r)>>,ret=;
if(mid>=a)ret=Query(ch[rt][],l,mid,a,b);
if(mid<b)ret+=Query(ch[rt][],mid+,r,a,b);
return ret;
}
void Init(){
memset(rt,,sizeof(rt));
memset(head,,sizeof(head));
memset(pre,,sizeof(pre));
memset(son,,sizeof(son));
memset(nxt,,sizeof(nxt));
memset(fa,,sizeof(fa));
memset(sum,,sizeof(sum));
memset(ok,,sizeof(ok));cnt=;
}
int main(){
int T,Q,n,l,r;a[]=-;
scanf("%d",&T);
while(T--){
Init();
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
Hash[i]=a[i];
sort(Hash+,Hash+n+);
for(int i=;i<=n;i++)
a[i]=lower_bound(Hash+,Hash+n+,a[i])-Hash;
for(int i=;i<n;i++)
if(a[i-]<=a[i]&&a[i]<=a[i+])
ok[i]=;
for(int i=;i<=n;i++){
pre[i]=head[a[i]];
nxt[pre[i]]=i;
head[a[i]]=i;
}
for(int i=;i<n;i++)
if(ok[i]){
int j=pre[i];
while(j&&(a[j-]!=a[i-]||a[j+]!=a[i+])){j=pre[j];}
fa[i]=j;son[j]=i;
}
for(int i=;i<n;i++){
if(!fa[i]&&ok[i])
Insert(rt[],rt[],,n,i,);
} for(int i=;i<n;i++){
if(!ok[i]){
rt[i]=rt[i-];
continue;
}
Insert(rt[i-],rt[i],,n,i,-);
if(son[i])Insert(rt[i],rt[i],,n,son[i],);
}
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&l,&r);
printf("%d\n",r->=l?Query(rt[l],,n,,r-):);
}
}
return ;
}

数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy的更多相关文章

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

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

  2. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  3. hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组

    题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...

  4. hdu 5654 xiaoxin and his watermelon candy 莫队

    题目链接 求给出的区间中有多少个三元组满足i+1=j=k-1 && a[i]<=a[j]<=a[k] 如果两个三元组的a[i], a[j], a[k]都相等, 那么这两个三 ...

  5. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

  6. 数据结构(主席树):COGS 2213. K个串

    2213. K个串 ★★★★   输入文件:bzoj_4504.in   输出文件:bzoj_4504.out   简单对比时间限制:20 s   内存限制:512 MB [题目描述] 兔子们在玩k个 ...

  7. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  8. 数据结构(主席树):COGS 2211. 谈笑风生

    2211. 谈笑风生 ★★★★   输入文件:laugh.in   输出文件:laugh.out   简单对比时间限制:3 s   内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...

  9. 主席树 hdu 4348

    题意:有一个由n个数组成的序列,有4中操作: 1.C l r d [l,r]这段区间都加上d 2.Q l r 询问[l,r]这段区间的和 3.H l r t 询问之前t时间[l,r]的区间和 4.B ...

随机推荐

  1. JavaScript 使用

    HTML 中的脚本必须位于 <script> 与 </script> 标签之间. 脚本可被放置在 HTML 页面的 <body> 和 <head> 部分 ...

  2. ASP.NET中如何生成图形验证码

    通常生成一个图形验证码主要 有3个步骤: (1)随机产生一个长度为N的随机字符串,N的值可由开发可由开发人员自行设置.该字符串可以包含数字.字母等. (2)将随机生成的字符串创建成图片,并显示. (3 ...

  3. 使用Spring简化JDBC操作数据库

    Spring的开发初衷是为了减轻企业级开发的复杂度,其对数据库访问的支持亦如此,使用Spring访问数据库能带来以下好处: 1.1     简化代码 使用原生的JDBC访问数据库,一般总是要执行以下步 ...

  4. Axiom3D学习日记 3.Cameras, Lights, and Shadows

    Camera 相机: 相机基础知识不写了,需要注意的是:axiom目前不支持同时操作多个相机. 创建,设置位置基本操作. _camera = _scene.CreateCamera("Mai ...

  5. linux教程:配置Tomcat开机启动

    我们在linux下安装好tomcat之后:经常是需要配置到开机启动的: 这样的话就不需要我们每次重启linux服务器之后自己在登陆运行startup.sh文件启动tomcat了 本次的演示环境是在ce ...

  6. [转]关于java中的 sychronized 同步方法 与 同步块的理解

    首先,需要说明一点,也是最重要的一点,无论是同步方法 还是 同步块 都是只针对同一个对象的多线程而言的,只有同一个对象产生的多线程,才会考虑到 同步方法 或者是 同步块,如果定义多个实例的同步,可以考 ...

  7. 共享受限资源,Brian的同步规则

    说明:如果一个变量是boolean,则此变量是原子性的,即赋值和返回值简单的操作在发生时没有中断的可能. 递增不是原子性炒作. 解决共享资源竞争: 1. 通过加锁,锁语句会产生相互排斥的效果,此种机制 ...

  8. .net excel利用NPOI导入oracle

    1.链接数据库 引用System.Data.OracleClient: //数据库链接字符串   Data Source如:192.168.5.153:1521/orcl string linkStr ...

  9. iOS开发之指定UIView的某几个角(小于4)为圆角

    在iOS开发中,我们经常会遇到View设置圆角的问题,如果需要将UIView的4个角全部都为圆角,做法相当简单,只需设置其Layer的cornerRadius属性即可(项目需要使用QuartzCore ...

  10. 不带头结点的单链表递归删除元素为X的结点

    #include <iostream> using namespace std; struct Node { Node *next; int elem; }; void creatList ...