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();
}

最新文章

  1. 趣说游戏AI开发:对状态机的褒扬和批判
  2. Ubuntu 14.04 软件源服务器列表
  3. python 遍历文件夹 文件
  4. Codeforces Round #367 (Div. 2) D. Vasiliy&#39;s Multiset(可持久化Trie)
  5. Unity AngryBots愤怒的机器人demo研究
  6. python 批量请求url
  7. 限制页面内部链接访问源-HTML注释
  8. EASYUI+MVC4通用权限管理平台--前言
  9. Java Collection集合接口
  10. Yii::app()方法详解
  11. sql 游标循环遍历
  12. matlab读取多幅图片,并对读取的图片降采样和双三次插值
  13. android源码 分享1
  14. 第二次项目冲刺(Beta阶段)第一天
  15. Lubuntu下安装Python3.6
  16. 2014-2015 ACM-ICPC, Asia Xian Regional Contest(部分题解)
  17. axios 使用
  18. 自由线程FreeThreadDOMDocument
  19. jmeter创建基本的FTP测试计划
  20. shell脚本的基本结构以及如何执行

热门文章

  1. 判断Selenium加载完成
  2. 5.Longest Palindromic Substring---dp
  3. 2017 NWERC
  4. LINUX gcc安装rpm包顺序
  5. 2.rabbitmq 工作队列
  6. jersey中的 404 Not Found 错误。
  7. Codeforces 351D Jeff and Removing Periods(莫队+区间等差数列更新)
  8. Codeforces Round #423 Div. 2 C-String Reconstruction(思维)
  9. Django 中form的用法
  10. Hadoop HDFS 单节点部署方案