1748E Yet Another Array Counting Problem

题目传送门

题目大意:

现在有一个长度为\(n\)的数组\(a\) , 现在你要找到一个数组\(b\)的每一个数都\(<= m\) 使得在\(n\)中的任意区间\([l , r]\)满足\(max_{i = l} ^ ra_i = max_{i = l} ^ rb_i\) 求方案数 $ mod 1e9 + 7$

做法

设\(dp_{pos , i}\)表示\(pos\)作为左边最大值的位置,且\(b_{pos} <= i\) 的方案数

显然\(dp_{pos , i} = \sum _{j = 1} ^ i dp_{lpos , j - 1} * dp_{rpos , j}\)

可以用线段树维护\(lpos、rpos\)

code

#include<bits/stdc++.h>
#define fu(x , y , z) for(int x = y ; x <= z ; x ++)
#define LL long long
using namespace std;
const int N = 2e5 + 5;
const LL mod = 1e9 + 7;
vector<LL> dp[N];
int a[N] , n , m , rt , tr[N << 2];
int read () {
int val = 0 , fu = 1;
char ch = getchar ();
while (ch < '0' || ch > '9') {
if (ch == '-') fu = -1;
ch = getchar ();
}
while (ch >= '0' && ch <= '9') {
val = val * 10 + (ch - '0');
ch = getchar ();
}
return val * fu;
}
void build (int p , int l , int r) {
if (l == r) {
tr[p] = l;
}
else {
int mid = l + r >> 1 , ll = p << 1 , rr = (p << 1) + 1;
build (ll , l , mid) , build (rr , mid + 1 , r);
if (a[tr[ll]] >= a[tr[rr]]) {
tr[p] = tr[ll];
}
else
tr[p] = tr[rr];
}
}
int query (int p , int l , int r , int L , int R) {
if (L <= l && R >= r) {
return tr[p];
}
else {
int mid = l + r >> 1 , ans1 = 0 , ans2 = 0 , ll = p << 1 , rr = (p << 1) + 1;
if (L <= mid)
ans1 = query (ll , l , mid , L , R);
if (mid < R)
ans2 = query (rr , mid + 1 , r , L , R);
return a[ans1] >= a[ans2] ? ans1 : ans2;
}
}
int dfs (int l , int r) {
if (l > r)
return -1;
if (l == r) {
fu(i , 1 , m)
dp[l][i] = i;
return l;
}
int id = query (1 , 1 , n , l , r);
int ll = dfs (l , id - 1) , rr = dfs (id + 1 , r);
if (ll == -1)
fu(i , 1 , m)
dp[id][i] = (dp[rr][i] + dp[id][i - 1]) % mod;
else if (rr == -1)
fu(i , 1 , m)
dp[id][i] = (dp[ll][i - 1] + dp[id][i - 1]) %mod;
else
fu(i , 1 , m)
dp[id][i] = (dp[ll][i - 1] * dp[rr][i] % mod + dp[id][i - 1]) % mod;
return id;
}
int main () {
int T = read ();
while (T --) {
n = read () , m = read ();
fu(i , 1 , n) {
a[i] = read ();
dp[i].clear ();
dp[i].resize(m + 1);
}
build (1 , 1 , n);
rt = dfs (1 , n);
printf ("%lld\n" , dp[rt][m]);
}
return 0;
}

1748E Yet Another Array Counting Problem的更多相关文章

  1. UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。

    /** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...

  2. 『The Counting Problem 数位dp』

    The Counting Problem Description 求 [L,R]内每个数码出现的次数. Input Format 若干行,一行两个正整数 L 和 R. 最后一行 L=R=0,表示输入结 ...

  3. POJ2282 The Counting Problem

    题意 Language:DefaultEspañol The Counting Problem Time Limit: 3000MS Memory Limit: 65536K Total Submis ...

  4. The Counting Problem

    The Counting Problem 询问区间\([a,b]\)中\(1\sim 9\)出现的次数,0 < a, b < 100000000. 解 显然为数位递推,考虑试填法,现在关键 ...

  5. [Codeforces 863D]Yet Another Array Queries Problem

    Description You are given an array a of size n, and q queries to it. There are queries of two types: ...

  6. UVa 1640 - The Counting Problem(数论)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. [Algorithm] Array production problem

    Given an array of integers, return a new array such that each element at index i of the new array is ...

  8. UVA 1640 The Counting Problem

    https://vjudge.net/problem/UVA-1640 题意:统计区间[l,r]中0——9的出现次数 数位DP 注意删除前导0 #include<cmath> #inclu ...

  9. [POJ 2282] The Counting Problem

    [题目链接] http://poj.org/problem?id=2282 [算法] 数位DP [代码] #include <algorithm> #include <bitset& ...

  10. 863D - Yet Another Array Queries Problem(思维)

    原题连接:http://codeforces.com/problemset/problem/863/D 题意:对a数列有两种操作: 1 l r ,[l, r] 区间的数字滚动,即a[i+1]=a[i] ...

随机推荐

  1. C++解决海盗分金问题

    #include <iostream> #include <unordered_map> #include <map> // 假定:每个海盗都是一样的聪明,没有谁比 ...

  2. Ubuntu系统Root用户无法登录解决办法

    默认 系统 root 登录 图形界面,出现 登录失败.解决方法如下: 1,登录普通用户, 打开终端执行命令, 使用su root或sudo -i切换到root用户(必须) su root 按照提示输入 ...

  3. windows环境下本地项目(或gitlab上拉取项目)在Jenkins上自动打包部署 超超超详细!!!!!

    一.环境准备 1.下载jdk,官网:http://www.oracle.com/ 2.下载Jenkins,官网:https://www.jenkins.io/ 3.下载Tomcat,官网:http:/ ...

  4. .Net 5.0导出Execl的两种方式

    项目中经常会用到表格的导入导出今天来简绍一下我所了解的两种方式 1.拼接成表格的简单方式直接导出,服务器上不用安装其他程序 可以直接导出 public async Task<FileResult ...

  5. tuxedo启动相关的知识

    tuxedo启动都要启动哪些服务? tuxedo常用命令有哪些? 参考链接: https://docs.oracle.com/cd/E13161_01/tuxedo/docs10gr3/rfcm/rf ...

  6. input_subsys 输入子系统框架分析

    在linux内核中 已做好各类驱动的框架,驱动程序也属于内核的一部分,我们可以在原有的驱动上修改,来匹配我们自已的硬件,也可以自已编写符合内核驱动框架的驱动程序.出于学习的目的,便于更好的理解各类驱动 ...

  7. Pytorch-UNet-master>utils>data_loading.py

    模块,包   在package_runoob同级目录下,用test.py调用package_runoob包中内容 参考链接: Python 模块 | 菜鸟教程 (runoob.com) Dataset ...

  8. 消息队列RabbitMQ业务场景应用及解决方案

    目录 0. 博客参考 1. 背景 2. 技术选型 3. 消息队列的几个常见问题 4. 代码功能开发及测试 4.1 生产者 4.2 消费者 5. 源代码 6.补充:消息的顺序性 0. 博客参考 http ...

  9. 自用nodejs安装笔记

    下载Nodejs 进入Nodejs官网https://nodejs.org/zh-cn/ 下载 安装Node.js 检查Nodejs和npm包管理器是否安装成功 用管理员打开cmd控制台 命令行输入n ...

  10. python进阶:带你学习实时目标跟踪

    摘要:本程序主要实现了python的opencv人工智能视觉模块的目标跟踪功能. 本文分享自华为云社区<python进阶--人工智能实时目标跟踪,这一篇就够用了!>,作者:lqj_本人 . ...