思路
堆
如果成功的话应该卡掉了所有手写平衡树的人。
这道题询问中位数完全不需要平衡树。
我们直接维护两个堆,一个是所有大于中位数的数的集合,一个是小于中位数的数的集合
代码
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
const int MOD=1e9+7;
const int N=1000000+5;
int n,one[N],two[N],top1,top2,x,y;
int main()
{
freopen("unnormal.in","r",stdin);
// freopen("unnormal.out","w",stdout);
long long a,b,c,sum=1;
scanf("%I64d%I64d%I64d%d",&a,&b,&c,&n);
one[0]=1;top1=1;top2=0;
for (int i=2;i<=n;i++)
{
x=one[0];//作为中位数
y=(int)((a*x+b*i+c)%MOD);
if (y<=one[0])
{
one[top1]=y;
push_heap(one,one+(++top1));
}
else
{
two[top2]=y;
push_heap(two,two+(++top2),greater<int>());
}
if (top1>top2)
{
two[top2]=one[0];
push_heap(two,two+(++top2),greater<int>());
pop_heap(one,one+(top1--));
}
if (top2>top1)
{
one[top1]=two[0];
push_heap(one,one+(++top1));
pop_heap(two,two+(top2--),greater<int>());
}
sum+=y;
}
printf("%I64d\n",sum);
return 0;
}