poj 3378 Crazy Thairs dp+线段树+大数
题目链接
题目大意: 给出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+线段树+大数的更多相关文章
- [POJ 3378] Crazy Thairs
Link: POJ 3378 传送门 Solution: 按序列长度$dp$, 设$dp[i][j]$为到第$i$个数,符合要求的序列长度为$j$时的序列个数, 易得转移方程:$dp[i][j]=\s ...
- POJ 3378 Crazy Thairs(树状数组+DP)
[题目链接] http://poj.org/problem?id=3378 [题目大意] 给出一个序列,求序列中长度等于5的LIS数量. [题解] 我们发现对于每个数长度为k的LIS有dp[k][i] ...
- ●POJ 3378 Crazy Thairs
题链: http://poj.org/problem?id=3378 题解: 树状数组维护,高精度. 依次考虑以每个位置结尾可以造成的贡献. 假设当前位置为i,为了达到5个元素的要求,我们需要求出,在 ...
- ZOJ 3349 Special Subsequence 简单DP + 线段树
同 HDU 2836 只不过改成了求最长子串. DP+线段树单点修改+区间查最值. #include <cstdio> #include <cstring> #include ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- cf834D(dp+线段树区间最值,区间更新)
题目链接: http://codeforces.com/contest/834/problem/D 题意: 每个数字代表一种颜色, 一个区间的美丽度为其中颜色的种数, 给出一个有 n 个元素的数组, ...
- Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树)
Codeforces Round #620 F2. Animal Observation (hard version) (dp + 线段树) 题目链接 题意 给定一个nm的矩阵,每行取2k的矩阵,求总 ...
- [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)
树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...
随机推荐
- OOP组合和继续的优缺点
—— 详解继承与组合的优缺点 组合与继承都是提高代码可重用性的手段.在设计对象模型时,可以按照语义来识别类之间的组合关系和继承关系.在有些情况下,采用组合关系或者继承关系能完成同样的任务,组合和继 ...
- linux下socket编程-UDP
下面是UDP的服务器的代码: /* server.c */ #include <stdio.h> #include <string.h> #include <netine ...
- CentOS上编译安装Git
1. 安装(编译安装)软件 # 先安装git依赖的包 yum install zlib-devel yum install openssl-devel yum install perl yum ins ...
- In Depth : Android Boot Sequence / Process
In Depth : Android Boot Sequence / Process What happened when I press power on button in my Android ...
- Datagridview控件实现分页功能
可以进行sql语句进行设置: 1.先新建一个窗体,一个DataGridView控件.两个label控件.两个Button控件 2.代码如下: using System; using Sy ...
- Mirantis Fuel fundations
Mirantis Nailgun is the most important service a RESTful application written in Python that contains ...
- #include <locale.h> #include <locale>
C C++ C 1 setlocale setlocale,本函数用来配置地域的信息,设置当前程序使用的本地化信息. #include <stdio.h> #include <std ...
- 关于nodejs,request模块的一个bug
今天在使用request时发生了一个错误, 对方网站的证书设置的不正确导致本地请求不能返回数据: 解决方案是在配置request时加入一个忽略证书验证得字段: 具体代码如下 request.post( ...
- FieldInfo.IsSpecialName Property【转】
Gets a value indicating whether the corresponding SpecialName attribute is set in the FieldAttribute ...
- iSCSI存储系统知识
一.概述 SCSI 即小型计算机系统接口(Small Computer System Interface:简写:SCSI),一种用于计算机和外部设备之间(硬盘.光驱.软驱.打印机等)系统级接口的独立处 ...