HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy
题目连接:
http://acm.hdu.edu.cn/showproblem.php?pid=5654
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:
- j=i+1,k=j+1
- 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
Hint
题意
问你[l,r]区间内有多少个不同的三元组
三元组的定义如下:
i=j-1,j=k-1
a[i]<=a[j],a[j]<=a[k]
题解:
一开始在莫队怼这道题,T成狗了……
这道题没有修改操作,所以直接考虑离线,一开始可以把所有的三元组全部预处理出来
然后用一个树状数组去维护就好了,等价于去计算区间不同数的个数,只需要记录一个nxt[i]表示下一个数在哪儿就好了
可持久化线段树也可以随便搞这道题。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+7;
map<pair<int,pair<int,int> >,int>H;
pair<pair<int,int>,int>p[maxn];
int tot=0;
int a[maxn];
int n;
int nxt[maxn],las[maxn],flag[maxn];
int ans[maxn];
struct bit
{
int c[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int v)
{
if(x==0)return;
for(int i=x;i<maxn;i+=lowbit(i))
c[i]+=v;
}
int get(int x)
{
int ans = 0;
for(int i=x;i;i-=lowbit(i))
ans+=c[i];
return ans;
}
}L;
void init()
{
H.clear();
tot=0;
memset(L.c,0,sizeof(L.c));
memset(a,0,sizeof(a));
memset(p,0,sizeof(p));
memset(nxt,0,sizeof(nxt));
memset(las,0,sizeof(las));
memset(flag,0,sizeof(flag));
memset(ans,0,sizeof(ans));
}
void solve()
{
init();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n-2;i++)
{
if(a[i]<=a[i+1]&&a[i+1]<=a[i+2])
{
pair<int,pair<int,int> > C = make_pair(a[i],make_pair(a[i+1],a[i+2]));
if(H[C]==0)H[C]=++tot;
}
else
flag[i]=1;
}
for(int i=1;i<=tot;i++)las[i]=n+1;
for(int i=n-2;i>=1;i--)
{
pair<int,pair<int,int> > C = make_pair(a[i],make_pair(a[i+1],a[i+2]));
int id = H[C];
if(id==0)nxt[i]=n+1;
else
{
nxt[i]=las[id];
las[id]=i;
}
}
for(int i=1;i<=tot;i++)L.update(las[i],1);
int m;scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int l,r;scanf("%d%d",&l,&r);
p[i]=make_pair(make_pair(l,r-2),i);
}
sort(p+1,p+1+m);
for(int i=1,j=1;i<=n;i++)
{
while(j<=m&&p[j].first.first==i)
{
int r = p[j].first.second;
int id = p[j].second;
if(r<i)ans[id]=0;
else ans[id]=L.get(r);
j++;
}
if(!flag[i])L.update(i,-1);
if(nxt[i]!=n+1)L.update(nxt[i],1);
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
}
int main()
{
int t;scanf("%d",&t);
while(t--)solve();
}
HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数的更多相关文章
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组
xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...
- HDU5654xiaoxin and his watermelon candy 离线+树状数组
题意:bc 77div1 d题(中文题面),其实就是询问一个区间有多少不同的三元组,当然这个三元组要符合条件 分析(先奉上官方题解) 首先将数列中所有满足条件的三元组处理出来,数量不会超过 nn个. ...
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU 4605 Magic Ball Game (dfs+离线树状数组)
题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...
- 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]都相等, 那么这两个三 ...
- HDU 4267 A Simple Problem with Integers(树状数组区间更新)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K ...
随机推荐
- Java企业级电商项目架构演进之路 Tomcat集群与Redis分布式
史诗级Java/JavaWeb学习资源免费分享 欢迎关注我的微信公众号:"Java面试通关手册"(坚持原创,分享各种Java学习资源,面试题,优质文章,以及企业级Java实战项目回 ...
- myeclipse/eclipse安装反编译插件jadclipse
jad是一个使用比较广泛的Java反编译软件,jadClipse是jad在eclipse下的插件,下面像大家介绍下如何将jadclipse加入到myeclipse/eclipse中. 文件下载 (1) ...
- spring源码解析--事务篇(前篇)
对于每一个JAVA程序员,spring应该是再熟悉不过的框架了,它的功能有多强大我就不多说了,既然他有这么强大的功能,是如何实现的呢?这个就需要从他的原理去了解,而最直接了解原理的方式莫过于源码.当然 ...
- python基础(9)--递归、二叉算法、多维数组、正则表达式
1.递归 在函数内部,可以调其他函数,如果一个函数在内部调用它本身,这个函数就是递归函数.递归算法对解决一大类问题是十分有效的,它往往使算法的描述简洁而且易于裂解 递归算法解决问题的特点: 1)递归是 ...
- 洛谷 P1296奶牛的耳语 题解
题目传送门 这道题很显然可以用O(n2)的方法来做(记得排序),由于数据较水...但还是在for循环中加一些优化:++i,据说这样会快一些... #include<bits/stdc++.h&g ...
- Mysql聚合函数count(*) 的性能分析
你首先要明确的是,在不同的 MySQL 引擎中,count(*) 有不同的实现方式. MyISAM 引擎把一个表的总行数存在了磁盘上,因此执行 count(*) 的时候会直接返回这个数,效率很高: 而 ...
- 【严蔚敏】【数据结构题集(C语言版)】1.16 自大至小依次输出读入的三个整数X,Y,Z
#include <stdio.h> #include<stdlib.h> int main() { int x,y,z,temp; scanf("%d%d%d&qu ...
- day4 正则表达式(regular)
正则(regular),要使用正则表达式需要导入Python中的re(regular正则的缩写)模块.正则表达式是对字符串的处理,我们知道,字符串中有时候包含很多我们想要提取的信息,掌握这些处理字符串 ...
- Solr本地服务器搭建及查询
0.安装solr之前,确保已安装好java8, java -version 查看是否安装 1.新建本地目录solr1 并 解压两个压缩包文件 .tar.gz .tgz tomcat7 2.将CATA ...
- 找到最大或最小的N个元素---heapq模块
堆排序heapq的用法 基本用法: 复杂数据结构: # coding=utf- # example.py # Example of using heapq to find the N smallest ...