Wavel Sequence

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(1≤fi≤n,fi<fi+1) and g1,g2,...,gk(1≤gi≤m,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(1≤T≤15), denoting the number of test cases.

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

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

Then in the next line, there are m integers b1,b2,...,bm(1≤bi≤2000), 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).

 
 
题解:
  设定dp[i][j][0/1] 表示已a[i], a[j]结尾的 序列,长度为奇数1,偶数0的方案数
  假设 当前a[i] = a[j] = x;
  那么dp[i][j][1]就要继承   满足尾端值比当前x大的 那些位置的那些dp[i'][j'][0]    (1<=i' < i && 1<=j' < j)
  同理dp[i][j][0] 就要继承 满足结尾值比x小的那些 dp[i'][j'][1]    1<=i' < i && 1<=j' < j
  定义树状数组sum[i][0] 表示 以i值结尾的 长度为奇数偶数的方案
  在树状数组上面修改查询即可
代码:
  

#include<bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 2e5+, M = 1e3+,inf = 2e9;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} const LL mod = 998244353LL; int n,m,a[N],b[N];
int nex[N],head[N],last[N],fi[N],vis[N],mx;
LL sum[N][],dp[][][];
void update(int x,LL c,int p) {
for(int i = x; i <= mx; i += i&(-i))
sum[i][p] += c,sum[i][p] %= mod;
}
LL ask(int x,int p) {
LL ret = ;
if(x == ) return ;
for(int i = x; i; i -= i&(-i))
ret += sum[i][p],ret %= mod;
return ret;
}
int main() {
int T;
T = read();
while(T--) {
n = read();
m = read();
mx = -;
for(int i = ; i <= n; ++i) a[i] = read(),mx = max(mx,a[i]);
for(int i = ; i <= m; ++i) b[i] = read(),mx = max(mx,b[i]);
for(int i = ; i <= n; ++i)
for(int j = ; j <= m;++j)
for(int k = ; k < ; ++k)
dp[i][j][k] = ; LL ans = ,tmp1,tmp2;
for(int i = ; i <= n; ++i)
{
for(int j = ; j <= m; ++j)
dp[i][j][] += dp[i-][j][],dp[i][j][] %= mod,
dp[i][j][] += dp[i-][j][],dp[i][j][] %= mod;
for(int j = ; j <= mx; ++j) sum[j][] = , sum[j][] = ; for(int j = ; j <= m; ++j) {
if(a[i] == b[j]) { tmp1 = ask(a[i]-,) % mod;
tmp2 = (ask(mx ,) - ask(a[i],) + mod) % mod; dp[i][j][] += tmp1;
dp[i][j][] %= mod;
dp[i][j][] += (tmp2+1LL) % mod;
dp[i][j][] %= mod; ans += ((tmp1+tmp2)%mod+1LL) % mod;
ans %= mod;
}
if(dp[i-][j][])
update(b[j],dp[i-][j][],);
if(dp[i-][j][])
update(b[j],dp[i-][j][],);
}
}
printf("%lld\n",ans);
}
return ;
}
 

HDU 6078 Wavel Sequence 树状数组优化DP的更多相关文章

  1. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

  2. Codeforces 946G Almost Increasing Array (树状数组优化DP)

    题目链接   Educational Codeforces Round 39 Problem G 题意  给定一个序列,求把他变成Almost Increasing Array需要改变的最小元素个数. ...

  3. LUOGU P2344 奶牛抗议 (树状数组优化dp)

    传送门 解题思路 树状数组优化dp,f[i]表示前i个奶牛的分组的个数,那么很容易得出$f[i]=\sum\limits_{1\leq j\leq i}f[j-1]*(sum[i]\ge sum[j- ...

  4. 【题解】Music Festival(树状数组优化dp)

    [题解]Music Festival(树状数组优化dp) Gym - 101908F 题意:有\(n\)种节目,每种节目有起始时间和结束时间和权值.同一时刻只能看一个节目(边界不算),在所有种类都看过 ...

  5. 【题解】ARC101F Robots and Exits(DP转格路+树状数组优化DP)

    [题解]ARC101F Robots and Exits(DP转格路+树状数组优化DP) 先删去所有只能进入一个洞的机器人,这对答案没有贡献 考虑一个机器人只能进入两个洞,且真正的限制条件是操作的前缀 ...

  6. Codeforces 909C Python Indentation:树状数组优化dp

    题目链接:http://codeforces.com/contest/909/problem/C 题意: Python是没有大括号来标明语句块的,而是用严格的缩进来体现. 现在有一种简化版的Pytho ...

  7. BZOJ3594: [Scoi2014]方伯伯的玉米田【二维树状数组优化DP】

    Description 方伯伯在自己的农田边散步,他突然发现田里的一排玉米非常的不美. 这排玉米一共有N株,它们的高度参差不齐. 方伯伯认为单调不下降序列很美,所以他决定先把一些玉米拔高,再把破坏美感 ...

  8. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)

    题意: 线段树做法 分析: 因为每次都是在当前位置的前缀区间查询最大值,所以可以直接用树状数组优化.比线段树快了12ms~ 代码: #include<cstdio> #include< ...

  9. BZOJ 3594: [Scoi2014]方伯伯的玉米田 (二维树状数组优化DP)

    分析 首先每次增加的区间一定是[i,n][i,n][i,n]的形式.因为如果选择[i,j](j<n)[i,j](j<n)[i,j](j<n)肯定不如把后面的全部一起加111更优. 那 ...

随机推荐

  1. ubuntu14.04 Cannot find OpenSSL's <evp.h>

    Cannot find OpenSSL's <evp.h> when i configure php7 manually,i get trouble with that problem,f ...

  2. 九度oj 题目1120:全排列

    题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < 'b' < ... < 'y' < 'z',而且给定的字符串中 ...

  3. Azure Storage Blob文件重命名

    Azure Storage的SDK并没有提供文件重命名的方法,而且从StorageExplorer管理工具里操作修改文件名的时候也有明确提示: 是通过复制当前文件并命名为新文件名再删除旧文件,不保存快 ...

  4. IntelliJ IDEA 代码提示快捷键

    1.写代码时用Alt-Insert(Code|Generate…)可以创建类里面任何字段的getter与setter方法. mac版 是ctrl+enter 2.CodeCompletion(代码完成 ...

  5. C 语言中的 feof()函数

    功能: feof 是 C 语言标准库函数函数,其原型在 stdio.h 中,其功能是检测流上的文件结束符,如果文件结束,则返回非0值,否则返回0,文件结束符只能被 clearerr() 清除. 用法: ...

  6. Vue && Angular 双向绑定检测不到对象属性的添加和删除

    由于ES5的限制 Vue  && Angular 双向绑定检测不到对象属性的添加和删除  还有数组增加索引.这些改变不会触发change事件.Vue是因为实例化的时候已经把各个属性都s ...

  7. VMware---之网卡设置

    闲来无事,扯点皮,详细说下NAT配置过程 NAT全称Network Address Translation网络地址转换,顾名思义,配置的重点也是地址转换. 步骤1.配置局域网段及网关 打开vmware ...

  8. [OS X实用技巧]机器人应用:一键将图片转换为PNG/JPEG/TIFF

    转自:http://www.maczhi.com/archives/2842.html 按教程老出错....试验了后使用: - 取得指定Finder对象,其它不变,但运行后不会出错. OS X实用技巧 ...

  9. 2018.11.7 PION 模拟赛

    期望:100 + 80 + 75 = 255 实际:0 + 80 + 60 = 140 唉~一天比一天犯的错误智障,感觉noip要凉啊... 吓得我赶紧吃几颗药补补脑子. 奶一下大佬: lgj AK ...

  10. 关于时间,日期,星期,月份的算法(Java中Calendar的使用方法)

    原文:http://www.open-open.com/code/view/1446195787257 package cn.outofmemory.codes.Date; import java.u ...