题目链接

题目大意: 给出n个数, 让你求出有多少个5元组满足

i < j < k < l < m并且ai < aj < ak < al < am

我们令d[i][j]表示以j位置为末尾的长度是i的序列的个数。

比如1234567这7个数。 dp[1][1], dp[1][2], dp[1][7]都是1。

dp[2][1] = 0, dp[2][2] = 1(1, 2), dp[2][3] = 3( (1,3), (2,3))。 依次类推。

那么显然以j位置结尾, 长度为i的个数等于 $ \sum dp[i-1][k] $

k为在j之前的, 并且ak < aj的数。

具体可以自己画一画就很好明白。

那么显然可以用树状数组来做。

答案会爆long long 需要自己写大数。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxlen = 5;
const int maxn = 5e4+5;
struct BigInteger
{
int num[6];
BigInteger() {
mem(num);
}
BigInteger (int b) {
mem(num);
int len = 0;
while(b) {
num[len++] = b%10000000;
b /= 10000000;
}
}
BigInteger operator + (BigInteger a) const
{
BigInteger ret(0);
for(int i = 0; i < maxlen; i++) {
ret.num[i] = num[i] + a.num[i] + ret.num[i];
ret.num[i+1] = ret.num[i]/10000000;
ret.num[i] %= 10000000;
}
return ret;
}
void output() {
int i;
for(i = maxlen; i>= 0; i--) {
if(num[i])
break;
}
printf("%d", num[i--]);
for(; i>=0; i--) {
printf("%07d", num[i]);
}
cout<<endl;
}
};
BigInteger sum[5][maxn<<2];
int a[maxn], b[maxn];
void update(int id, int p, BigInteger val, int l, int r, int rt) {
if(l == r) {
sum[id][rt] = sum[id][rt] + val;
return ;
}
int m = l+r>>1;
if(p<=m)
update(id, p, val, lson);
else
update(id, p, val, rson);
sum[id][rt] = sum[id][rt<<1]+sum[id][rt<<1|1];
}
BigInteger query(int id, int L, int R, int l, int r, int rt) {
if(L>R)
return BigInteger(0);
if(L<=l&&R>=r) {
return sum[id][rt];
}
int m = l+r>>1;
BigInteger ret(0);
if(L<=m)
ret = query(id, L, R, lson);
if(R>m)
ret = ret + query(id, L, R, rson);
return ret;
}
int main()
{
int n;
while(~scanf("%d", &n)) {
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b[i-1] = a[i];
}
mem(sum);
sort(b, b+n);
int cnt = unique(b, b+n)-b;
for(int i = 1; i <= n; i++) {
a[i] = lower_bound(b, b+cnt, a[i])-b+1;
}
BigInteger ans(0), one(1);
for(int i = 1; i <= n; i++) {
update(0, a[i], one, 1, n, 1);
for(int j = 1; j < 5; j++) {
BigInteger tmp = query(j-1, 1, a[i]-1, 1, n, 1);
update(j, a[i], tmp, 1, n, 1);
if(j == 4) {
ans = ans+tmp;
}
}
}
ans.output();
}
return 0;
}

poj 3378 Crazy Thairs dp+线段树+大数的更多相关文章

  1. [POJ 3378] Crazy Thairs

    Link: POJ 3378 传送门 Solution: 按序列长度$dp$, 设$dp[i][j]$为到第$i$个数,符合要求的序列长度为$j$时的序列个数, 易得转移方程:$dp[i][j]=\s ...

  2. POJ 3378 Crazy Thairs(树状数组+DP)

    [题目链接] http://poj.org/problem?id=3378 [题目大意] 给出一个序列,求序列中长度等于5的LIS数量. [题解] 我们发现对于每个数长度为k的LIS有dp[k][i] ...

  3. ●POJ 3378 Crazy Thairs

    题链: http://poj.org/problem?id=3378 题解: 树状数组维护,高精度. 依次考虑以每个位置结尾可以造成的贡献. 假设当前位置为i,为了达到5个元素的要求,我们需要求出,在 ...

  4. ZOJ 3349 Special Subsequence 简单DP + 线段树

    同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...

  5. hdu 3016 dp+线段树

    Man Down Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  6. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  7. cf834D(dp+线段树区间最值,区间更新)

    题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...

  8. Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)

    Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...

  9. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

随机推荐

  1. asp.net cookie的使用.

    Cookie就是服务器暂时存放在你的电脑里的资料(.txt格式的文本文件),好让服务器用来辨认你的计算机.当你在浏览网站的时候,Web服务器会先送一小小资料放在你的计算机上,Cookies 会帮你在网 ...

  2. nsstring 替换

    -(NSString*)searchaAndPlacing:(NSString*)string { NSString *text=[string stringByReplacingOccurrence ...

  3. Subsets,Subsets II

    一.Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...

  4. Permutations,Permutations II,Combinations

    这是使用DFS来解数组类题的典型题目,像求子集,和为sum的k个数也是一个类型 解题步骤: 1:有哪些起点,例如,数组中的每个元素都有可能作为起点,那么用个for循环就可以了. 2:是否允许重复组合 ...

  5. yum php56

    美国时间2014年11月13日,PHP开发团队,在「PHP 5.6.3 is available|PHP: Hypertext Preprocessor」上公布了PHP5.6系的最新版本「PHP 5. ...

  6. 6 支持向量机SVM

    注:理论部分参考:http://blog.csdn.net/v_july_v/article/details/7624837 (1)SVM是现成最好的分类器,这里“现成”指的是分类器不加修改即可直接使 ...

  7. windows下使用mingw编译python扩展模块

    环境: 1.python2.7.2 2.mingw20120426 说明: PYTHON_HOME:不做特殊说明的话,默认代表python的安装路径. MINGW_HOME:不做特殊说明的话,默认代表 ...

  8. Linux05--Shell程序设计01

    1.Shell脚本介绍 基本介绍: shell脚本是一个可执行的纯文本文件,由多个shell命令组成. 命令的执行是从上而下,从左而右的分析和执行 命令,参数间的多个空白也会被忽略 #是注释 #!用于 ...

  9. Java中,当表单含有文件上传时,提交数据的如何读取

    http://blog.csdn.net/lian_zhihui1984/article/details/6822201

  10. Jquery html页面处理基础

    1.怎样获得浏览器的可视高度?   var windHight = $(window).height(); //获得浏览器的可视高度 2.怎样获得滚动条相对于顶部的高度?   var scrollHi ...