这题我们可以用权值线段树来做。
题外话:这题50分的应该都是AC的吧???
我们先求出a数组的前缀和数组c (记得模P)。
然后每次做完当前状态i后将c[i]加入到权值线段树里。
因为答案要求尽可能小,所以我们对于以当前点为右端点的答案为:
a[i] - a[i]-K的前驱
或
a[i]+p - a[i]+P-K的前驱
然后不断更新答案即可。
上标:
#include<cstdio>
#define N 100010
#define ma 100000000
#define E (1<<30)
using namespace std;
int ls[4000010],rs[4000010],mi[4000010];
int n,K,P,a[N],s,ans=E,tot=1,x1;
inline int read()
{
int x=0; char c=getchar();
while (c<'0' || c>'9') c=getchar();
while (c>='0' && c<='9') x=(x<<1)+(x