LightOJ 1085(树状数组+离散化+DP,线段树)
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Description
An increasing subsequence from a sequence A1, A2 ... An is defined by Ai1, Ai2 ... Aik, where the following properties hold
- i1 < i2 < i3 < ... < ik and
- 2. Ai1 < Ai2 < Ai3 < ... < Aik
Now you are given a sequence, you have to find the number of all possible increasing subsequences.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 105) denoting the number of elements in the initial sequence. The next line will contain n integers separated by spaces, denoting the elements of the sequence. Each of these integers will be fit into a 32 bit signed integer.
Output
For each case of input, print the case number and the number of possible increasing subsequences modulo 1000000007.
Sample Input
3
3
1 1 2
5
1 2 1000 1000 1001
3
1 10 11
Sample Output
Case 1: 5
Case 2: 23
Case 3: 7
题解:让求单调递增序列的个数;dp[i]=sum(dp[j])+1(j<i);由于数太大,需要离散化由于是单调的,所以当相等的时候树状数组要从大到小inset,以免对后面相等的造成影响,还可以用线段树写;
树状数组:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int MOD=;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
int dp[MAXN],a[MAXN],p[MAXN];
int N;
int lowbit(int x){return x&(-x);}
void insert(int x,int v){
while(x<=N){
dp[x]=(dp[x]+v)%MOD;
x+=lowbit(x);
}
}
int sum(int x){
int ans=;
while(x>){
ans=(ans+dp[x])%MOD;
x-=lowbit(x);
}
return ans;
}
int cmp(int x,int y){
if(a[x]!=a[y])return a[x]<a[y];
else return x>y;
}
int main(){
int T,kase=;
SI(T);
while(T--){
SI(N);
for(int i=;i<=N;i++)SI(a[i]),p[i]=i;
sort(p+,p+N+,cmp);
mem(dp,);
for(int i=;i<=N;i++){
insert(p[i],sum(p[i])+);
}
printf("Case %d: %d\n",++kase,sum(N));
}
return ;
}
线段树:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define P_ printf(" ")
const int MOD=;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int MAXN=1e5+;
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r
#define ll root<<1
#define rr root<<1|1
int tree[MAXN<<],p[MAXN],a[MAXN];
int ans=;
void pushup(int root){
tree[root]=(tree[ll]+tree[rr])%MOD;
}
void insert(int root,int l,int r,int flog,int v){
int mid=(l+r)>>;
if(l==flog&&r==flog){
tree[root]=v;
return;
}
if(mid>=flog)insert(lson,flog,v);
if(mid<flog)insert(rson,flog,v);
pushup(root);
}
void sum(int root,int l,int r,int L,int R){
int mid=(l+r)>>;
if(l>=L&&r<=R){
ans=(ans+tree[root])%MOD;
return;
}
if(mid>=L)sum(lson,L,R);
if(mid<R)sum(rson,L,R);
return;
}
int cmp(int x,int y){
if(a[x]!=a[y])return a[x]<a[y];
else return x>y;
}
int main(){
int T,N,kase=;
SI(T);
while(T--){
SI(N);
for(int i=;i<=N;i++)SI(a[i]),p[i]=i;
sort(p+,p+N+,cmp);
mem(tree,);
for(int i=;i<=N;i++){
ans=;
sum(,,N,,p[i]);
insert(,,N,p[i],ans+);
}
printf("Case %d: %d\n",++kase,tree[]);
}
return ;
}
LightOJ 1085(树状数组+离散化+DP,线段树)的更多相关文章
- [Usaco2014 Open Gold ]Cow Optics (树状数组+扫描线/函数式线段树)
这道题一上手就知道怎么做了= = 直接求出原光路和从目标点出发的光路,求这些光路的交点就行了 然后用树状数组+扫描线或函数式线段树就能过了= = 大量的离散+模拟+二分什么的特别恶心,考试的时候是想到 ...
- 【BZOJ3110】【整体二分+树状数组区间修改/线段树】K大数查询
Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c 如果是2 a b c形式,表示询问从第a个位置到第b个位 ...
- bzoj4785:[ZJOI2017]树状数组:二维线段树
分析: "如果你对树状数组比较熟悉,不难发现可怜求的是后缀和" 设数列为\(A\),那么可怜求的就是\(A_{l-1}\)到\(A_{r-1}\)的和(即\(l-1\)的后缀减\( ...
- HDU - 1166 树状数组模板(线段树也写了一遍)
题意: 汉语题就不说题意了,用到单点修改和区间查询(树状数组和线段树都可以) 思路: 树状数组的单点查询,单点修改和区间查询. 树状数组是巧妙运用二进制的规律建树,建树就相当于单点修改.这里面用到一个 ...
- BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 418 Solved: 235 [ Submit][ ...
- BZOJ 4785 [Zjoi2017]树状数组 | 二维线段树
题目链接 BZOJ 4785 题解 这道题真是令人头秃 = = 可以看出题面中的九条可怜把求前缀和写成了求后缀和,然后他求的区间和却仍然是sum[r] ^ sum[l - 1],实际上求的是闭区间[l ...
- 2019.01.21 bzoj2441: [中山市选2011]小W的问题(树状数组+权值线段树)
传送门 数据结构优化计数菜题. 题意简述:给nnn个点问有多少个www型. www型的定义: 由5个不同的点组成,满足x1<x2<x3<x4<x5,x3>x1>x2 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 4325 离散化+树状数组 或者 不使用树状数组
题意:给出一些花的开放时间段,然后询问某个时间点有几朵花正在开放. 由于ti<1e9,我们需要先将时间离散化,然后将时间点抽象为一个数组中的点,显然,我们需要进行区间更新和单点查询,可以考虑线段 ...
随机推荐
- Amzon MWS API开发之 请求报告
时间一晃而过又过了两周,博客园更新的速度确实有点慢,今天我要分享的是对请求报告的调用. 在文档中,相信大家也看了下面这个流程图吧? 相关流程,在文档中也有细说,我就不一一去Copy了:http://d ...
- Delphi在Webbrowser上绘制图像
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- Unix/Linux环境C编程入门教程(30) 字符串操作那些事儿
函数介绍 rindex(查找字符串中最后一个出现的指定字符) 相关函数 index,memchr,strchr,strrchr 表头文件 #include<string.h> 定义函数 c ...
- JDBC批量操作
/** * 批量执行预定义模式的SQL */ public static void exeBatchParparedSQL() { ...
- API经济产业
技术大咖为我们铺好了前进道路,我们为什么还要敬而远之舍近索远呢?充分利用开源,利用API进行App有效整合. 为应用添加日志功能,Loggly; 为应用添加用户管理和身份认证模块,Stormpath; ...
- GCC 编译选项
http://www.cnblogs.com/xmphoenix/archive/2011/03/21/1989944.html GCC 编译选项(转) gcc提供了大量的警告选项,对代码中可能存在的 ...
- c++冒泡排序的模板函数设计
说明 由于课程设计需要,特编写本程序.本程序首先定义了一个冒泡程序的模板函数,然后在main()函数中定义了两个不同类型的数组,调用模板函数对其进行排序.(注意,本程序是在linux下编写,但是直接拷 ...
- 项目中常用方法总结(将将DataTable数据集映射到实体对象)【转】
本篇把项目中用到的一些通用方法总结出来, 这些方法因为经常需要在项目中用到,所以把它们归纳在一起, 形成一个.dll 文件是一个理想的选择. 这样也便于日后缩短开发周期. 一. 把一个DataGrid ...
- Font Awesome 4.0.3 提供了369个网页常用的矢量字体图标
Font Awesome 为您提供了一套可缩放的字体矢量图标,可以快速自定义图标的大小,颜色,阴影,这些都可以通过CSS来实现,无需任何的JS代码哦. 一,主要特点如下: 1,一个字体,369个图标 ...