HDU 5654 xiaoxin and his watermelon candy 归并树
2016-03-27 14:23
288 查看
xiaoxin and his watermelon candy
Time Limit: 4000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 161 Accepted Submission(s): 38
[align=left]Problem Description[/align]
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
[align=left]Input[/align]
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].
[align=left]Output[/align]
For each query, print an integer which represents the number of ways xiaoxin can choose a triplet.
[align=left]Sample Input[/align]
1 5 1 2 3 4 5 3 1 3 1 4 1 5
[align=left]Sample Output[/align]
1 2 3
[align=left]Source[/align]
BestCoder Round #77 (div.1)
参见官方题解。
处理以每个位置开始的满足要求的三元组,a[i],a[i+1],a[i+2],(用pair<int,pair<int,int> >储存)
然后对每个三元组map一下,用pre[i]表示和第i个三元组同的三元组前一次出现的位置,如果全面没出现过就pre[i]=0,如果不满足三元组要求的是pre[i]=n。
这样对于查询[L,R],,只需要计算区间[L,R-2]里pre值小于L的个数就是答案。
用归并树维护查询即可。
/**************** *PID:hdu5654 *Auth:Jonariguez ***************** */ #define lson k*2,l,m #define rson k*2+1,m+1,r #define rep(i,s,e) for(i=(s);i<=(e);i++) #define For(j,s,e) For(j=(s);j<(e);j++) #define sc(x) scanf("%d",&x) #define In(x) scanf("%I64d",&x) #define pf(x) printf("%d",x) #define pfn(x) printf("%d\n",(x)) #define Pf(x) printf("%I64d",(x)) #define Pfn(x) printf("%I64d\n",(x)) #define Pc printf(" ") #define PY puts("YES") #define PN puts("NO") #include <stdio.h> #include <string.h> #include <string> #include <math.h> #include <set> #include <map> #include <stack> #include <queue> #include <vector> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; typedef int Ll; Ll quick_pow(Ll a,Ll b,Ll MOD){a%=MOD;Ll res=1;while(b){if(b&1)res=(res*a)%MOD;b/=2;a=(a*a)%MOD;}return res;} const int maxn=200000+10; int pre[maxn],a[maxn]; pair<int,pair<int,int> > pp; map<pair<int,pair<int,int> >,int> mp; struct Tree{ vector<int> sum[maxn*4]; void pushUp(int k,int l,int r){ int lc=k*2,rc=k*2+1,m=(l+r)/2; sum[k].clear(); sum[k].resize(r-l+1); merge(sum[lc].begin(),sum[lc].end(),sum[rc].begin(),sum[rc].end(),sum[k].begin()); } void build(int k,int l,int r){ if(l==r){ sum[k].clear(); sum[k].push_back(pre[l]); return ; } int m=(l+r)/2; build(lson); build(rson); pushUp(k,l,r); } int ask(int a,int b,int v,int k,int l,int r){ if(a<=l && r<=b) return upper_bound(sum[k].begin(),sum[k].end(),v)-sum[k].begin(); int m=(l+r)/2,res=0; if(a<=m) res+=ask(a,b,v,lson); if(b>m) res+=ask(a,b,v,rson); return res; } }tree; int main() { int i,j,n,m,T; scanf("%d",&T); while(T--){ scanf("%d",&n); mp.clear(); for(i=1;i<=n;i++) sc(a[i]); if(n<3){ scanf("%d",&m); while(m--){ int x,y; sc(x);sc(y); puts("0"); } continue; } for(i=1;i<=n-2;i++){ if(!(a[i]<=a[i+1] && a[i+1]<=a[i+2])) pre[i]=n; else { pp=make_pair(a[i],make_pair(a[i+1],a[i+2])); if(mp[pp]!=0) pre[i]=mp[pp]; else pre[i]=0; mp[pp]=i; } } pre[n-1]=pre =n; tree.build(1,1,n); scanf("%d",&m); while(m--){ int x,y; scanf("%d%d",&x,&y); if(y-x<2) puts("0"); else printf("%d\n",tree.ask(x,y-2,x-1,1,1,n)); } } return 0; }
相关文章推荐
- 阿里2016实习offer五面经验与总结
- PHP网络编程
- HDU ACM 1392 Surround the Trees->凸包
- SQL Server 2012 实现分页新语法
- R语言的一些矩阵运算
- P1174 互素
- 2016sdau课程练习专题一 1008 problemI
- Android AutoLayout全新的适配方式 堪称适配终结者
- BestCoder Round #77 (div.2)
- EasyUI表单验证
- 免费开源!仿微信仿陌陌类APP源代码整项目开源,包括ADT项..
- 2016/3/27 - 记
- SQl优化问题4
- 实验五分析system_call中断处理过程
- P1001 第K极值
- 浅析VO、DTO、DO、PO的概念、区别和用处
- 全部开源!仿微信仿陌陌类APP整项目开源,客户端+服务端
- 面试题的那些事(1)
- java事务学习笔记(六)--使用动态代理(Dynamic Proxy)完成事务
- SQl优化问题3