HDU 5654 xiaoxin and his watermelon candy
来源:互联网 发布:淘宝评价规则 编辑:程序博客网 时间:2024/05/20 00:35
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
1.
2.
Your task is to calculate how many different ways xiaoxin can choose a triplet in range [L, R]?
two triplets
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 integern(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 integerQ(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
The third line is an integer
Output
For each query, print an integer which represents the number of ways xiaoxin can choose a triplet.
Sample Input
151 2 3 4 531 31 41 5
Sample Output
123在线要可持久化线段树,离线直接树状数组就可以。
#include<cstdio>#include<vector>#include<cmath>#include<queue>#include<string>#include<map>#include<iostream>#include<cstring>#include<algorithm>using namespace std;typedef long long LL;const int low(int x){ return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int maxn = 2e5 + 10;int T, n, m;int a[maxn], f[maxn], ans[maxn], tot, p[maxn];struct point{int l, r, id;void read(int x){ scanf("%d%d", &l, &r); id = x; }bool operator<(const 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){}}q[maxn];void add(int x, int y){for (int i = x; i <= n; i += low(i)) f[i] += y;}int sum(int x){int ans = 0;for (int i = x; i; i -= low(i)) ans += f[i];return ans;}int main(){scanf("%d", &T);while (T--){scanf("%d", &n);for (int i = 1; i <= n; i++) scanf("%d", &a[i]), f[i] = 0;scanf("%d", &m);for (int i = 0; i < m; i++) q[i].read(i), ans[i] = 0;sort(q, q + m);map<point, int> M; tot = 0;for (int i = 1, j = 0; i <= n; i++){if (i + 2 <= n&&a[i] <= a[i + 1] && a[i + 1] <= a[i + 2]){if (!M[point(a[i], a[i + 1], a[i + 2])]){M[point(a[i], a[i + 1], a[i + 2])] = ++tot;p[tot] = 0;}int x = M[point(a[i], a[i + 1], a[i + 2])];add(p[x] + 1, 1); add(i + 1, -1); p[x] = i;}for (; j < m&&q[j].r <= i + 2; j++){if (q[j].r - q[j].l < 2) continue;ans[q[j].id] = sum(q[j].l);}}for (int i = 0; i < m; i++) printf("%d\n", ans[i]);}return 0;}
0 0
- HDU 5654 xiaoxin and his watermelon candy
- HDU 5654 xiaoxin and his watermelon candy
- HDU 5654 xiaoxin and his watermelon candy 归并树
- dp hdu5653 xiaoxin and his watermelon candy
- HDU5654 xiaoxin and his watermelon candy 莫队
- 【没A】【HDU 5654】 xiaoxin and his watermelon candy|主席树
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
- [hdu5654 xiaoxin and his watermelon candy]区间内不同数的个数
- 【HDU5654 BestCoder Round 77 (div1) D】【前驱位置思想 排序 树状数组】xiaoxin and his watermelon candy 区间内多少个不同连续单升三元
- HDU - 5410 CRB and His Birthday
- HDU 5410 CRB and His Birthday
- hdu acm 5410 CRB and His Birthday
- HDU 5410 CRB and His Birthday
- CRB and His Birthday(HDU-5410)
- hdu 6222 Heron and His Triangle
- HDU 5410 CRB and His Birthday
- HDU 5410 CRB and His Birthday(DP)
- HDU 5651 xiaoxin juju needs help
- The type javax.servlet.ServletRequest cannot be resolved. It is indirectly referenced from required
- 计算机网络协议层
- 兰州出差记之第一篇
- 变量定义与const变量定义
- HDU 5654 xiaoxin and his watermelon candy
- Laravel学习笔记-1-环境搭建
- 如何系统地自学一门Python 语言?https://mp.weixin.qq.com/s?__biz=MzA3ODY0MzEyMA==&mid=408122444&idx=2&sn=ae6f3ddd
- CSS学习(二十)-flexbox模型
- 方法,传真和传引用
- Spring学习之一基础篇
- C语言库函数三
- 成都麻将
- 阿里云CentOS 7.1编译安装MySql5.6.24