bzoj 3236: [Ahoi2013]作业(缺线段树)

时间:2023-03-10 07:02:24
bzoj 3236: [Ahoi2013]作业(缺线段树)

3236: [Ahoi2013]作业

Time Limit: 100 Sec  Memory Limit: 512 MB
Submit: 1744  Solved: 702
[Submit][Status][Discuss]

Description

bzoj 3236: [Ahoi2013]作业(缺线段树)

Input

bzoj 3236: [Ahoi2013]作业(缺线段树)

Output

bzoj 3236: [Ahoi2013]作业(缺线段树)

Sample Input

3 4
1 2 2
1 2 1 3
1 2 1 1
1 3 1 3
2 3 2 3

Sample Output

2 2
1 1
3 2
2 1

HINT

N=100000,M=1000000

Source

By wangyisong1996加强数据

思路:莫队套权值树状数组。

错因:数组开小了。

莫队套树状数组:

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1000001
using namespace std;
int n,m,S,ans;
int num[MAXN],sum[MAXN],c[][MAXN];
struct nond{
int l,r,a,b;
int id,pos,ans,sum;
}edge[MAXN];
int cmp(nond a,nond b){
if(a.pos==b.pos) return a.r<b.r;
else return a.pos<b.pos;
}
int cmp1(nond a,nond b){
return a.id<b.id;
}
int lowbit(int x){
return x&(-x);
}
void change(int k,int w,int h){
while(k<=n){
c[h][k]+=w;
k+=lowbit(k);
}
}
int query(int k,int h){
int tot=;
while(k){
tot+=c[h][k];
k-=lowbit(k);
}
return tot;
}
void up(int x,int k){
sum[num[x]]+=k;
change(num[x],k,);
if(k==&&sum[num[x]]==) change(num[x],k,);
else if(k==-&&sum[num[x]]==) change(num[x],k,);
}
void mode(){
int l=,r=;
for(int i=;i<=m;i++){
while(l<edge[i].l) up(l++,-);
while(l>edge[i].l) up(--l,);
while(r<edge[i].r) up(++r,);
while(r>edge[i].r) up(r--,-);
edge[i].sum=query(edge[i].b,)-query(edge[i].a-,);
edge[i].ans=query(edge[i].b,)-query(edge[i].a-,);
}
}
int main(){
freopen("ahoi2013_homework.in","r",stdin);
freopen("ahoi2013_homework.out","w",stdout);
scanf("%d%d",&n,&m);
S=sqrt(n);
for(int i=;i<=n;i++)
scanf("%d",&num[i]);
for(int i=;i<=m;i++){
scanf("%d%d%d%d",&edge[i].l,&edge[i].r,&edge[i].a,&edge[i].b);
edge[i].id=i;
edge[i].pos=(edge[i].l-)/S+;
}
sort(edge+,edge++m,cmp);
mode();
sort(edge+,edge++m,cmp1);
for(int i=;i<=m;i++)
cout<<edge[i].ans<<" "<<edge[i].sum<<endl;
}

莫队套线段树: