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, ...
随机推荐
- vue 父子组件的加载顺序
一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...
- 拖拽功能-jquery
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 12scrapy_redis
一.简介 1.redis redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zse ...
- 手动编译openslide
1.下载openslide源代码, 2.转到openslide代码目录: ./configure 3.安装依赖库: sudo apt-get update sudo apt-get install l ...
- 年华利率n%
年化利率12%指的是,在您出借的本金不减少的情况下,您一年后的利息将达到您出借本金的12%.也就是说,如果年化利率是12%,则每月您出借资金获得的利息是1%(12% / 12个月). 在有利网,您的投 ...
- python基础 : 1.计算机基础 2.注释 3.变量 4.标识符 5.输出 6.格式化输出 7.输入 8.算数运算符 9.字符串操作
- 杭电 1069 Monkey and Banana
Description A group of researchers are designing an experiment to test the IQ of a monkey. They will ...
- python接口自动化-发xml格式post请求
前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...
- Leetcode 264.丑数II
丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10 ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...