题目链接

题目大意: 给出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. Spring事务传播机制详解

    1 事务的传播属性(Propagation) 1) REQUIRED ,这个是默认的属性 Support a current transaction, create a new one if none ...

  2. linux通过文件查找依赖关系

    通过文件查找安装包安装缺少libstdc++6这个文件在ls /usr/lib/libstd*下有两个文件/usr/lib/libstdc++.so.6 /usr/lib/libstdc++.so.6 ...

  3. ios消息的交互方式

    注意这些都是界面回传(即从第二个界面传到第一个界面,从第一个界面传到第二个界面的时候用第二个界面的属性即可)   iOS消息的交互方式有4种,分别为:通知,代理,block,kvo 现在我们对这个4中 ...

  4. 如何:控制命名空间前缀 (C#) (LINQ to XML)

    Visual Studio 2010 本主题介绍在序列化 XML 树时如何控制命名空间前缀. 在很多情况下,不需要控制命名空间前缀. 但是,某些 XML 编程工具需要命名空间前缀的特定控制. 例如,您 ...

  5. javaweb一周总结(菜鸟)

    我的试用期开始了. 这是我的第一篇博客,这也是我作为java工程师的第六天,主要是为了记录一周内出现的问题以及一些工作上的解答,吐槽一句工作的确和想的不一样之后直接记录下吧. 由于拥有工作平台看不到底 ...

  6. 用Cookie和Session实现用户登录 函数

    由于网页是一种无状态的连接程序,你无法得知用户的浏览状态,必须通过Cookie或Session记录用户的有关信息. Cookie: 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制. PHP透 ...

  7. Linux man 后面的数字含义及作用

    Linux的man很强大,该手册分成很多section,使用man时可以指定不同的section来浏览,各个section意义如下: 1 Executable programs or shell co ...

  8. QTcpSocket 及 TCP粘包分析

    ----我的生活,我的点点滴滴!! 这两天用Qt简单的实现一个tcp多线程client,在此记录下知识. 一.长连接与短连接 1.长连接 Client方与Server方先建立通讯连接,连接建立后不断开 ...

  9. <!DOCTYPE html>

    html5标准网页声明,原先的是一串很长的字符串,现在是这个简洁形式,支持html5标准的主流浏览器都认识这个声明.表示网页采用html5.

  10. libcurl提交表单上传文件

    不多说了,curl的http上传文件代码示例,有需要的可以参考. int http_post_file(const char *url, const char *user, const char *p ...