题目链接

求给出的区间中有多少个三元组满足i+1=j=k-1 && a[i]<=a[j]<=a[k] 如果两个三元组的a[i], a[j], a[k]都相等, 那么这两个三元组算一个。

预处理一下所有三元组, 然后跑莫队就水过去了...

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 2e5+5;
struct node
{
int l, r, id, block;
bool operator < (node a) const
{
if(block == a.block)
return r<a.r;
return block<a.block;
}
}q[maxn];
int a[maxn], b[maxn];
int n, m, ans[maxn], num[maxn];
map <pair<pll, int>, int> mp;
void solve() {
cin>>n;
mem(num);
mem(b);
int block = sqrt(n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
cin>>m;
for(int i = 0; i < m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].l++, q[i].r--;
q[i].block = q[i].l/block;
q[i].id = i;
}
sort(q, q+m);
int cnt = 0;
mp.clear();
for(int i = 2; i <= n-1; i++) {
if(a[i]>=a[i-1] && a[i]<=a[i+1]) {
if(!mp[mk(mk(a[i-1], a[i]), a[i+1])]) {
mp[mk(mk(a[i-1], a[i]), a[i+1])] = ++cnt;
}
b[i] = mp[mk(mk(a[i-1], a[i]), a[i+1])];
}
}
int L = 1, R = L-1, tot = 0;
for(int i = 0; i < m; i++) {
if(q[i].l>q[i].r) {
ans[q[i].id] = 0;
continue;
}
while(L<q[i].l) {
num[b[L]]--;
if(num[b[L]]==0&&b[L]!=0)
tot--;
L++;
}
while(L>q[i].l) {
L--;
num[b[L]]++;
if(num[b[L]]==1&&b[L]!=0)
tot++;
}
while(R>q[i].r) {
num[b[R]]--;
if(num[b[R]]==0&&b[R]!=0)
tot--;
R--;
}
while(R<q[i].r) {
R++;
num[b[R]]++;
if(num[b[R]]==1&&b[R]!=0)
tot++;
}
ans[q[i].id] = tot;
}
for(int i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
}
int main()
{
int t;
cin>>t;
while(t--) {
solve();
}
return 0;
}

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. HDU 5654 xiaoxin and his watermelon candy 离线树状数组

    xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...

  3. 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy

    Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...

  4. hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组

    题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...

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

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  6. hdu 4358 Boring counting 离散化+dfs序+莫队算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...

  7. HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)

    题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...

  8. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

  9. dp hdu5653 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:有n个箱子排成一排,有m个炸弹.位置告诉你.如今炸弹的左边伤害和右边伤害能够自己控制,要求 每一个炸弹炸的箱子数的累乘,输出答案取log2并乘以1e6 思路:直接2for ...

随机推荐

  1. phpstorm 设置多项目并存

    phpstorm 或 webstorm 设置多个项目可以并存: File -> settings -> Directories -> Add Content Root 中添加你当前的 ...

  2. 5.7.2.2 min()和max()方法

    Math对象还包含许多方法,用于辅助完成简单和复杂的数学计算. 其中,min()和max()方法用于确定一组数值中的最小值和最大值.这两个方法都可以接受任意多个数值参数,如下例子: var max = ...

  3. 【原】从一个bug浅谈YUI3组件的资源加载

    篇前声明:为了不涉及业务细节,篇内信息统一以某游戏,某功能代替 前不久,某游戏准备内测客户端,开发人员测试过程中发现某功能突然不灵了,之前的测试一切ok,没有发现任何异常,第一反应是,游戏内浏览器都是 ...

  4. php设计模式之:观察者模式

    转载自php面向对象设计模式 之 观察者模式 问题 假如一个小贩, 他把产品的价格提升了, 不同的消费者会对此产生不同的反应.一般的编程模式无非是获取提升的价格,然后获取所有的消费者,再循环每个消费者 ...

  5. nginx,wsgi,flask之间的关系

    之前看写flask 应用的一些疑问,百度上的答案解释的不错,这里记着以后可以看看Web 服务器层对于传统的客户端 - 服务器架构,客户端向服务器发送请求,服务器接收请求,处理请求,最后给客户端返回请求 ...

  6. 又是玻璃效果?调用一句代码就OK了

    原文 http://www.cnblogs.com/lan-mei/archive/2012/05/11/2495740.html 最近自学WPF,网上一查资料,全是依赖属性,路由事件,动画效果等等. ...

  7. centos6.5 Eclipse C/C++开发环境及项目创建测试

    1,新建C++ project

  8. 量身定制顺美男女西服、衬衫、大衣、T恤等 - 北京58同城

    量身定制顺美男女西服.衬衫.大衣.T恤等 - 北京58同城 量身定制顺美男女西服.衬衫.大衣.T恤等 发布时间:2014-04-11浏览2次

  9. C#中dynamic的正确用法【转】

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  10. Dreamweaver PHP代码护眼配色方案

    结果展示 [1]主菜单选择编辑->首选项.在分类中选择"字体".设置代码视图的字体为Courier New [2]在分类中选择 "代码颜色",点击 &qu ...