hdu 4638 Group(离线+树状数组)
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 546 Accepted Submission(s): 299
2
#include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
#define lowbit(x) (x&(-x))
const int N=;
int C[N],n; void add(int x,int inc){
while(x<=n){
C[x]+=inc;
x+=lowbit(x);
}
}
int sum(int x){
int res=;
while(x){
res+=C[x];
x-=lowbit(x);
}
return res;
} int a[N];
struct node
{
int l,r,id,ans;
}query[N];
struct ppp
{
int x[],cc;
void init()
{
memset(x,,sizeof(x));
cc=;
}
}b[N];//b[i]用来储存与i相邻的数的下标且 这个相邻的数在i之前!!
bool cmpR(node a,node b)
{
return a.r<b.r;
}
bool cmpID(node a,node b)
{
return a.id<b.id;
}
int main()
{
int i,m,T;
scanf("%d",&T);
while(T--)
{ memset(C,,sizeof(C));
for(i=;i<N;i++)b[i].init();
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)scanf("%d",&a[i]);
for(i=;i<=m;i++)scanf("%d%d",&query[i].l,&query[i].r),query[i].id=i;
sort(query+,query+m+,cmpR);
int cnt=;
for(i=;i<=n;i++)
{
int tmp=a[i];
if(b[tmp].x[])add(b[tmp].x[],-);//将之前出现过与之相邻的数减1,单点更新!!
if(b[tmp].x[])add(b[tmp].x[],-);
if(tmp->=)
{
b[tmp-].x[++b[tmp-].cc]=i;
}
if(tmp+<=n)
{
b[tmp+].x[++b[tmp+].cc]=i;
}
add(i,);
while(i==query[cnt].r)
{
query[cnt].ans=sum(query[cnt].r)-sum(query[cnt].l-);
cnt++;
}
}
sort(query+,query+m+,cmpID);
for(i=;i<=m;i++)printf("%d\n",query[i].ans);
}
}
hdu 4638 Group(离线+树状数组)的更多相关文章
- HDU 4638 group(离线+树状数组)
此题实质上是询问每段区间[l,r]内的数字能组成多少段连续的数字. 不大好用线段树合并区间来写. 考虑离线,类似于HH的项链的话,对询问按右端点排序,对于右端点的每一次右移,则对于询问,实际上是求的后 ...
- HDU 4638 Group 【树状数组,分块乱搞(莫队算法?)】
根据题目意思,很容易得出,一个区间里面连续的段数即为最少的group数. 题解上面给的是用树状数组维护的. 询问一个区间的时候,可以一个一个的向里面添加,只需要判断a[i]-1 和 a[i]+1是否已 ...
- 区间的关系的计数 HDU 4638 离线+树状数组
题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...
- hdu 4605 Magic Ball Game (在线主席树/离线树状数组)
版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 4605 题意: 有一颗树,根节点为1,每一个节点要么有两个子节点,要么没有,每个节点都有一个权值wi .然后,有一个球,附带值x . 球 ...
- HDU 2852 KiKi's K-Number(离线+树状数组)
题目链接 省赛训练赛上一题,貌似不难啊.当初,没做出.离线+树状数组+二分. #include <cstdio> #include <cstring> #include < ...
- HDU - 4777 离线树状数组
离线树状数组搞一搞. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #de ...
- HDU 4417 离线+树状数组
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- POJ 3416 Crossing --离线+树状数组
题意: 给一些平面上的点,然后给一些查询(x,y),即以(x,y)为原点建立坐标系,一个人拿走第I,III象限的点,另一个人拿II,IV象限的,点不会在任何一个查询的坐标轴上,问每次两人的点数差为多少 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
随机推荐
- 错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误
WIN7 64位旗舰版安装OFFICE2003 提示:“错误1919,配置ODBC数据源MS Access Database时发生错误ODEC错误” 在64位系统上,32位软件的注册表的信息不是直接在 ...
- Using FileUpload
Using FileUpload FileUpload can be used in a number of different ways, depending upon the requiremen ...
- m3u8直播测试地址
调试m3u8的时候需要测试地址 找了几个,备用一下 安徽卫视 http://stream2.ahtv.cn/ahws/cd/live.m3u8经济生活 http://stream2.ahtv.cn/j ...
- freetype相关总结
FreeType-2.7 API : https://www.freetype.org/freetype2/docs/reference/ft2-basic_types.html ---------- ...
- leetcode 234 回文链表 Palindrome Linked List
要求用O(n)时间,和O(1)空间,因此思路是用本身链表进行判断,既然考虑回文,本方法思想是先遍历一次求链表长度,然后翻转前半部分链表:然后同时对前半部分链表和后半部分链表遍历,来判断对应节点的值是否 ...
- ubuntu用mentohust连接ruijie
32位 http://download.csdn.net/detail/yan456jie/8720395 64位 http://download.csdn.net/detail/yan456jie ...
- 网站模板-AdminLTE:AdminLTE
ylbtech-网站模板-AdminLTE:AdminLTE 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 1. https://adminlte.io/ 1. ...
- java dwg转svg
package com.example.demo.dxf2svg; import com.aspose.cad.InterpolationMode; import com.aspose.cad.Smo ...
- 浅谈防火墙对FTP的影响及故障排除
本文转载自:http://www.cnblogs.com/emanlee/archive/2013/01/07/2849680.html 向作者致敬! TP是常见的基于TCP的网络服务,它使用了两个 ...
- Pycharm激活方法(license server方法)
pycharm所有版本 http://www.jetbrains.com/pycharm/download/previous.html 打开激活窗口 选择 Activate new license w ...