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. drf(3)

    1 不使用drf编写5个接口 1.1 路由 urlpatterns = [    path('books/', views.BookView.as_view()),    path('books/&l ...

  2. Servlet的学习之路

    一.什么是什么Servlet? Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程 ...

  3. 【Python】pcap抓MySQL网络包

    pcap # -*- coding:utf-8 -*- # yum install libpcap-devel python-devel # pip install pypcap hexdump -i ...

  4. Vue的官方脚手架 Vue-cli 安装使用解析

    ------------恢复内容开始------------ 1.首先什么是vue-cli 可以知道Vue-cli是一个官方提供的脚手架,主要作用是用来快速搭建Vue的项目模板,可以预先定义好项目的结 ...

  5. CF1732A Bestie

    思路 观察数据\(n \le 20\) 直接暴力. 我们直接算所有数的\(GCD\),然后枚举\(1\)~\(n\)的每一个数要不要选,然后选的话,就把原来的\(GCD\)和当前枚举的数\(GCD\) ...

  6. Python学习笔记-argparse模块

    Python学习笔记-argparse模块 optparseargparse 昨天学习了一个简单的端口扫描器的脚本,其中涉及到了optparse模块,网上关于此模块的介绍已有很多,但这个模块已经不更新 ...

  7. 7. 基础增删改 - 创建管理员用Model-Drive App管理后台信息 - 在Model-Driven App中创建视图

    ​ 当我们创建完Model-Driven之后,就可以在里面创建我们所需要的视图,视图一般分为三类: 个人:根据自己的个人需求创建个人视图,只有创建者和其分享的人才能查看这些视图. 公共:可以根据团体需 ...

  8. goujian

    第二章的内容先对第一张来说更偏向于技术性,在团队合作中,如何保证自己所负责模块的质量的稳定,这就对自身的技术和一些良好的代码书写习惯有一定的要求.这里除了之前接触的代码的整齐(段落划分),变量值和文件 ...

  9. Web For Pentester - SQL injections/Directory traversal

    SQL injections Example 1 典型的SQL注入 name=root' or 1=1 %23 直接执行就可以导出所有用户 查看后端的源码 观察到,我们传入入的name中,传入就成为了 ...

  10. SpringBoot笔记--自动配置(高级内容)(中集)

    @Enable*注解 使用该注解,需要导入相应的依赖坐标,其中的groupId标签里面写入Bean的Java文件所在的包的路径下面 spring-enable-other 还需要在SpringBoot ...