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 ...
随机推荐
- ireport报表,打印时,报表加载失败的解决方法
1.报表加载失败图示 2.解决方法 原创作者:DSHORE 作者主页:http://www.cnblogs.com/dshore123/ 原文出自:http://www.cnblogs.com/dsh ...
- awk中NF,NR的含义
awk中NF和NR的意义,其实你已经知道NF和NR的意义了,NF代表的是一个文本文件中一行(一条记录)中的字段个数,NR代表的是这个文本文件的行数(记录数).在编程时特别是在数据处理时经常用到.建议你 ...
- hosts,命令行前面的显示
1,/etc/hosts,主机名ip配置文件. # Do not remove the following line, or various programs # that require netwo ...
- 浅谈Linux系统中如何查看进程 ——ps,pstree,top,w,全解
进程是一个其中运行着一个或多个线程的地址空间和这些线程所需要的系统资源.一般来说,Linux系统会在进程之间共享程序代码和系统函数库,所以在任何时刻内存中都只有代码的一份拷贝. 1,ps命令 作用:p ...
- C语言再学习之 setjmp与longjmp
前不久在阅读Quake3源代码的时候,看到一个陌生的函数:setjmp,一番google和查询后,觉得有必要针对setjmp和longjmp这对函数写一篇blog,总结一下. setjmp和longj ...
- Delphi XE增强的RTTI妙用--动态创建包中的窗口类
以前要在运行时创建package中的form类,必须要在form单元文件中这样注册类: Initialization RegisterClass(TForm3);Finalization UnRe ...
- 数据库介绍及MySQL安装
阅读目录 一.数据库是什么? 二.数据库特点 三. 什么是数据库管理系统(DataBase Management System 简称DBMS) 四.数据库服务器.数据管理系统.数据库.表与记录的关系( ...
- Porting of cURL to Android OS using NDK (from The Software Rogue)
Porting of cURL to Android OS using NDK In continuing my journey into Android territory, I decided ...
- Flyweight模式(亨元模式)
这应该算是最好理解的一个设计模式了吧·················· 面向对象语言的原则就是一切都是对象,但是如果真正使用起来,有时对象数可能显得很庞大,比如,字处理软件,如果以每个文字都作为一个 ...
- Redis学习-redis概述
最近刚刚接触了redis技术,对此有一些了解,这是简单做一点总结. Redis简介 首先,简单了解一下NoSQL(Not only sql),不要错误的理解为:没有SQL,而是不仅仅是SQL.NoSQ ...