xiaoxin and his watermelon candy

Problem 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 ninteger 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
 
题意:
  给你n个数,Q次询问
  每次询问,LR之间有多少个不同的三元组
题解:
  我们预处理出相同三元组的位置
  这题就变成了 区间处理不同数了
  树状数组做就好了
  但是要注意三元组取的 三位数,这个wa到死
#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<map>
using namespace std;
const int N = 1e6+, M = , mod = 1e9+, inf = 0x3f3f3f3f;
typedef long long ll;
//不同为1,相同为0 map< pair< int, pair< int,int> > ,int> mp;
int T,a[N],n,m,b[N],c,C[N],nex[N],p[N],H[N];
struct data{int l,r,id,ans;}Q[N];
int cmp1(data a,data b) {if(a.l==b.l) return a.r<b.r;else return a.l<b.l;}
int cmp2(data a,data b) {return a.id<b.id;}
int lowbit(int x) {
return x&(-x);
}
void add(int x, int add) {
for (; x <= n; x += lowbit(x)) {
C[x] += add;
}
}
int sum(int x) {
int s = ;
for (; x > ; x -= lowbit(x)) {
s += C[x];
}
return s;
}
int main() {
scanf("%d",&T);
while(T--) {
mp.clear();
memset(H,,sizeof(H));
memset(p,,sizeof(p));
memset(b,-,sizeof(b));
memset(C,,sizeof(C));memset(nex,,sizeof(nex));
scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d",&a[i]), b[i] = -,nex[i] = ;
int k = ;
for(int i=;i<=n;i++) {
if(a[i]>=a[i-]&&a[i-]>=a[i-]) {
if(mp[make_pair(a[i-],make_pair(a[i-],a[i]))]) b[i] = mp[make_pair(a[i-],make_pair(a[i-],a[i]))];
else b[i] = k,mp[make_pair(a[i-],make_pair(a[i-],a[i]))] = k,H[k] = , k++;
}
else b[i] = -;
}
for(int i=n;i>=;i--)
if(b[i]!=-)
nex[i]=p[b[i]],p[b[i]]=i;
for(int i=;i<k;i++)
add(p[i],);
int q;
scanf("%d",&q);
for(int i=;i<=q;i++) {
scanf("%d%d",&Q[i].l,&Q[i].r);Q[i].id = i;
}
sort(Q+,Q+q+,cmp1);
int l = ;
for(int i=;i<=q;i++) {
while(l<Q[i].l+) {
if(nex[l] ) add(nex[l],);l++;
}
if(Q[i].l+>Q[i].r) Q[i].ans = ;
else Q[i].ans = sum(Q[i].r) - sum(Q[i].l+-);
}
sort(Q+,Q+q+,cmp2);
for(int i=;i<=q;i++) {
printf("%d\n",Q[i].ans);
}
}
return ;
}

HDU 5654 xiaoxin and his watermelon candy 离线树状数组的更多相关文章

  1. HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数

    xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...

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

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

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

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

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

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

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

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

  6. hdu 5654 xiaoxin and his watermelon candy 莫队

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

  7. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  8. hdu 4605 Magic Ball Game (在线主席树/离线树状数组)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...

  9. HDU 2852 KiKi's K-Number(离线+树状数组)

    题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...

随机推荐

  1. Python生成器实现杨辉三角打印

    def triangles(): c = [1] while 1: yield c a,b=[0]+c,c+[0] c=[a[i]+b[i] for i in range(len(a))] n = 0 ...

  2. C#中DataSet中的relation

    //关系定义的方法是 DataRelation 变量名 = “DataSet对象”.Relations.Add("关系名",DataSet对象.主表.列名 , DataSet对象. ...

  3. Codeforces Round #447

    QAQ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> ...

  4. python课程设计笔记(三)turtle绘图库(海龟库)

    实例:绘制一条蟒蛇 #turtle:绘图库(海龟库) import turtle turtle.setup(650,350,200,200) turtle.penup() turtle.fd(-250 ...

  5. 自定义typecho后台路径

    如何自定义后台路径 Typecho 安装好后,默认的后台路径是 domain.com/admin/,为了提高安全性,我们允许以 domain.com/xxxx/ 的方式访问,其中 xxxx 是你自定义 ...

  6. 4) 十分钟学会android--建立第一个APP,启动另一个Activity

    在完成上一课(建立简单的用户界面)后,我们已经拥有了显示一个activity(一个界面)的app(应用),该activity包含了一个文本字段和一个按钮.在这节课中,我们将添加一些新的代码到MyAct ...

  7. 读书笔记8-浪潮之巅(part3)

    浪潮之巅 ——风险投资 <浪潮之巅>的前半部分列举了在现代史上举足轻重的几家大型科技公司的历史,虽说成功的公司各有各的绝招,但是读多之后又略显重复.无聊(这不是说原书的内容.描述是无聊的, ...

  8. Winform开发 如何为dataGridView 添加CheckBox列,并获取选中行

    //添加CheckBox列 DataGridViewCheckBoxColumn columncb = new DataGridViewCheckBoxColumn(); columncb.Heade ...

  9. vc++如何创建程序-构造函数

    如果给Animal带参,则提示没有缺省的构造函数了,缺省就是不带参数的 改进:从子类当中向基类传递代参的,这样他就会给Animal传递400,300 对一个常量来调用 #include<iost ...

  10. Windows批量查找文件

    for /r 目录名 %i in (匹配模式1,匹配模式2) do @echo %i for /r SATA %i in (*.txt) do @echo %i D:\REY\test>for ...