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

Problem Description
XXX has an array of length n. XXX wants to know that, for a given w, what is the sum of the distinct elements’ number in all substrings of length w. For example, the array is { 1 1 2 3 4 4 5 } When w = 3, there are five substrings of length 3. They are (1,1,2),(1,2,3),(2,3,4),(3,4,4),(4,4,5)
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
 

Input
There are several test cases.
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
 

Output
For each test case, your program should output exactly Q lines, the sum of the distinct number in all substrings of length w for each query.
 

Sample Input

7
1 1 2 3 4 4 5
3
1
2
3
0
 

Sample Output

7
10
12
 

Source
 
/* ***********************************************
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 递推+树状数组的更多相关文章

  1. HDU 4455 Substrings --递推+树状数组优化

    题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...

  2. ACM学习历程—UESTC 1217 The Battle of Chibi(递推 && 树状数组)(2015CCPC C)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1217 题目大意就是求一个序列里面长度为m的递增子序列的个数. 首先可以列出一个递推式p(len, i) =  ...

  3. 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 ...

  4. UVA 12446 How Many... in 3D! ( 递推 + 树状数组 )

    C. How Many... in 3D! Time Limit: 1000ms Memory Limit: 131072KB 64-bit integer IO format: %lld      ...

  5. HDOJ 5419 Victor and Toys 树状数组

    分母是一定的C(m,3) 树状数组求每一个数能够在那些段中出现,若x出如今了s段中,分子加上w[x]*C(s,3) Victor and Toys Time Limit: 2000/1000 MS ( ...

  6. hdoj 1166 敌兵布阵(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166 思路分析:该问题为动态连续和查询问题,使用数组数组可以解决:也可使用线段树解决该问题: 代码如下 ...

  7. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

  8. 【HDOJ 5654】 xiaoxin and his watermelon candy(离线+树状数组)

    pid=5654">[HDOJ 5654] xiaoxin and his watermelon candy(离线+树状数组) xiaoxin and his watermelon c ...

  9. 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵

    题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...

随机推荐

  1. 10MongoDB

    一.  介绍MongoDB 1. NoSQL 1)“NoSQL”⼀词最早于1998年被⽤于⼀个轻量级的关系数据库的名字 随着web2.0的快速发展, NoSQL概念在2009年被提了出来 2)NoSQ ...

  2. Navicat Premium 12破解(转载)

    一.Navicat Premium 12下载 Navicat Premium 12是一套数据库开发管理工具,支持连接 MySQL.Oracle等多种数据库,可以快速轻松地创建.管理和维护数据库. Na ...

  3. EPT和VPID简介

    EPT(Extended Page Tables,扩展页表),属于Intel的第二代硬件虚拟化技术,它是针对内存管理单元(MMU)的虚拟化扩展.EPT降低了内存虚拟化的难度(与影子页表相比),也提升了 ...

  4. 1284-Primitive Roots,学信安的路过

                                                      Primitive Roots 此题通过率如此之高,料想不会很难,但是再简单小菜还是不会.. 嗯,下 ...

  5. POJ 2513 无向欧拉通路+字典树+并查集

    题目大意: 有一堆头尾均有颜色的木条,要让它们拼接在一起,拼接处颜色要保证相同,问是否能够实现 这道题我一开始利用map<string,int>来对颜色进行赋值,好进行后面的并查操作以及欧 ...

  6. 洛谷P1244 青蛙过河

    P1244 青蛙过河 362通过 525提交 题目提供者该用户不存在 标签 难度普及- 时空限制1s / 128MB 提交 讨论 题解 最新讨论更多讨论 题目什么意思 题目看不懂啊 题目描述 有一条河 ...

  7. java Web项目Service层通用接口和entityVo对象与entity对象转化问题的解决方案

    Service层的接口中有一些比较常用方法,一次又一次的在新的Service层中被书写,所以懒惰的程序员又烦了,他们决定写个通用接口来解决这个问题. 有些项目中,实体类即承担接收表单数据的任务,又承担 ...

  8. [NOIP1998] 提高组 洛谷P1012 拼数

    题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整数13,312,343联接成的最大整数为:34331213 又如:n=4时,4个整数7,13,4 ...

  9. 【POJ2406】Power Strings(KMP,后缀数组)

    题意: n<=1000000,cas较大 思路:这是一道论文题 后缀数组已弃疗,强行需要DC3构造,懒得(不会)写 ..]of longint; n,m,i,j,len,ans,st:longi ...

  10. flex里InputText不能输入中文

    最近做项目都没做任何的更新,今天突然遇到在flex里的InputText无法进行中文输入,晚上查找了下资料,很多原因说是flashplayer的一个BUG. 在网上找到两种解决办法: 1.会出现这种情 ...