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

pid=5654">【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

xiaoxin and his watermelon candy

Time Limit: 4000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 233    Accepted Submission(s): 61

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
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

Source

BestCoder Round #77 (div.1)

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5650 5649 pid=5648" target="_blank">5648 

pid=5646" target="_blank">5646 5645

题目大意:有n个糖果。从左到右列出每一个糖果的甜度

之后有Q次查询,每次查询[L,R]中三元组的个数

这个三元组要求满足为连续的三个值,然后这三个值为非递减。

问[L,R]中不反复的三元组的个数。

反复表示三元组中三个数均对影响等。

假设反复则仅仅记一次

正解为主席树………………额………………恩…………

搜到这个离线+树状数组的写法,给大家分享下

思路非常巧妙。先离线存储全部查询。

然后对查询进行排序,以右边界从小到大排序。

然后从1到n開始枚举位置pos

每到一个位置,看一下从当前往后连续的三个满不满足题目中三元组的要求,假设满足。在树状数组中1的位置+1 pos+1处-1

如今让我们先不考虑反复。

经过如上处理,对于全部右边界等于pos+2的区间,树状数组中0~L的值即为三元组个数!

由于假设满足R等于pos+2 事实上就是找全部出现过的l >= L的三元组,在遍历的过程中。每一个满足要求的三元组pos+1处-1了,事实上就是求0~L的区间和了

想想看~~

至于R 等于pos+2 因为对查询依照R排序了,所以在遍历的过程中对于每一个pos都把查询遍历到右区间pos+2就好啦

这里没有去重,关于去重。我是琢磨了好久……做法就是哈希,哈希三元组。

假设没出现过。跟上面一样,在线段树1处+1

假设出现过,须要在上次出现的位置+1处 累加上一个1

这样就能够避免反复统计了

做这道题真长记性……因为要哈希,排序必须对全部元素都进行比較。

否则会出现重叠!

这也是跟其内部实现相关。

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

int bit[233333];
int n;

int Lowbit(int x)
{
	return x&(-x);
}

int sum(int x)
{
	int ans = 0;
	while(x)
	{
		ans += bit[x];
		x -= Lowbit(x);
	}
	return ans;
}

void add(int x,int d)
{
	while(x <= n)
	{
		bit[x] += d;
		x += Lowbit(x);
	}
}

struct Point
{
	int l,r,id;
	bool operator <(const struct Point a)const
	{
		return r == a.r?

l == a.l? id < a.id: l < a.l: r < a.r;
	}
	Point(int _l = 0,int _r = 0,int _id = 0):l(_l),r(_r),id(_id){};
};

Point pt[233333];
int num[233333];
int ans[233333];
int p[233333];

int main()
{
	int t,m;

	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i = 1; i <= n; ++i)
			scanf("%d",&num[i]);
		scanf("%d",&m);
		for(int i = 0; i < m; ++i)
		{
			scanf("%d%d",&pt[i].l,&pt[i].r);
			pt[i].id = i;
		}

		memset(ans,0,sizeof(ans));
		memset(bit,0,sizeof(bit));
		sort(pt,pt+m);

		map <Point,int> mp;
		int j = 0;
		int tp = 1;
		for(int i = 1; i <= n-2; ++i)
		{
			if(num[i] <= num[i+1] && num[i+1] <= num[i+2])
			{
				if(!mp[Point(num[i],num[i+1],num[i+2])])
				{
					mp[Point(num[i],num[i+1],num[i+2])] = tp;
					p[tp++] = 0;
				}
				int x = mp[Point(num[i],num[i+1],num[i+2])];
				add(p[x]+1,1);
				add(i+1,-1);
				p[x] = i;
			}
			for(; j < m && pt[j].r <= i+2; ++j)
			{
				if(pt[j].l+2 > pt[j].r) continue;
				ans[pt[j].id] = sum(pt[j].l);
			}
		}

		for(int i = 0; i < m; ++i)
			printf("%d\n",ans[i]);
	}

	return 0;
}

时间: 2024-10-25 07:28:56

【HDOJ 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 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 inte

区间的关系的计数 HDU 4638 离线+树状数组

题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了,23333,反正都是定义右区间的. 这题的关键难度就是如何定义id是连续的呢.我们每次往区间里面放一个数值以后都要add(pos, 1),就是把pos~n的所有的关系都+1.然后如果说在pos之前就出现id-1,就要add(pos[id-1], -1)(同理id+1也是这样),这样子表示从pos[

HDU 5156 - Harry and Christmas tree (dfs序+离线树状数组)

http://acm.hdu.edu.cn/showproblem.php?pid=5156 BC#25的C题. 题意是:给出一颗大小为n的树,以1为根,然后给出m次染色,每次将节点u加上一种颜色(一个节点可以有多个颜色). 最后查询树上每个节点对应子树上包含的不同颜色数量. 当时这场比赛没有做,回来看一下题目,没看标解就试着敲了一遍,于是解题思路从一开始就走上了不归路. 标解是O(n+m)的方法,主要思路是将问题转为:一次染色表示将u到根节点的路径都染上这种颜色. 但这样做需要去重,因为如果u

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

hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球到达某个节点上,如果x==wi,那么球停在这个节点上 .当然,这个点是叶子节点也会停止 . 如果x<wi,那么有1/2的概率走向左子树,有1/2的概率走向右子树 . 如果x>wi,那么有1/8的概率走向左子树,有7/8的概率走向右子树 . 问球经过v节点的概率 .(停在v节点也算) 解法: 在线的话每一个节点建一棵根节点到该节点的线段树,离线的话就先

13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec  Memory Limit: 128 MB Description In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence.

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

SPOJ DQUERY D-query 离线+树状数组

本来是想找个主席树的题目来练一下的,这个题目虽说可以用主席树做,但是用这个方法感觉更加叼炸天 第一次做这种离线方法,所谓离线,就在把所有询问先存贮起来,预处理之后再一个一个操作 像这个题目,每个操作要求区间不同元素的个数,我盲目去查的话,某个元素在之前如果出现了,我把他算在当前区间也不好,算在之前的区间也不好,都会出错. 一个好的方法就是把区间排好序,针对某个区间在树状数组上更新以及查询相应值,这样能准确查出结果,但又不影响之后的查询 具体来说,先把区间按右端点进行排序(我一开始按左端点排,想错

TOJ 4105 Lines Counting(离线树状数组)

4105.   Lines Counting Time Limit: 2.0 Seconds   Memory Limit: 150000K Total Runs: 152   Accepted Runs: 47 On the number axis, there are N lines. The two endpoints L and R of each line are integer. Give you M queries, each query contains two interval

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee