博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
阅读量:6388 次
发布时间:2019-06-23

本文共 2987 字,大约阅读时间需要 9 分钟。

 

Group

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1959    Accepted Submission(s): 1006

Problem Description
There are n men ,every man has an ID(1..n).their ID is unique. Whose ID is i and i-1 are friends, Whose ID is i and i+1 are friends. These n men stand in line. Now we select an interval of men to make some group. K men in a group can create K*K value. The value of an interval is sum of these value of groups.
 The people of same group's id must be continuous. Now we chose an interval of men and want to know there should be how many groups so the value of interval is max.
 

 

Input
First line is T indicate the case number.
For each case first line is n, m(1<=n ,m<=100000) indicate there are n men and m query.
Then a line have n number indicate the ID of men from left to right.
Next m line each line has two number L,R(1<=L<=R<=n),mean we want to know the answer of [L,R].
 

 

Output
For every query output a number indicate there should be how many group so that the sum of value is max.

 

 

Sample Input

15 23 1 2 5 41 52 4

Sample Output

12
/*hdu 4638 树状数组 区间内连续区间的个数(尽可能长)给你n个数,让你查询区间[l,r]内最长连续区间的个数。求的是区间长度平方的和,所以区间长度越长越好3 1 2 5 4  在[1,5]上:1,2,3,4,5   1个           在[2,4]上:1,2 和 4    2个首先我们把每个数看成独立的,即每个数赋值为1对于当前出现的a[i],如果前面出现了a[i-1],a[i+1]那么就能组成一队,但是我们是从左往右递推出来的,所以保存的值应尽可能在右边以方便查询,所以如果前面出现了a[i-1],a[i+1].则在它们的位置上-1,删除它们的独立值.hhh-2016-04-04 17:05:44*/#include 
#include
#include
#include
#include
using namespace std;#define lson (i<<1)#define rson ((i<<1)|1)typedef long long ll;const int mod = 10007;const int maxn = 100050;int s[maxn];int a[maxn];int p[maxn];int ans[maxn];struct node{ int l,r; int id;} op[maxn];int n,m;bool cmp(node a,node b){ return a.r < b.r;}int lowbit(int x){ return x&(-x);}void add(int x,int val){ while(x <= n) { s[x] += val; x += lowbit(x); }}int sum(int x){ int cnt = 0; while(x) { cnt += s[x]; x -= lowbit(x); } return cnt;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(s,0,sizeof(s)); for(int i = 1; i <= n; i++) { scanf("%d",&a[i]); p[a[i]] = i; } for(int i =0; i< m; i++) { scanf("%d%d",&op[i].l,&op[i].r); op[i].id = i; } sort(op,op+m,cmp); for(int i = 1,cur = 0; i <= n; i++) { add(i,1); if(a[i] > 1 && p[a[i]-1] < i) add(p[a[i]-1],-1); if(a[i] < n && p[a[i]+1] < i) add(p[a[i]+1],-1); while(i == op[cur].r && cur < m) { ans[op[cur].id] = sum(op[cur].r)-sum(op[cur].l-1); cur++; } } for(int i = 0; i < m; i++) printf("%d\n",ans[i]); } return 0;}

  

 

转载于:https://www.cnblogs.com/Przz/p/5409560.html

你可能感兴趣的文章
HDU Problem 1513 Palindrome 【LCS】
查看>>
针对异常的微信支付开发 坚守两大原则(分享)
查看>>
ExtJs4发送同步请求的store
查看>>
恶意邮件假冒系统安全公告发送病毒,通过个人签名数字证书排除不明邮件干扰...
查看>>
linux内核编译
查看>>
实时股票数据接口 ZT
查看>>
Object-C - 类的定义
查看>>
小程序-动态设置顶部导航条
查看>>
Mybatis通过工具类根据用户名查找用户列表
查看>>
c++ inline 的位置不当导致的 无法解析的外部符号
查看>>
recvfrom WSAEFAULT 10014 的错误记录
查看>>
HLG 1460 Highway Construction【树的直径】
查看>>
hdu 3397 Sequence operation
查看>>
Marketplace Client安装
查看>>
【python】-- 递归函数、高阶函数、嵌套函数、匿名函数
查看>>
Ubuntu打开终端的方法三种
查看>>
php关闭浏览器不终止运行
查看>>
twig的 function 学习
查看>>
robotframework关于变量的引用
查看>>
6.MarkDown语法总结-复杂场景的使用
查看>>