数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
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
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].
这道题额,对于每个点建一棵线段树,表示以它为左端点的所有区间……口胡不清。
#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的更多相关文章
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组
xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...
- hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组
题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...
- hdu 5654 xiaoxin and his watermelon candy 莫队
题目链接 求给出的区间中有多少个三元组满足i+1=j=k-1 && a[i]<=a[j]<=a[k] 如果两个三元组的a[i], a[j], a[k]都相等, 那么这两个三 ...
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...
- 数据结构(主席树):COGS 2213. K个串
2213. K个串 ★★★★ 输入文件:bzoj_4504.in 输出文件:bzoj_4504.out 简单对比时间限制:20 s 内存限制:512 MB [题目描述] 兔子们在玩k个 ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- 数据结构(主席树):COGS 2211. 谈笑风生
2211. 谈笑风生 ★★★★ 输入文件:laugh.in 输出文件:laugh.out 简单对比时间限制:3 s 内存限制:512 MB [问题描述] 设T 为一棵有根树,我们做如下 ...
- 主席树 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 ...
随机推荐
- windows10UWP:如何判断一个文件或者文件夹是否存在?
使用 StorageFolder.TryGetItemAsync 方法,尝试按名称获取文件或文件夹,不需将错误捕捉逻辑添加到代码(就像使用 StorageFolder.GetItemAsync 一样) ...
- 多目标遗传算法 ------ NSGA-II (部分源码解析) 二进制编码的个体解码操作 decode.c
种群解码函数 decode_pop 为包装函数, 核心调用函数为 decode_ind , 对每个个体进行解码. /* Routines to decode the population */ ...
- webservice 发布到外网的时候
在web.config的<system.web></system.web>中间加入如下配置节内容<webServices> <protoco ...
- 编程基础-msdn编程指南笔记
此博仅为笔记,摘自msdn编程指南文档,链接地址:http://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx 注释:// 单行注释 /* 多行注释*/ ...
- Couchbase用的端口
文档首页: http://www.couchbase.com/documentation http://docs.couchbase.com/couchbase-manual-2.2/#prepara ...
- Axiom3D学习日记 3.Cameras, Lights, and Shadows
Camera 相机: 相机基础知识不写了,需要注意的是:axiom目前不支持同时操作多个相机. 创建,设置位置基本操作. _camera = _scene.CreateCamera("Mai ...
- HTML5媒体播放说明
HTML5中video标签播放m3u8整理 http://www.xue163.com/588880/39097/390970871.html 移动端HTML5<video>视频播放优化实 ...
- EF FluentAPI映射一对多 关系时候报错
提示很明显,不可为空的外键为空了,但是 问题是,我只是初始化 关系映射而已:见代码 public ColumnsCategoryMapConfiguration() { ToTable("C ...
- php_curl扩展在WINDOWS2003上如何添加
一.使用星外PHP安装后 二.修改环境变量,PATH: c:\php;c:\php\ext;%SystemRoot%\system32;
- 【POJ2155】【二维树状数组】Matrix
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...