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 ...
随机推荐
- AndroidUI--SlidingMenu使用例子
SlidingMenu简介: SlidingMenu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.目前有大量的应用都在使用这一效果.如Evernote ...
- jQuery :lt()选择器
定义和用法 :lt() 选择器选取 index 值小于指定数字的元素.(不包含指定值) index 值从 0 开始.(从0开始计数) 最常见的用法:与其他选择器一起使用,选取指定组合中特定序号之前的元 ...
- Java多线程相关知识
1)wait() notify() sleep() sleep是Thread类的函数,wait和notify是Object的函数. sleep的时候keep对象锁,wait的时候release 对 ...
- Collections类方法详解
Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序(Sort)使用sort方法可以根据元素的自然顺序 对 ...
- 判断是否联网_检测网络的类型为3G、2G、wap、wifi
判断是否联网_检测网络的类型为3G.2G.wap.wifi 判断是否联网: /*** * judge Internet is available * * @author wei-spring * @ ...
- Sublime_调试PHP编译系统设置.
工具->编译系统->新建编译系统 { "cmd": ["C:\\wampserver\\bin\\php\\php5.3.13\\php.ex ...
- Linux 下搭建ftp服务器 指定用户指定目录及其他操作
搭建 Linux下 rpm -qa |grep vsftpd查看是否安装 没安装yum安装 /etc/vsftpd/目录下有vsftpd.conf配置文件 根据需求 进行配置 是否使用匿名用户以及文 ...
- Delphi2007下CIS的clHttp使用
Delphi组件Clever Suite Internet是一款优秀的网络组件,唯一让我感觉不足的是ClHttp竟然使用了断言,当程序遇到问题的时候就会弹出一个对话框,并显示问题是出在了那个单元里.好 ...
- fragment中listview触发事件setOnItemClickListener不好使
<listView/>中// listview点击 ,高度wrap_content改成fill_prarent
- Hessian Servlet和Hessian Spring的简单应用
转自: http://lancui.iteye.com/blog/935578 简介 相比WebService,Hessian更简单.快捷.采用的是二进制RPC协议(Binary),因为采用的是二进制 ...