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. 【BZOJ3926】【ZJOI2015】诸神眷顾的幻想乡 广义后缀自动机

    题目: 题目在这里 思路&做法: 参考的题解 既然只有\(20\)个叶子节点, 那么可以从每个叶子节点往上建一颗\(trie\)树, 然后合并成一棵大的\(trie\)树, 然后构建广义后缀自 ...

  2. POJ-3061 Subsequence 二分或尺取

    题面 题意:给你一个长度为n(n<100000)的数组,让你找到一个最短的连续子序列,使得子序列的和>=m  (m<1e9) 题解: 1 显然我们我们可以二分答案,然后利用前缀和判断 ...

  3. Netty简单介绍(非原创)

    文章大纲 一.Netty基础介绍二.Netty代码实战三.项目源码下载四.参考文章   一.Netty基础介绍 1. 简介 官方定义为:”Netty 是一款异步的事件驱动的网络应用程序框架,支持快速地 ...

  4. IE浏览器缓存导致Ajax请求失败

    在IE浏览器中通过Ajax请求后台的数据,如果Page请求是postback类型的,可能会导致Ajax请求失败的问题 我们都知道ajax能提高页面载入的速度主要的原因是通过ajax减少了重复数据的载入 ...

  5. [Offer收割]编程练习赛33

    矩阵游戏II 把每列的数字加起来当一行处理.因为每次操作两列,所以最后最多剩下一个负数.如果负数的个数是偶数,直接所有数字的绝对值加起来即可:若负数个数为奇数,把所有数的绝对值加起来减去其中最小的绝对 ...

  6. Java中从控制台输入数据的几种常用方法(转转)

    原文博客地址:https://www.cnblogs.com/SzBlog/p/5404246.html 一.使用标准输入串System.in  //System.in.read()一次只读入一个字节 ...

  7. (转载)RxJava 与 Retrofit 结合的最佳实践

    RxJava 与 Retrofit 结合的最佳实践 作者:tough1985 感谢 DaoCloud 为作者提供的 500 RMB 写作赞助: 成为赞助方 /开始写作 前言 RxJava和Retrof ...

  8. [ Tools ] [ MobaXterm ] [ SSH ] [ Linux ] 中文顯示解決

    預設是無法顯示中文的,需要修改連線的 Terminal Setting

  9. Python Tutorial笔记

    Python Tutorial笔记 Python入门指南 中文版及官方英文链接: Python入门指南 (3.5.2) http://www.pythondoc.com/pythontutorial3 ...

  10. 应用一:Vue之开发环境搭建

    简单分享下vue项目的开发环境搭建流程~ 1.安装nodeJS vue的运行是要依赖于node的npm的管理工具来实现,下载地址:https://nodejs.org/en/.安装完成之后以管理员身份 ...