God is the summer solstice festival lowered the oracle problem solution

  Let me talk about an aside, anyone can see at first glance became famous up a station's main summer festival?

Questions face

the summer solstice festival is a welcome ancestral spirits at the same time pray for the return of the summer harvest celebration. Men of the village will be in Canton
with military war court Winter summer performances Army, the military would overthrow the summer and winter of Army generals winter male, and then set fire to his
burned car with Hill.
Rochelle N Kyrgyz village have already selected individuals to participate in the show, some of them in charge of the army played summer, another
some people in charge of the army played in winter. Due to the large number of Rochelle N Kyrgyz we want this individual into several contiguous
segments. In order to ensure the smooth progress of the show, the absolute value of the difference between the number of army troops summer and winter the number of each piece does not
exceed K.
Rochelle Kyrgyz wondering qualifying division scheme how many. Because there are many qualified plan,
you count as long as the output of the program is divided by the 1e9 + 7.
[Input format
of the first row two integers N, K, as shown in the subject described meaning. The next line N integers, the integer i is 0 for the first i personally Summer Jun 1 indicates the i-th
person is winter army.
[Format] output
line an integer representing the number of qualifying program is divided by the 1e9 + 7.
[Sample input]
. 4. 1
0 0. 1. 1
[output] Sample
5
[] Example Description
legitimate 5 kinds of programs are:
0. 1. 1 0
0 0. 1 |. 1
0 | 0. 1. 1
0 | 0. 1 |. 1
0 | 0 | 1 | 1
And 00 | 1 | 1 is not a valid solution because the first paragraph, "00" is the number of troops in the summer of 2, the number of military winter is 0, the absolute value of the difference between the number of people exceeds the K.
[Data] range
of 20% to ensure data, N≤20.
50% of data guarantee, N≤8000.
Another 15% of the data to ensure that all people are summer army.
Another 15% of the data is guaranteed, K = 0.
100% guarantee of data, 1≤N≤10 ^ 5,0≤K≤N. Obvious, which is a DP; F [I] = F + [J] (J ~ I meet the requirements stated in the title); represents a prefix and then the original array matching conditions with s [i] is: abs (2 * s [i] -i + 2 * s [j-1] - (j-1)) <= k then let w [i] = 2 * s [i] -i; obviously, w [i ] -k <= w [j] <= w [i] + k found? Position transfer is a continuum; this time we can use Splay or Fenwick tree or tree line to vigorously maintain dp state; then you can off the AC; Note w [i] -k likely to be negative, so to array subscript to expand several times to prevent negative numbers;
 
 
 
 
 
 
 








#include <bits/stdc++.h>
#pragma GCC optimize(2)
#define p 1000000007
#define inc(i,a,b) for(register int i=a;i<=b;i++)
using namespace std;
int a[100010],sum[100010];
long long f[100010];
long long ss[100010];
int n,k;
long long w[100010];
long long c[600010];
class node{
	public:
	inline int lowbit(register int x){
		return x&(-x);
	}
	void add(register int x,register long long v){
		while(x<=6*n){
			c[x]=(c[x]+v)%p;
			x+=lowbit(x);
		}
	}
	int query(register int x){
		register long long res=0;
		while(x>0){
			res=(res+c[x])%p;
			x-=lowbit(x);
		}
		return res%p;
	}
}tree;
template<class nT>
inline void read(nT& x)
{
	char c;while(c=getchar(),!isdigit(c));
	x=c^48;while(c=getchar(),isdigit(c)) x=x*10+c-48;
}
int main()
{
	read(n); read(k);
	inc(i,1,n){
		read(a[i]); sum[i]=(sum[i-1]+a[i]);	
		w[i]=2*sum[i]-i;
	}
	f[0]=1;
	tree.add(w[0]+2*n,1);
	inc(i,1,n){
		long long tmp1=tree.query(w[i]+k+2*n),tmp2=tree.query(w[i]-k-1+2*n);
		long long tmp=((tmp1-tmp2)%p+p)%p;
		f[i]=tmp%p;
		tree.add(w[i]+2*n,f[i]);
	}
	cout<<f[n]%p;
}
/*
2 1
1 0

3 0
0 1 0

30 7
1 0 0 0 1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 1

49 17
0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 1 0 1 1 0 1 1 0 1 1 0 1
*/

 

Guess you like

Origin www.cnblogs.com/kamimxr/p/11794467.html