hdu 5654 xiaoxin and his watermelon candy 莫队
题目链接
求给出的区间中有多少个三元组满足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 莫队的更多相关文章
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组 区间不同数的个数
xiaoxin and his watermelon candy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5654 Description Du ...
- HDU 5654 xiaoxin and his watermelon candy 离线树状数组
xiaoxin and his watermelon candy Problem Description During his six grade summer vacation, xiaoxin g ...
- 数据结构(主席树):HDU 5654 xiaoxin and his watermelon candy
Problem Description During his six grade summer vacation, xiaoxin got lots of watermelon candies fro ...
- hdu 5654 xiaoxin and his watermelon candy 树状数组维护区间唯一元组
题目链接 题意:序列长度为n(1<= n <= 200,000)的序列,有Q(<=200,000)次区间查询,问区间[l,r]中有多少个不同的连续递增的三元组. 思路:连续三元组-& ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- hdu 4358 Boring counting 离散化+dfs序+莫队算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4358 题意:以1为根节点含有N(N <= 1e5)个结点的树,每个节点有一个权值(weight ...
- 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)).有公 ...
- 线段树+离线 hdu5654 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...
- dp hdu5653 xiaoxin and his watermelon candy
传送门:点击打开链接 题意:有n个箱子排成一排,有m个炸弹.位置告诉你.如今炸弹的左边伤害和右边伤害能够自己控制,要求 每一个炸弹炸的箱子数的累乘,输出答案取log2并乘以1e6 思路:直接2for ...
随机推荐
- SQL Server 已提交读快照 测试
1. 打开数据库 已提交读快照 选项 2. 数据库 已提交读快照 模式下的测试 a) 测试表 Test b) 开启事务1,更新数据C2 = '200'(未提交) BEGIN TRAN ' WHERE ...
- Win32汇编开始 Hello Asm
今天开始学习Win32汇编 因为自己很多都是Windows方面 所以 接触一下Win32汇编 . ;.386指令集 .model flat,stdcall ;工作模式 option casemap:n ...
- python基础教程
转自:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html Python快速教程 作者:Vamei 出处:http://www.cn ...
- [Python] 应用kNN算法预测豆瓣电影用户的性别
应用kNN算法预测豆瓣电影用户的性别 摘要 本文认为不同性别的人偏好的电影类型会有所不同,因此进行了此实验.利用较为活跃的274位豆瓣用户最近观看的100部电影,对其类型进行统计,以得到的37种电影类 ...
- python---连接MySQL第五页
Connector/Python Connection Arguments A connection with the MySQL server can be established using ei ...
- xml 解析 Xstream
1.概述 json 解析可以使用gson包解为现成的对象,那么xml可以不可以用现成的包解析成java的对象呢? 带着这个问题,查询了一些材料找到了Xstream解析方法,确实可以完成这种思考,只是实 ...
- C#的DLL注册为COM,Delphi来调用
非常实用的东西!过去知道这个方法的话可以解决多少问题啊 首先建立一个C#的DLL工程,写一个类 //Test.csnamespace Test...{public class MyTest...{pu ...
- docker 导入下载模板
<pre name="code" class="ruby">docker:/root# cat centos-6-x86.tar.gz | dock ...
- 2014第13周四Webservice概念问题记
晚上回来看网页学习了这两天一直疑惑的两个问题: 1.REST和SOAP架构下的Webservice的区别? 2.axis2和CXF的区别. 大部分是理论,暂时摘录一下,以后有更多实践后再回顾. 一.R ...
- Go语言Eclipse开发环境配置-Windows
1.首先安装eclipse,选择一个适合的版本就好,解压即可 http://www.eclipse.org/downloads/ 2.下载go语言安装包 官网地址 :http://www.golang ...