# Hdu 4638 Group(莫队算法)+对拍教程

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

#include<queue>
#include<math.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define mid ((l+r)>>1)
#define clr(x,v) memset(x,v,sizeof(x))
typedef long long ll;
const int N=1e5+7;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
int t,n,m,unit,temp;
int a[N],ans[N],book[N];
struct node {
int l,r,id;
friend bool operator <(node a,node b) {
if(a.l/unit==b.l/unit) {
/*    if((a.l/unit)&1) {
return a.r<b.r;
}
else {
return a.r>b.r;
}*/
return a.r<b.r;
}
else {
return a.l/unit<b.l/unit;
}
}
} query[N];
void add(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp++;
}
if(book[x-1]==1&&book[x+1]==1) {
temp--;
}
book[x]=1;
}
void del(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp--;
}
if(book[x-1]==1&&book[x+1]==1) {
temp++;
}
book[x]=0;
}
int main() {
//ios::sync_with_stdio(0);
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
unit=(int)sqrt((double)n);
for(int i=1; i<=m; i++) {
scanf("%d %d",&query[i].l,&query[i].r);
query[i].id=i;
}
sort(query+1,query+m+1);
int tl=1,tr=0;
temp=0;
clr(book,0);
for(int i=1; i<=m; i++) {
while(tr<query[i].r)
add(a[++tr]);
while(tr>query[i].r)
del(a[tr--]);
while(tl<query[i].l)
del(a[tl++]);
while(tl>query[i].l)
add(a[--tl]);
ans[query[i].id]=temp;
}
for(int i=1; i<=m; i++) {
printf("%d\n",ans[i]);
}
}
return 0;
}


#include<queue>
#include<math.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;
#define mid ((l+r)>>1)
#define clr(x,v) memset(x,v,sizeof(x))
typedef long long ll;
const int N=1e5+7;
const int inf=0x3f3f3f3f;
const double eps=1e-6;
int t,n,m,unit,temp;
int a[N],ans[N],book[N],belong[N];
struct node {
int l,r,id;
friend bool operator <(node a,node b) {
if(a.l/unit==b.l/unit) {
if((a.l/unit)&1) {
return a.r<b.r;
} else {
return a.r>b.r;
}
} else {
return a.l/unit<b.l/unit;
}
}
} query[N];
void add(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp++;
}
if(book[x-1]==1&&book[x+1]==1) {
temp--;
}
book[x]++;
}
void del(int x) {
if(book[x-1]==0&&book[x+1]==0) {
temp--;
}
if(book[x-1]==1&&book[x+1]==1) {
temp++;
}
book[x]--;
}
int main() {
//ios::sync_with_stdio(0);
scanf("%d",&t);
while(t--) {
scanf("%d %d",&n,&m);
for(int i=1; i<=n; i++) {
scanf("%d",&a[i]);
}
unit=(int)sqrt((double)n);
for(int i=1; i<=m; i++) {
scanf("%d %d",&query[i].l,&query[i].r);
query[i].id=i;
}
sort(query+1,query+m+1);
int tl=1,tr=0;
temp=0;
clr(book,0);
for(int i=1; i<=m; i++) {
while(tr<query[i].r) {
add(a[++tr]);
//    printf("add:%d\n",tr);
}
while(tr>query[i].r) {
del(a[tr--]);
//    printf("del:%d\n",tr+1);
}
while(tl<query[i].l) {
del(a[tl++]);
//    printf("del:%d\n",tl-1);
}
while(tl>query[i].l) {
add(a[--tl]);
//    printf("add:%d\n",tl);
}
ans[query[i].id]=temp;
}
for(int i=1; i<=m; i++) {
printf("%d\n",ans[i]);
}
}
return 0;
}


1.生成数据插入data。in
2.准备正解（或者暴力解法）和待验证解法

3.新建txt，改为。bat

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客