[loj3247]Non-Decreasing Subsequences
分治,考虑分治到[l,r]的区间(设$mid=(l+r)/2$),将询问分为两类:
1.在左/右区间,直接递归下去;
2.跨越中间,那么处理出两个数组:L[i][j]表示左区间在开头第i个位置,以数字j为结尾的上升子序列个数(不跨越mid),右区间同理
(L和R的计算很简单,只需要再处理出一个L[i][j][k]表示i后面以j为开头,以k为结尾的子序列个数即可)
对其求前/后缀和,那么就求出了开头在第i个位置以后/结尾在第i个位置以前的子序列个数
设询问区间为[x,y],那么答案就是$\sum_{i=1}^{20}L[x][i]+R[y][i]+L[x][i]\sum_{j=i}^{20}R[y][j]$,注意取模
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 200005
4 #define mid (l+r>>1)
5 #define mod 1000000007
6 struct ji{
7 int l,r,id;
8 }q[N];
9 int n,m,a[N],ans[N],f[N][21],ff[N][21][21],g[N][21],gg[N][21][21],sum[N][22];
10 void dfs(int l,int r,int x,int y){
11 if (l==r){
12 for(int i=x;i<=y;i++)ans[q[i].id]=1;
13 return;
14 }
15 int p=x;
16 for(int i=x;i<=y;i++)
17 if (q[i].r<=mid)swap(q[p++],q[i]);
18 dfs(l,mid,x,p-1);
19 x=p;
20 for(int i=x;i<=y;i++)
21 if (mid<q[i].l)swap(q[p++],q[i]);
22 dfs(mid+1,r,x,p-1);
23 x=p;
24 memset(f[mid],0,sizeof(f[mid]));
25 memset(ff[mid],0,sizeof(ff[mid]));
26 memset(g[mid],0,sizeof(g[mid]));
27 memset(gg[mid],0,sizeof(gg[mid]));
28 ff[mid][a[mid]][a[mid]]=f[mid][a[mid]]=1;
29 for(int i=mid-1;i>=l;i--)
30 for(int j=1;j<=20;j++){
31 f[i][j]=0;
32 for(int k=1;k<=j;k++){
33 ff[i][j][k]=ff[i+1][j][k];
34 if (k==a[i]){
35 if (j==k)ff[i][j][k]++;
36 for(int t=k;t<=20;t++)ff[i][j][k]=(ff[i][j][k]+ff[i+1][j][t])%mod;
37 }
38 f[i][j]=(f[i][j]+ff[i][j][k])%mod;
39 }
40 }
41 gg[mid+1][a[mid+1]][a[mid+1]]=g[mid+1][a[mid+1]]=1;
42 for(int i=mid+2;i<=r;i++)
43 for(int j=1;j<=20;j++){
44 g[i][j]=0;
45 for(int k=j;k<=20;k++){
46 gg[i][j][k]=gg[i-1][j][k];
47 if (k==a[i]){
48 if (j==k)gg[i][j][k]++;
49 for(int t=1;t<=k;t++)gg[i][j][k]=(gg[i][j][k]+gg[i-1][j][t])%mod;
50 }
51 g[i][j]=(g[i][j]+gg[i][j][k])%mod;
52 }
53 }
54 for(int i=mid+1;i<=r;i++)
55 for(int j=20;j;j--)sum[i][j]=(sum[i][j+1]+g[i][j])%mod;
56 for(int i=x;i<=y;i++)
57 for(int j=1;j<=20;j++)
58 ans[q[i].id]=(ans[q[i].id]+f[q[i].l][j]*(1LL+sum[q[i].r][j])+g[q[i].r][j])%mod;
59 }
60 int main(){
61 scanf("%d%*d",&n);
62 for(int i=1;i<=n;i++)scanf("%d",&a[i]);
63 scanf("%d",&m);
64 for(int i=1;i<=m;i++){
65 scanf("%d%d",&q[i].l,&q[i].r);
66 q[i].id=i;
67 }
68 dfs(1,n,1,m);
69 for(int i=1;i<=m;i++)printf("%d\n",(ans[i]+1)%mod);
70 }
[loj3247]Non-Decreasing Subsequences的更多相关文章
- CF502C The Phone Number
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes Mrs. Smith ...
- CodeForces - 1017 C. The Phone Number(数学)
Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secret phone number! The ...
- cf 1017C
C. The Phone Number time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces 597C C. Subsequences(dp+树状数组)
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Leetcode Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- LeetCode(115) Distinct Subsequences
题目 Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequen ...
随机推荐
- oracle基础安全配置
1.oracle中用户密码复杂度配置 1)查看参数 select limit from dba_profiles where resource_name='PASSWORD_VERIFY_FUNCTI ...
- JUC多线程之ThreadPoolExecutor类任务执行流程
ThreadPoolExecutor类: ThreadPoolExecutor是我们最常用的一个线程池类,它实现了AbstractExecutorService接口.首先来看一下它的构造器及相关关键变 ...
- bzoj2064分裂(dp)
题目大意: 给定一个初始集合和目标集合,有两种操作:1.合并集合中的两个元素,新元素为两个元素之和 2.分裂集合中的一个元素,得到的两个新元素之和等于原先的元素.要求用最小步数使初始集合变为目标集合, ...
- 【UE4 C++】关卡切换、流关卡加载卸载
切换关卡 基于 UGameplayStatics:: OenLevel UGameplayStatics::OpenLevel(GetWorld(), TEXT("NewMap") ...
- Java继承中父类和子类构造函数的问题
父类有无参构造函数时(显示或隐式),子类的有参和无参构造函数都是默认调用父类的无参构造函数:当父类只有有参构造函数时,子类可以有有参和无参构造函数,子类有参构造函数必须显式调用父类的有参构造函数,子类 ...
- Java:volatile笔记
Java:volatile笔记 本笔记是根据bilibili上 尚硅谷 的课程 Java大厂面试题第二季 而做的笔记 1. volatile 和 JMM 内存模型的可见性 JUC 下的三个包 java ...
- [对对子队]会议记录4.12(Scrum Meeting 3)
今天已完成的工作 朱骏豪 工作内容:找到了游戏的背景场景,用PS扣了按钮的图 相关issue:实现UI的美术需求 实现游戏场景中的必要模型 梁河览 工作内容:将关卡选择界面和欢迎界面导入项 ...
- Windows平台编译器相关的几个预定义宏
WIN32 是在windows.h 中定义的宏,包含winodws.h则定义该宏 _WIN32/_WIN64跟windows平台有关的宏,_WIN32在windows 32位和64位下都有该宏,_ ...
- 计算机网络之网络层移动IP
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105319753 学习课程:<2019王道考研计算机网络> 学习目的 ...
- 对dy和Δy的浅薄理解
一.导数定义 当函数y=f(x)的自变量x在一点x0上产生一个增量Δx时,函数输出值的增量Δy与自变量增量Δx的比值在Δx趋于0时的极限a如果存在,a即为在x0处的导数,记作f'(x0)或df(x0) ...