2016多校训练Contest9: 1002 Best Division hdu5845

来源:互联网 发布:sip电话软件 编辑:程序博客网 时间:2024/05/16 07:52

Problem Description
You are given an array A, consisting of N integers.

You are also given 2 integers K and L.

You must divide the whole array A into exactly K nonempty intervals, such that the length of each interval is not greater than L.

The cost of an interval [S, E] is the bitwise XOR sum of all elements of A whose indices are in [S, E].

The score of a division is simply the maximum cost of K intervals in the division. You are interested in the best division, which minimizes the score of the division. Since this is too simple for you, the problem is reversed.

You know the minimum score: the answer for the original problem is not greater than X. Now you want to know, the maximum value of K.
 

Input
There are several test cases.

The first line of the input contains an integer T (1<=T<=20), the number of test cases. Then T test cases follow.

Each test case starts with 3 integers N, X, L (1<= L<=N<=100000, 0<=X<268435456), which are described above.

The next line contains 3 integers A[1], P, Q. All other integers of the array A are generated from these 3 integers in the following rule:

For every integer 1<k<=N, A[k] = (A[k-1]*P+Q) mod 268435456.
(0 <= A[1], P, Q < 268435456)
 

Output
For each test case, you should print a single line containing the answer.

If the answer does not exist, just print 0.
 

Sample Input
23 1 21 1 13 0 31 1 1
 

Sample Output
21

首先朴素转移,f[i]表示前i个数可以分成的满足条件的最大段数

f[i]=max(f[j]+1),a[j]^a[j+1]^……^a[i]<=x,i-l+1>=j

然后观察满足转移的条件,令s[i]为i的前缀异或和

那么上式即为s[i]^s[j]<=x,i-l+1>=j

考虑用trie维护所有的s[j]

那么转移可以直接从trie上查找最大的f[i]

每次加入s[i]后删除s[i-l来]维护trie的值即可

因为如果s[i]相同,后面的f[i]一定不小于前一次出现的f[i]

因此维护某个s[i]出现次数,每次用后面的f[i]覆盖前面的,减成0的时候清空trie的值即可

#include<map>#include<cmath>#include<queue>#include<vector>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>using namespace std;long long mod=268435456;struct tree{int ll,rr;int s,x;}tr[3000001];int tot;int a[40],b[40];int f[100001],px[100001];inline void cale(int xx){memset(a,0,sizeof(a));int d=0;while(xx>0){d++;a[d]=xx%2;xx=xx/2;}}inline void up(int p){tr[p].x=max(tr[tr[p].ll].x,tr[tr[p].rr].x);}inline void add(int p,int d,int xx){if(d==0)return ;tr[p].s++;if(a[d]==0){if(tr[p].ll!=0)add(tr[p].ll,d-1,xx);else{tot++;tr[p].ll=tot;add(tr[p].ll,d-1,xx);}}else{if(tr[p].rr!=0)add(tr[p].rr,d-1,xx);else{tot++;tr[p].rr=tot;add(tr[p].rr,d-1,xx);}}if(d==1)tr[p].x=xx;elseup(p);}inline void dec(int p,int d){if(d==1)return ;tr[p].s--;if(tr[p].s==0)tr[p].x=0;if(a[d]==0)dec(tr[p].ll,d-1);elsedec(tr[p].rr,d-1);up(p);}inline int ask(int p,int d){if(d==1||p==0)return tr[p].x;if(a[d]==0){if(b[d]==0)return ask(tr[p].ll,d-1);elsereturn max(ask(tr[p].rr,d-1),tr[tr[p].ll].x);}else{if(b[d]==0)return ask(tr[p].rr,d-1);elsereturn max(ask(tr[p].ll,d-1),tr[tr[p].rr].x);}}int main(){//freopen("1002.in","r",stdin);//freopen("1002.ans","w",stdout);int T;scanf("%d",&T);while(T>0){T--;tot=1;memset(tr,0,sizeof(tr));int n,x,l;scanf("%d%d%d",&n,&x,&l);long long p,q;scanf("%d%I64d%I64d",&px[1],&p,&q);int i;for(i=2;i<=n;i++)px[i]=((long long)px[i-1]*p+q)%mod;for(i=2;i<=n;i++)px[i]=(px[i]^px[i-1]);memset(f,0,sizeof(f));f[0]=0;cale(x);memset(b,0,sizeof(b));for(i=1;i<=35;i++)b[i]=a[i];for(i=1;i<=n;i++){cale(px[i]);int xt=ask(1,30);if(xt!=0)f[i]=xt+1;else if(px[i]<=x)f[i]=1;if(f[i]!=0)add(1,30,f[i]);if(i>l&&f[i-l]!=0){cale(px[i-l]);dec(1,30);}}printf("%d\n",f[n]);}return 0;}



0 0
原创粉丝点击