HDU 5654 xiaoxin and his watermelon candy 离线树状数组
Posted Meek
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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
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].
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+10, M = 30005, mod = 1e9+7, 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 = 0; for (; x > 0; x -= lowbit(x)) { s += C[x]; } return s; } int main() { scanf("%d",&T); while(T--) { mp.clear(); memset(H,0,sizeof(H)); memset(p,0,sizeof(p)); memset(b,-1,sizeof(b)); memset(C,0,sizeof(C));memset(nex,0,sizeof(nex)); scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%d",&a[i]), b[i] = -1,nex[i] = 0; int k = 1; for(int i=3;i<=n;i++) { if(a[i]>=a[i-1]&&a[i-1]>=a[i-2]) { if(mp[make_pair(a[i-2],make_pair(a[i-1],a[i]))]) b[i] = mp[make_pair(a[i-2],make_pair(a[i-1],a[i]))]; else b[i] = k,mp[make_pair(a[i-2],make_pair(a[i-1],a[i]))] = k,H[k] = 1, k++; } else b[i] = -1; } for(int i=n;i>=1;i--) if(b[i]!=-1) nex[i]=p[b[i]],p[b[i]]=i; for(int i=1;i<k;i++) add(p[i],1); int q; scanf("%d",&q); for(int i=1;i<=q;i++) { scanf("%d%d",&Q[i].l,&Q[i].r);Q[i].id = i; } sort(Q+1,Q+q+1,cmp1); int l = 1; for(int i=1;i<=q;i++) { while(l<Q[i].l+2) { if(nex[l] ) add(nex[l],1);l++; } if(Q[i].l+2>Q[i].r) Q[i].ans = 0; else Q[i].ans = sum(Q[i].r) - sum(Q[i].l+2-1); } sort(Q+1,Q+q+1,cmp2); for(int i=1;i<=q;i++) { printf("%d\n",Q[i].ans); } } return 0; }
以上是关于HDU 5654 xiaoxin and his watermelon candy 离线树状数组的主要内容,如果未能解决你的问题,请参考以下文章
线段树+离线 hdu5654 xiaoxin and his watermelon candy
HDOJ 5654 xiaoxin and his watermelon candy(离线+树状数组)
HDOJ 5654 xiaoxin and his watermelon candy(离线+树状数组)
dp hdu5653 xiaoxin and his watermelon candy