题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少。

解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8(看做无穷)。

考虑已知w=k的答案,推w = k+1时,这时每个区间都将增加一个数,即后n-k个数会增加到这些区间中,容易知道,如果区间内有该数,那么个数不会加1,,即D[i] > k时,结果++,即查询后n-k个数有多少个D[i] > k 的数即为要加上的数,然后最后面还会损失一个区间,损失的是不能增加一个数的那个区间,随着w的增加,该区间会向左边伸展,所以记录一下该区间有多少个不同的数即可。 求区间内有多少个D[i]>k可以用树状数组先求有多少个D[i]<=k(getsum(k)),然后区间长度减一下即可。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define lll __int64
using namespace std;
#define N 1000107 int c[N],n,D[N],Last,vis[N],a[N],pos[N],L[N];
lll sum[N]; int lowbit(int x) { return x&-x; }
void modify(int x,int val)
{
while(x <= n+)
{
c[x]+=val;
x += lowbit(x);
}
} int getsum(int x)
{
int res = ;
while(x > )
{
res += c[x];
x -= lowbit(x);
}
return res;
} int main()
{
int q,i,j,w;
while(scanf("%d",&n)!=EOF && n)
{
for(i=;i<=n;i++)
scanf("%d",&a[i]);
memset(pos,,sizeof(pos));
for(i=;i<=n;i++)
{
int x = a[i];
D[i] = i-pos[x];
if(D[i] == i) D[i] = n+;
pos[x] = i;
}
memset(c,,sizeof(c));
memset(vis,,sizeof(vis));
for(i=;i<=n;i++)
modify(D[i],);
sum[] = n;
Last = ;
vis[a[n]] = ;
for(i=;i<=n;i++)
{
sum[i] = sum[i-]+(n-i+-getsum(i-))-Last;
modify(D[i],-);
if(!vis[a[n-i+]])
{
vis[a[n-i+]] = ;
Last++;
}
}
scanf("%d",&q);
while(q--)
{
scanf("%d",&w);
printf("%I64d\n",sum[w]);
}
}
return ;
}

HDU 4455 Substrings --递推+树状数组优化的更多相关文章

  1. HDOJ 4455 Substrings 递推+树状数组

    pre[i]第i位数往前走多少位碰到和它同样的数 dp[i]表示长度为i的子串,dp[i]能够由dp[i-1]加上从i到n的pre[i]>i-1的数减去最后一段长度为i-1的断中的不同的数得到. ...

  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. HDU 6447 - YJJ's Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 Problem DescriptionYJJ is a salesman who has tra ...

  6. HDU 5862 Counting Intersections(离散化+树状数组)

    HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...

  7. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  8. HDU - 5877 Weak Pair (dfs+树状数组)

    题目链接:Weak Pair 题意: 给出一颗有根树,如果有一对u,v,如果满足u是v的父节点且vec[u]×vec[v]<=k,则称这对结点是虚弱的,问这棵树中有几对虚弱的结点. 题解: 刚开 ...

  9. HDU 6240 Server(2017 CCPC哈尔滨站 K题,01分数规划 + 树状数组优化DP)

    题目链接  2017 CCPC Harbin Problem K 题意  给定若干物品,每个物品可以覆盖一个区间.现在要覆盖区间$[1, t]$. 求选出来的物品的$\frac{∑a_{i}}{∑b_ ...

随机推荐

  1. jquery 拓展

    1. 概述 jquery允许拓展自定义的方法, 绑定到$.fn对象上, 编写一个jQuery插件的原则: 给$.fn绑定函数,实现插件的代码逻辑: 插件函数最后要return this;以支持链式调用 ...

  2. 多准则决策模型-TOPSIS评价方法-源码

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  3. 动态创建JS

    var element=document.createElement('script'); element.setAttribute('src', './js/move.js'); document. ...

  4. 每日微软面试题——day 6(打印所有对称子串)

    每日微软面试题——day 6(打印所有对称子串) 分类: 2.数据结构与算法2011-08-14 14:27 9595人阅读 评论(15) 收藏 举报 面试微软string测试systemdistan ...

  5. 关于C#中泛型类型参数约束(where T : class)

    .NET支持的类型参数约束有以下五种:where T : struct                               | T必须是一个结构类型where T : class       ...

  6. Echarts ecomfe 触摸屏 touch 在IE10下无法显示悬浮框

    问题描述: Windows 8 IE10浏览http://echarts.baidu.com/doc/example/line2.html 时,鼠标放置在数据点上时无法显示悬浮框. 正常情况为: 而现 ...

  7. 极光推送和百度lbs android sdk一起使用使用proguard 混淆的问题

    主要是http得类被混淆后,导致apk定位失败.经过确认,保留apache 的http类就好了 # To enable ProGuard in your project, edit project.p ...

  8. 安卓开发_浅谈OptionsMenus(选项菜单)

    Android平台下所提供的菜单大体上可分为三类:选项菜单.上下文菜单和子菜单. 当Activity在前台运行时,如果用户按下手机上的Menu键,此时就会在屏幕低端弹出相应的选项菜单.但这个功能需要开 ...

  9. UIStoryBoard 中修改控件borderColor

    storyBoard中直接修改UI控件的边框颜色,可以避免先关联再从控制器中修改属性等繁琐操作 因为layer.borderColor隶属于CALayer对象,且CGColor无法直接在sb中直接使用 ...

  10. CocoaPod出现-bash: pod: command not found 解决办法

    从过年来到公司  就不用自己电脑了    之前一直自己带电脑   昨天随便建了一个demo   使用cocoapods  发现     -bash: pod: command not found 刚开 ...