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 ...
随机推荐
- (c#)WinForm遍历所有控件
遍历WinForm中的所有控件,只执行一次以下语句是不行的 foreach (Control ctl in this.Controls) { } 这样只能遍历到第一层控件,子控件是遍历不到的,要想遍历 ...
- c#创建带参数的线程
1.无参数线程的创建 Thread thread = new Thread(new ThreadStart(getpic)); thread.Start(); private void showmes ...
- DOS下导入dmp文件到Oracle数据库
imp usename/password@SID full=y file= d:\data\xxxx.dmp ignore=y imp system/manager file=bible_db lo ...
- Weka回归
第一个数据挖掘技术:回归 例子:给房子定价 房子的价格(因变量)是很多自变量 — 房子的面积.占地的大小.厨房是否有花岗石以及卫生间是否刚重装过等的结果.所以,不管是购买过一个房子还是销售过一个房子, ...
- Ubuntu package offline install
apt-get Use apt-get with the "--print-uris" option to do it. I also add "-qq" so ...
- sencha touch 2.3 结合cordova 环境搭建
sencha touch 2.3环境搭建必备工具 sencha touch 2.3 包sencha cmd 4.0以上JAVA JDK 1.7以上(注意JDK和JRE的区别)Ruby 1.9.3或更早 ...
- MySQL之字符串函数
1. left函数, 对查询字段的字符串内容进行截取,用法select left(content,50) as summary from article; 在这里的意思是只查询content列内容的前 ...
- Linq to sql 操作
1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...
- 关于关注和取消关注的nodejs写法
本例子的关注和取消关注,是通过ajax的方法实现的:nodejs后台写好api接口:响应前台的ajax 先看ajax的代码实现: // 用户关注标签 function subscribe(uid, t ...
- hdu 5115 Dire Wolf(区间dp)
Problem Description Dire wolves, also known as Dark wolves, are extraordinarily large and powerful w ...