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. oracle查看死锁和处理方法

    在操作数据库中经常会遇到表死锁问题,特别是不良的数据库设计和操作的时候,更容易遇到死锁问题.今天在系统中往oracle中添加数据时,特意快速多点几次添加的时候,就出现了死锁问题,而且刚好在读取和更新表 ...

  2. Linux试玩指令开机关机

    Linux内核最初只是由芬兰人李纳斯·托瓦兹(Linus Torvalds)在赫尔辛基大学上学时出于个人爱好而编写的. Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和U ...

  3. CSS3 box-sizing 属性

    定义和用法 box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "border-box& ...

  4. C#Socket编程socket.Connect权限出错问题及解决

    最近使用Vs2010编写Socket程序,客户端在调用socket.Connect()时,总是出现: 请求“System.Net.SocketPermission, System, Version=4 ...

  5. Android 安全性和权限

    自定义权限 permission <permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT& ...

  6. [转帖]FPGA--Vivado

    来源:http://home.eeworld.com.cn/my/space-uid-639749-blogid-267593.html 一般的,在Verilog中最常用的编码方式有二进制编码(Bin ...

  7. 数据库导出excel表数据

    - 执行之前 (错误) 消息 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 OLE DB 错误.错误代码: 0x80040E37.  (S ...

  8. 类和ID选择器的区别

    学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...

  9. gulp-htmlmin可以压缩html的gulp插件

    通过一条命令用Npm安装gulp-htmlmin: npm install gulp-htmlmin --save-dev 安装完毕后,打开gulpfile.js文件,我们里面编写一个task用来专门 ...

  10. 【HOJ1356】【Miller_rabin素性测试】Prime Judge

    Given a positive integer, your job is writing a program to determine whether it is a prime number or ...