2017多校训练Contest4: 1012 Wavel Sequence hdu6078

Problem Description
Have you ever seen the wave? It's a wonderful view of nature. Little Q is attracted to such wonderful thing, he even likes everything that looks like wave. Formally, he defines a sequence  a1,a2,...,an  as ''wavel'' if and only if  a1<a2>a3<a4>a5<a6...



Picture from Wikimedia Commons


Now given two sequences  a1,a2,...,an  and  b1,b2,...,bm , Little Q wants to find two sequences  f1,f2,...,fk(1fin,fi<fi+1)  and  g1,g2,...,gk(1gim,gi<gi+1) , where  afi=bgi  always holds and sequence  af1,af2,...,afk  is ''wavel''.

Moreover, Little Q is wondering how many such two sequences  f  and  g  he can find. Please write a program to help him figure out the answer.
 

Input
The first line of the input contains an integer  T(1T15) , denoting the number of test cases.

In each test case, there are  2  integers  n,m(1n,m2000)  in the first line, denoting the length of  a  and  b .

In the next line, there are  n  integers  a1,a2,...,an(1ai2000) , denoting the sequence  a .

Then in the next line, there are  m  integers  b1,b2,...,bm(1bi2000) , denoting the sequence  b .
 

Output
For each test case, print a single line containing an integer, denoting the answer. Since the answer may be very large, please print the answer modulo  998244353 .
 

Sample Input
  
  
1 3 5 1 5 3 4 1 1 5 3
 

Sample Output
  
  
10
Hint
(1)f=(1),g=(2). (2)f=(1),g=(3). (3)f=(2),g=(4). (4)f=(3),g=(5). (5)f=(1,2),g=(2,4). (6)f=(1,2),g=(3,4). (7)f=(1,3),g=(2,5). (8)f=(1,3),g=(3,5). (9)f=(1,2,3),g=(2,4,5). (10)f=(1,2,3),g=(3,4,5).


f_{i,j,k}fi,j,k表示仅考虑a[1..i]a[1..i]b[1..j]b[1..j],选择的两个子序列结尾分别是a_iaib_jbj,且上升下降状态是kk 时的方案数,则f_{i,j,k}=\sum f_{x,y,1-k}fi,j,k=fx,y,1k,其中x<i,y<jx<i,y<j。暴力转移的时间复杂度为O(n^4)O(n4),不能接受。

考虑将枚举决策点x,yx,y的过程也DP掉。设g_{i,y,k}gi,y,k表示从某个f_{x,y,k}fx,y,k作为决策点出发,当前要更新的是ii的方案数,h_{i,j,k}hi,j,k表示从某个f_{x,y,k}fx,y,k作为决策点出发,已经经历了gg的枚举,当前要更新的是jj的方案数。转移则是要么开始更新,要么将ii或者jj继续枚举到i+1i+1以及j+1j+1。因为每次只有一个变量在动,因此另一个变量恰好可以表示上一个位置的值,可以很方便地判断是否满足上升和下降。

时间复杂度O(n^2)O(n2)


以上是官方题解

然后我们的程序复杂度似乎不是n^2的。中间用二维树状数组维护转移所以多了两个log

#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cassert>
using namespace std;
const int N=2110;
const int M=2005;
const int pp=998244353;
int tre[2][N][N];
int f[2][N][N];
int a[N],b[N];
int n,m;
void add(int &x,int y)
{
    x=x+y;
    if (x>pp) x-=pp;
}
void work(int now,int xx,int yy,int v)
{
    for (int x=xx;x<=M;x+=x&-x)
        for (int y=yy;y<=M;y+=y&-y)
        add(tre[now][x][y],v);
}
int get(int now,int xx,int yy)
{
    int s=0;
    for (int x=xx;x;x-=x&-x)
        for (int y=yy;y;y-=y&-y)
        add(s,tre[now][x][y]);
    return s;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%d%d",&n,&m);
        for (int i=0;i<=M;i++)
        for (int j=0;j<=M;j++) tre[0][i][j]=tre[1][i][j]=0;
        for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
        f[0][i][j]=f[1][i][j]=0;
        for (int i=1;i<=n;i++)
        scanf("%d",&a[i]);
        for (int i=1;i<=m;i++)
        scanf("%d",&b[i]);
        int an=0;
        for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++)
        if (a[i]==b[j])
        {
            add(f[0][i][j],(get(1,j-1,M)-get(1,j-1,a[i])+pp)%pp);
            add(f[1][i][j],get(0,j-1,a[i]-1));
            add(f[0][i][j],1);
            work(0,j,a[i],f[0][i][j]);
            work(1,j,a[i],f[1][i][j]);
            add(an,f[0][i][j]);
            add(an,f[1][i][j]);
        }
        printf("%d\n",(an+pp)%pp);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值