HDU-4638 Group 树状数组+离线
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4638
个人认为比较不错的题目。
题意:给一个1-n的排列,询问区间[l,r]的数排序后连续区间的个数。
对于这种题目容易想到对询问离线处理,难点是怎样在logn的时间内求出连续区间的个数。先对询问按右端点y从左到右排序,然后从左到右扫描整个数列,现在考虑加一个数num进去,如果我们前面存在num-1或者num+1,那么数列连续区间的个数是没有增加的,可能还会减少。因此我们可以维护一个数组数组或者线段树,对于每加进去的一个数num[i],对 i 点加1,如果 i 点前存在num[i]-1和num[i]+1,那么分别对他们所在的点-1。如果遇到询问,就求和就可以了。。。
//STATUS:C++_AC_484MS_3364KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Node{
int x,y,id;
bool operator <(const Node& a)const {
return y<a.y;
}
}nod[N]; int num[N],sum[N],ans[N],vis[N],w[N];
int T,n,m; int lowbit(int x)
{
return x&(-x);
} void update(int x,int val)
{
while(x<=n){
sum[x]+=val;
x+=lowbit(x);
}
} int getsum(int x)
{
int ret=;
while(x){
ret+=sum[x];
x-=lowbit(x);
}
return ret;
} int main(){
// freopen("in.txt","r",stdin);
int i,j,k;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
scanf("%d",&num[i]);
w[num[i]]=i;
}
for(i=;i<m;i++){
scanf("%d%d",&nod[i].x,&nod[i].y);
nod[i].id=i;
}
sort(nod,nod+m);
mem(sum,);mem(vis,);
for(i=,k=;i<=n;i++){
update(i,);
if(vis[num[i]-])update(w[num[i]-],-);
if(vis[num[i]+])update(w[num[i]+],-);
vis[num[i]]=;
for(;nod[k].y==i && k<m;k++){
ans[nod[k].id]=getsum(nod[k].y)-getsum(nod[k].x-);
}
if(k==m)break;
} for(i=;i<m;i++){
printf("%d\n",ans[i]);
}
}
return ;
}
HDU-4638 Group 树状数组+离线的更多相关文章
- HDU 4638 Group 树状数组 + 思路
实际上就是问这个区间编号连续的段的个数,假如一个编号连续的段有(a+b)个人,我把他们分在同一组能得到的分值为(a+b)^2,而把他们分成人数为a和b的两组的话,得到的分值就是a^2+b^2,显然(a ...
- HDU 4638 Group ★(树状数组)
题意 询问一段区间里的数能组成多少段连续的数. 思路 先考虑从左往右一个数一个数添加,考虑当前添加了i - 1个数的答案是x,那么可以看出添加完i个数后的答案是根据a[i]-1和a[i]+1是否已经添 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- SPOJ DQUERY树状数组离线or主席树
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- D-query SPOJ 树状数组+离线
D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...
- HDU 4638 Group (线段树 | 树状数组 + 离线处理)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
随机推荐
- uva 10034
计算所有点之间的权值 然后就是最小生成树 #include<cstring> #include<string> #include<cstdio> #includ ...
- 【leetcode】Word Ladder (hard) ★
Given two words (start and end), and a dictionary, find the length of shortest transformation sequen ...
- struts2 权限拦截器 拦截没有登陆的请求
假设有这样的登陆: ActionContext.getContext().getSession().put("UserMsg", userMsg); 则可以这样判断是否登陆: im ...
- WPF之小动画二
上一篇文章简单介绍了动画的定义方法和一些控制动画的方法,并没有涉及复杂属性的动画处理方式,本文将继续动画的其它方面的使用. 写在前面(对于一些动画操作时候的建议): 1.如果希望某个元素从显示到消失, ...
- 再谈 retain,copy,mutableCopy(官方SDK,声明NSString都用copy非retain)
之前一直以为retain就是简单的计数器+1,copy就是重新开辟内存复制对象: 其实不是这样,原来之前的自己独自徘徊于糊涂之中. (官方SDK,对NSString属性的定义都是用copy,而不是re ...
- python学习笔记二--列表的使用
一.基本列表操作 1. 合并‘+’:左右两边必须均为列表 可以用str(),%,list()做类型的转换后再做合并 2. 重复‘*’: 3. 迭代和解析: x作为for循环里步进变量,由于列表是序列, ...
- umount nfs状态为busy的处理方法
umountall状态为busy的系统: 以下四种情况,文件系统都会显示:umount: file_system_name busy 1 A program is accessing a file o ...
- CCS使用TIPS
2013-06-20 09:37:49 CCS使用TIPS: 代码编写: CCS中通过Using CodeSense方便写代码,跟VC助手类似,具体使用方法在ccs的help中搜索using visu ...
- 函数page_get_space_id
#define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID 34 /****************************************************** ...
- postgreSQL数据类型转换字符串和数值
1.将数值转成字符串类型 方法1:调用to_char(int, text)函数,int为要转换值,text为数值格式化模式,其中模式描述为: 模式 描述 9 带有指定数值位数的值 0 带前导零的值 ...