hdu 4638 Group(离线+树状数组)
2024-08-30 21:49:32
Group
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 546 Accepted Submission(s): 299
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
1
5 2
3 1 2 5 4
1 5
2 4
Sample Output
1
2
2
Source
Recommend
zhuyuanchen520
队友的思路!!
先按区间右端点r排升序!!
下面是操作:(若之前有相邻的,就把那位上减1,相当于把之前的组归到当前的组!!!)
如 3 1 2 5 4
3 in: 1
1 in: 1 1
2 in: 1-1=0 1-1=0 1
5 in: 0 0 1 1
4 in: 0-1=-1 0 1 1-1=0 1
然后再区间求和即可!!!
#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);
}
}
最新文章
- win8改win7笔记
- sqlmap用户手册 | WooYun知识库
- Dynamics AX 2012 R2 耗尽用户
- OC基础数据类型-NSSet
- 博创arm板编译内核makefile不兼容问题解决
- (六)C#中判断空字符串的三种方法性能分析
- makefile实例(3)-多个文件实例优化
- 解决编译报错:Unable to copy file, because it is being used by another process.
- POJ 1698 (二分图的多重匹配)
- 用户在浏览器中输入一个url发生的奥秘
- 上海启动5G试用!104页PPT,为你深度解析5G终端的创新和机遇
- java.lang.AbstractMethodError: org.mybatis.spring.transaction.SpringManagedTransaction.getTimeout()Ljava/lang/Integer; 	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.jav
- FPGA 主时钟约束---primary clocks
- 小记 ArchLinux 下 Typora 无法输入中文
- 微信小程序点击 navigator ,页面不跳转
- ASP.NET MVC中如何实现页面跳转
- MySQl资料链接
- 0java之泛型解说
- alibaba/fescar 阿里巴巴 开源 分布式事务中间件
- Beta 冲刺1