HDU 6078 Wavel Sequence 树状数组优化DP
Wavel Sequence
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.
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.
3 5
1 5 3
4 1 1 5 3
(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).
#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 ;
}
最新文章
- 网络切片在5G中的应用
- ASP.NET MVC 混搭 ASP.NET WebForms 所导致的 Html.ActionLink/BeginForm 问题
- 解决问题:The context cannot be used while the model is being created
- TYVJ 1074 武士风度的牛
- mysql 事务提交过程
- hdu 1040 As Easy As A+B
- 九度OJ1184二叉树
- 查看当前hadoop的版本号
- ManagerDay-1
- android移植pppoe拨号上网的全过程
- [渣译文] SignalR 2.0 系列: 开始使用SignalR 2.0
- POJ 1704 Georgia and Bob(阶梯博弈+证明)
- 正则匹配 sql语句参数
- 【Android Developers Training】 101. 显示快速联系人挂件(Quick Contact Badge)
- JetBrain server certificate is not trusted 弹出框
- SAP MM 根据采购订单反查采购申请?
- Linux - 变量的查看与设置
- Excel里面Left这个怎么用?
- vue-3-Class 与 Style 绑定
- 浅谈 Unix I/O 模型