题目链接

题目大意: 给出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. 蜗牛爱课- iOS中定时器NSTimer使用

    调用一次计时器方法: //不重复,只调用一次.timer运行一次就会自动停止运行 self.locationTimer = [NSTimer  target:self selector: @selec ...

  2. FIFO算法

    package studyJava;class FIFO{ /* * 先进先出算法 * N:内存块的个数 * array:内存块 * size:页面数目 * */ private static fin ...

  3. jQuery渐变弹出层

    css: [css]#race{display:block;width:200px;height:50px;line-height:50px;text-align:center;background: ...

  4. 自制DTU

        最近打算自己做一款工业级DTU产品,预计到今年年底前完成,博客会随时更新产品进度,有兴趣的朋友可以联系我.

  5. 论山寨手机与Android 【11】移动网络规范的合纵连横

    上一章我们讨论了SmartPhone BP部分的硬件系统,接下去我们将讨论SmartPhone BP部分的软件系统.所谓BP,指的是基带处理器(Baseband Processor),又称为通讯处理器 ...

  6. SQL Server创建LinkServer

    USE [master] GO /****** Object: LinkedServer [xxx_LNK] Script Date: 2014/7/7 17:04:13 ******/ EXEC m ...

  7. css案例学习之id要唯一

    ID有两个的后果 <html> <head> <title>ID选择器</title> <style type="text/css&qu ...

  8. MultiByteToWideChar和WideCharToMultiByte用法详解, ANSI和UNICODE之间的转换

    //========================================================================//TITLE://    MultiByteToW ...

  9. layout_weight 的解释及使用

    layout_weight 的解释及使用 转自:http://my.oschina.net/jsan/blog/191492 在Android的控件布局中,有一个奇葩的 layout_weight 属 ...

  10. 谷歌page speed 安装使用及页面问题详解

    原文地址:http://wenku.baidu.com/view/b0a61f3ebcd126fff7050b40.html 谷歌page speed 安装使用及页面问题详解 谷歌page speed ...