HDOJ 4455 Substrings 递推+树状数组
pre[i]第i位数往前走多少位碰到和它同样的数
dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到....
爆int+有点卡内存....
Substrings
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2300 Accepted Submission(s): 716
The distinct elements’ number of those five substrings are 2,3,3,2,2.
So the sum of the distinct elements’ number should be 2+3+3+2+2 = 12
Each test case starts with a positive integer n, the array length. The next line consists of n integers a1,a2…an, representing the elements of the array.
Then there is a line with an integer Q, the number of queries. At last Q lines follow, each contains one integer w, the substring length of query. The input data ends with n = 0 For all cases, 0<w<=n<=106, 0<=Q<=104, 0<= a1,a2…an <=106
1 1 2 3 4 4 5
3
1
2
3
0
10
12
/* ***********************************************
Author :CKboss
Created Time :2015年08月17日 星期一 22时06分06秒
File Name :HDOJ4455.cpp
************************************************ */ #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map> using namespace std; typedef long long int LL;
const int maxn=1001000; int n;
LL dp[maxn];
int a[maxn];
int pre[maxn];
int spre[maxn];
int wz[maxn];
bool vis[maxn]; /*************BIT*********************/ inline int lowbit(int x) { return x&(-x); } int tree[maxn]; void add(int p,int v)
{
for(int i=p;i<maxn;i+=lowbit(i))
tree[i]+=v;
} LL sum(int p)
{
LL ret=0;
for(int i=p;i;i-=lowbit(i)) ret+=tree[i];
return ret;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); while(scanf("%d",&n)!=EOF&&n)
{
for(int i=1;i<=n;i++) scanf("%d",a+i); memset(wz,-1,sizeof(wz));
memset(pre,0,sizeof(pre));
memset(spre,0,sizeof(spre));
memset(tree,0,sizeof(tree));
memset(vis,false,sizeof(vis)); for(int i=n;i>=1;i--)
{
int x=a[i];
if(wz[x]==-1) wz[x]=i;
else
{
spre[wz[x]-i]++;
pre[wz[x]]=wz[x]-i;
wz[x]=i;
}
} int zero=0,nozero;
for(int i=1;i<=n;i++)
{
if(pre[i]==0) zero++;
if(spre[i]) add(i,spre[i]);
}
int ed=n;
int siz=0;
nozero=n-zero;
dp[1]=n; zero--; for(int i=2;i<=n;i++)
{
if(vis[a[ed]]==false)
{
vis[a[ed]]=true;
siz++;
}
ed--;
int B=siz;
int A=zero+nozero-sum(i-1);
dp[i]=dp[i-1]+A-B;
if(pre[i]) nozero--,add(pre[i],-1);
else zero--;
}
int Q;
scanf("%d",&Q);
while(Q--)
{
int x;
scanf("%d",&x);
printf("%lld\n",dp[x]);
}
} return 0;
}
HDOJ 4455 Substrings 递推+树状数组的更多相关文章
- HDU 4455 Substrings --递推+树状数组优化
题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...
- ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) = ...
- Code Chef JUMP(递推+树状数组+李超线段树)
\(JUMP\) 很容易写出转移柿子 \[f_i=\min_{p_j<p_i}\{(h_i-h_j)^2+f_j\}+w_i\] 把\(\min\)里面的东西展开一下 \[f_j=\min_{p ...
- UVA 12446 How Many... in 3D! ( 递推 + 树状数组 )
C. How Many... in 3D! Time Limit: 1000ms Memory Limit: 131072KB 64-bit integer IO format: %lld ...
- HDOJ 5419 Victor and Toys 树状数组
分母是一定的C(m,3) 树状数组求每一个数能够在那些段中出现,若x出如今了s段中,分子加上w[x]*C(s,3) Victor and Toys Time Limit: 2000/1000 MS ( ...
- hdoj 1166 敌兵布阵(树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...
- HDOJ 4417 - Super Mario 线段树or树状数组离线处理..
题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...
- 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)
pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...
- 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵
题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...
随机推荐
- C++11程序设计要点总结-模板机制详解
C++程序设计要点总结 在编程的过程中呢我们总会遇到一些各种各样的问题,就比如在写方法的时候,我们一个同样的方法要写好几种类型的呢,这让我们很伤脑筋,但是呢C++有一个强大的功能就是模板机制,这个模板 ...
- [LUOGU] P2704 炮兵阵地
题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队. 一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示), 也可能是平原(用"P&q ...
- mysql恢复数据
1.崩溃恢复: 突然断电.宕机,导致mysql无法正常启动: (1) 关闭数据库. (2) Vim /etc/my.cnf 添加:innodb_force_recovery=1 默认为0. 1( ...
- 利用system-config-kickstart实现半自动化安装
老司机开车了… 上车请坐稳… centos7系统 首先确认已经安装了system-config-kickstart包,如果没有安装就yum install system-config-kickstar ...
- 实现验证的vsftpd虚拟用户
实现基于文件验证的vsftpd虚拟用户--(一台) 一.创建用户数据库文件 vim /etc/vsftpd/vuser cd /etc/vsftpd/ db_load -T -t hash -f vu ...
- python全套视频十五期(116G)
python全套视频,第十五期,从入门到精通,基础班,就业班,面试,软件包 所属网站分类: 资源下载 > python视频教程 作者:精灵 链接:http://www.pythonheidong ...
- Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33
06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...
- Android滚动页面位置指示器:CircleIndicator
Android滚动页面位置指示器:CircleIndicator CircleIndicator是github上的一个开源的用于页面滚动时候的位置指示器,指示当前页面在总的页面中的位置和前后位置 ...
- 【Ts 2】Nginx服务器搭建
在项目中,首先是需要Nginx服务器作为一个图片服务器来使用.那么,久涉及到服务器的搭建.这次服务器的搭建,主要是在三个环境上进行了学习:CentOS6.2,CentOS7,和Ubuntu16.那么本 ...
- ms sqlserver数据库建索引
索引分类:从物理结构上可分为两种:聚集索引和非聚集索引 (此外还有空间索引.筛选索引.XML索引) 因为聚集索引是索引顺序与物理存储顺序一致,所以只能建一个. 聚集索引就是把数据按主键顺序存储: 因为 ...