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 ...
随机推荐
- mysq配置
mysql运维 1.mysql配置文件:/etc/my.cnf mysql日记文件 :安装时候配置的,可以通过ps aux|grep mysqld 查询 ps aux|grep mysqld mysq ...
- 【Git/GitHub学习笔记】一键更新多个git仓库至远程
因为同时在本地维护几个Github的仓库,每次更新后每个仓库要重复三步提交同步,有点麻烦. 发现可以写.sh文件来实现一键更新. 比如我要更新我的BlogBackup和CodeRepo两个仓库的代码如 ...
- 【技术分享】ReBreakCaptcha:利用谷歌来破解谷歌的验证码
概述 从2016年开始,我就在琢磨寻找一种新的绕过谷歌验证码v2的方法会有多难,如果这种方法能够适用于任何环境而不仅仅是针对特定的案例,那这种方法将是非常理想的.接下来我将向你介绍ReBreakCap ...
- 使用非root用户启动tomcat
以下操作均为以root用户运行1.添加tomcat用户组 /usr/sbin/groupadd tomcat 2.添加tomcat用户,并限制登录 /usr/sbin/useradd -s /bin/ ...
- jekyll简单使用
jekyll build # => 当前文件夹中的内容将会生成到 ./site 文件夹中. jekyll build –destination <destination> # =&g ...
- 【hdoj_1753】大明A+B(大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 本题要求是,进行多位的小数加法,由于位数很多,所以不能用double类型存储,可以用字符串存储,然后 ...
- 【hdoj_1250】Hat's Fibonacci(大数)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1250 思路:本题的Fibonacci数列是扩展的四阶的Fibonacci数列,用递推关系式求解即可. 题目 ...
- 如何将svg图标快速转换成字体图标?
今天遇到一个客户需要我将页面的图标做成字体图标,想想哎可能整的麻烦,不过想想这也是对项目的一个优化 ( 1.字体图标直接用color自由控制颜色:2.整合在一起,减少http请求等 PS:平时 ...
- Oracle 入门学习笔记
linux命令 查看linux系统版本号 uname -r 或 uname -a 查看linux发行版本号 cat /etc/redhat-release 查看linux具体版本号 cat /proc ...
- sed实践
后来也找到一篇文章讲的很详细: http://www.cnblogs.com/ctaixw/p/5860221.html --------------------------------------- ...