题目链接

很裸的莫队, 就不多说了...

 #include<bits/stdc++.h>
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, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int maxn = 1e5+;
int vis[maxn];
int a[maxn], ans[maxn]; struct node {
int l, r, block, id;
bool operator < (node x) const
{
if(block == x.block)
return r < x.r;
return block < x.block;
}
} q[maxn];
int main() {
int t, n, m;
cin>>t;
while (t--) {
scanf("%d%d", &n, &m);
int BLOCK = sqrt(n*1.0);
for(int i=; i<=n; i++)
scanf("%d", &a[i]);
for(int i=; i<m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].block = q[i].l/BLOCK;
q[i].id = i;
}
sort(q, q+m);
mem(vis);
int tmp = ;
int x = q[].l, y = q[].r;
for(int i=x; i<=y; i++) {
vis[a[i]] = ;
if (vis[a[i]-] && vis[a[i]+])
tmp--;
else if (!vis[a[i]-] && !vis[a[i]+])
tmp++;
}
ans[q[].id] = tmp;
int L = x, R = y;
for(int i=; i<m; i++) {
x = q[i].l, y = q[i].r;
for (int i=x; i<L; i++) {
vis[a[i]] = ;
if (vis[a[i]-] && vis[a[i]+])
tmp--;
else if (!vis[a[i]-] && !vis[a[i]+])
tmp++;
}
for (int i=R+; i<=y; i++) {
vis[a[i]] = ;
if (vis[a[i]-] && vis[a[i]+])
tmp--;
else if (!vis[a[i]-] && !vis[a[i]+])
tmp++;
}
for (int i=L; i<x; i++) {
vis[a[i]] = ;
if (vis[a[i]-] && vis[a[i]+])
tmp++;
else if (!vis[a[i]-] && !vis[a[i]+])
tmp--;
}
for (int i=y+; i<=R; i++) {
vis[a[i]] = ;
if (vis[a[i]-] && vis[a[i]+])
tmp++;
else if (!vis[a[i]-] && !vis[a[i]+])
tmp--;
}
ans[q[i].id] = tmp;
L = x, R = y;
}
for(int i=; i<m; i++)
printf("%d\n", ans[i]);
}
return ;
}

hdu 4638 Group 莫队算法的更多相关文章

  1. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  2. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  3. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  4. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. HDU 3333 Turing Tree 莫队算法

    题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...

  6. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

  7. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

  8. 【魔改】莫队算法+组合数公式 杭电多校赛4 Problem B. Harvest of Apples

    http://acm.hdu.edu.cn/showproblem.php?pid=6333 莫队算法是一个离线区间分块瞎搞算法,只要满足:1.离线  2.可以O(1)从区间(L,R)更新到(L±1, ...

  9. SPOJ COT2 Count on a tree II 树上莫队算法

    题意: 给出一棵\(n(n \leq 4 \times 10^4)\)个节点的树,每个节点上有个权值,和\(m(m \leq 10^5)\)个询问. 每次询问路径\(u \to v\)上有多少个权值不 ...

随机推荐

  1. Swift学习之十四:闭包(Closures)

    * 闭包(Closures) * 闭包是自包含的功能代码块,可以在代码中使用或者用来作为参数传值. * 在Swift中的闭包与C.OC中的blocks和其它编程语言(如Python)中的lambdas ...

  2. hdu-4468-Spy-KMP+贪心

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4468 题目意思: 给你一个串r,求一个串s,使得s的前缀1+s的前缀2+s的前缀3+...+s的前缀 ...

  3. 2.&与&&以及位运算符。

    这是单独的一块,因为一条讲不清楚(虽然内容也不够一篇),而且我之前也没好好弄清楚,所以有必要写出来. 说位运算符也是从&与&&(|与||类似)之间的区别讲起的.事实上,对于两个 ...

  4. char、varchar和nvarchar的区别

    首先char.varchar和nvarchar.text.ntext都是数据库中的文本数据类型,再区分区分var前缀.n前缀的区别.而text.ntext已经普遍被varchar(MAX)和nvarc ...

  5. JavaScript之获取和设置元素属性

    1.与我前面的随笔获取元素的那些方法不同http://www.cnblogs.com/GreenLeaves/p/5689075.html 获取元素属性的方法getAttribute()不属于docu ...

  6. 指定hive输出格式

    0.11版本以前: sed -e 's/\x01/|/g' file 0.11版本以后: insert overwrite local directory '/opt/aimcpro/libc/tes ...

  7. mysql 正确的关闭方式

    ./bin/mysqladmin -uroot -p123456 -S /home/allen/var/mysql/mysql.sock shutdown

  8. Android webViewj简单处理apk的下载链接

    最近在开发二维码扫一扫的功能,需要分多种情况处理外部的url.遇到一个问题是,一些程序包(比如一些android应用)的下载不好处理.如果不做任何处理的话,webView会打开一个空白页.比如这个链接 ...

  9. 【STM32学习笔记1】基于固件库的STM32_MDK工程模版

    文章包含STM32固件库介绍和工程模板搭建两方面内容. 一.STM32固件库介绍 要建立工程模板,首先要对STM32的固件库有所了解.STM32的固件可以从ST官网下载,网址为:http://www. ...

  10. SSHD配置

    http://blog.licess.com/sshd_config/ LIBPCAP http://blog.csdn.net/htttw/article/details/7521053 Oracl ...