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:

  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

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 离线树状数组 区间不同数的个数的更多相关文章

  1. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  2. HDU5654xiaoxin and his watermelon candy 离线+树状数组

    题意:bc 77div1 d题(中文题面),其实就是询问一个区间有多少不同的三元组,当然这个三元组要符合条件 分析(先奉上官方题解) 首先将数列中所有满足条件的三元组处理出来,数量不会超过 nn个. ...

  3. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  4. HDU 4605 Magic Ball Game (dfs+离线树状数组)

    题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...

  5. hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

    Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...

  7. hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组

    题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...

  8. hdu 5654 xiaoxin and his watermelon candy 莫队

    题目链接 求给出的区间中有多少个三元组满足i+1=j=k-1 && a[i]<=a[j]<=a[k] 如果两个三元组的a[i], a[j], a[k]都相等, 那么这两个三 ...

  9. HDU 4267 A Simple Problem with Integers(树状数组区间更新)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K ...

随机推荐

  1. 五. Jmeter--HTTP Cookie Manager

    1. 添加HTTP Cookie Manager 2.添加登录login http,request info 和 HTTP Header Manager 中的信息是从fiddler中拿的, 至于hea ...

  2. perl6 单线程破解phpmyadmin脚本

    use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...

  3. linux下的僵尸进程处理SIGCHLD信号【转】

    转自:http://www.cnblogs.com/wuchanming/p/4020463.html 什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打 ...

  4. hdu 2852 KiKi's K-Number (线段树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2852 题意: 一个容器,三种操作: (1) 加入一个数 e (2) 删除一个数 e,如果不存在则输出 No Elment! (3) 查 ...

  5. 《跟老齐学Python Django实战》读后感

    1.说一下这本书,讲解的很细致,内容选取足够入门Django. 2.在学习这本书要注意的几点: <1>如果你想跟着敲这本书的代码必须要安装:Django版本1.10.1(当然也可以玩玩新版 ...

  6. 以太坊go-ethereum客户端JSON-RPC API调用(一)

    前几篇博客主要介绍了go-ethereum客户端不同环境的搭建,今天这篇博客是建立在前几排博客的基础上.当搭建完成之后,我们可以通过各种方式与节点进行交互(JavaScript Console.JSO ...

  7. Hadoop(五)分布式集群中HDFS系统的各种角色

    NameNode 学习目标 理解 namenode 的工作机制尤其是元数据管理机制,以增强对 HDFS 工作原理的 理解,及培养 hadoop 集群运营中“性能调优”.“namenode”故障问题的分 ...

  8. 借助svn进行半自动多台服务器上线部署

    传统简单保留 如果web服务器就那么几台,大致可以在测试服务器上测试好以后,直接在正式的web服务器 压缩拷贝一个,然后再覆盖下,进行简单暴力的发布. 这种纯手工发布往往会带来几个问题 压缩一不小心把 ...

  9. [翻译]HLS实践

    最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...

  10. Python全栈开发之10、网络编程

    网络编程就是如何在程序中实现两台计算机的通信,而两台计算机间的通信其实就是两个进程间的通信,进程间的通信主要通过socket(套接字)来描述ip地址(主机)和端口(进程)来实现的,因此我们学习网络编程 ...