http://www.cnblogs.com/icode-girl/p/5783983.html

要注意卡片没有都被使用的情况。

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 100010
int K,n,a[N],nex[N],now[N],m,anss[N];
bool vis[N];
struct ASK{int l,r,p;}Q[N];
int num[N];
bool operator < (const ASK &a,const ASK &b)
{return num[a.l]!=num[b.l] ? num[a.l]<num[b.l] : a.r<b.r;}
void makeblock()
{
int sz=sqrt(n),sum=1; if(!sz) sz=1;
for(;sum*sz<n;++sum)
{
int r=sum*sz;
for(int i=sz*(sum-1)+1;i<=r;++i)
num[i]=sum;
}
for(int i=sz*(sum-1)+1;i<=n;++i)
num[i]=sum;
}
int T[N],ans;
void Insert(int x)
{
if(!T[x]) ++ans;
++T[x];
}
void Delete(int x)
{
--T[x];
if(!T[x]) --ans;
}
int main()
{
//freopen("c.in","r",stdin);
scanf("%d%d",&K,&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
int k=0;
for(int i=1;i<=n;++i)
if(!vis[a[i]])
{
++k;
vis[a[i]]=1;
printf("%d%c",a[i],k==K ? '\n' : ' ');
}
for(int i=1;i<=K;++i)
if(!vis[i])
{
++k;
printf("%d%c",i,k==K ? '\n' : ' ');
}
for(int i=n;i>=1;--i)
{
nex[i]=now[a[i]];
now[a[i]]=i;
}
for(int i=1;i<=n;++i)
if(nex[i]>i+1)
{
Q[++m].l=i+1;
Q[m].r=nex[i]-1;
Q[m].p=i;
}
else if(nex[i]==i+1)
anss[i]=0;
else
anss[i]=K-1;
makeblock();
sort(Q+1,Q+m+1);
for(int i=Q[1].l;i<=Q[1].r;++i) Insert(a[i]);
anss[Q[1].p]=ans;
for(int i=2;i<=m;++i)
{
if(Q[i].l<Q[i-1].l){for(int j=Q[i-1].l-1;j>=Q[i].l;--j)Insert(a[j]);}
else{for(int j=Q[i-1].l;j<Q[i].l;++j)Delete(a[j]);}
if(Q[i].r<Q[i-1].r){for(int j=Q[i-1].r;j>Q[i].r;--j)Delete(a[j]);}
else{for(int j=Q[i-1].r+1;j<=Q[i].r;++j)Insert(a[j]);}
anss[Q[i].p]=ans;
}
for(int i=1;i<=n;++i)
printf("%d\n",anss[i]);
return 0;
}

【莫队算法】URAL - 2080 - Wallet的更多相关文章

  1. NBUT 1457 莫队算法 离散化

    Sona Time Limit:5000MS     Memory Limit:65535KB     64bit IO Format: Submit Status Practice NBUT 145 ...

  2. BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】

    2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 7687  Solved: 3516[Subm ...

  3. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

  4. Codeforces617 E . XOR and Favorite Number(莫队算法)

    XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...

  5. Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...

  6. 【BZOJ-3052】糖果公园 树上带修莫队算法

    3052: [wc2013]糖果公园 Time Limit: 200 Sec  Memory Limit: 512 MBSubmit: 883  Solved: 419[Submit][Status] ...

  7. 莫队算法 2038: [2009国家集训队]小Z的袜子(hose)

    链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 ...

  8. Codeforces 617E XOR and Favorite Number(莫队算法)

    题目大概说给一个序列,多次询问区间异或和为k的连续子序列有多少个. 莫队算法,利用异或的性质,通过前缀和求区间和,先处理出序列各个前缀和,然后每次区间转移时维护i以及i-1前缀和为某数的个数并增加或减 ...

  9. 信心题--FUOJ2226(莫队算法)

    http://acm.fzu.edu.cn/problem.php?pid=2226 信心题,还说是信心题,题目给的真好.但是一点都不像信心题. 又是一个新的算法,莫队算法 莫队算法是一个用数组就可以 ...

  10. [BZOJ2038]小Z的袜子(莫队算法)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2038 分析:莫队算法 莫队算法是一种思想…… 处理问题:不带修改的区间询问 使用要求:[l-1 ...

随机推荐

  1. CentOS 7, 升级python到3.x

    By francis_hao    Apr 11,2017 使用源码安装方式 首先到官网https://www.python.org/downloads/source/ 下载python最新版本.当前 ...

  2. Codeforces 931.D Peculiar apple-tree

    D. Peculiar apple-tree time limit per test 1 second memory limit per test 256 megabytes input standa ...

  3. [codeforces gym Matrix God]随机矩阵乘法

    题目链接:http://codeforces.com/gym/101341/problem/I 随机真是一个神奇的方法.原本矩阵乘法是n^3的复杂度,但是这个题是让判断两个矩阵是否相等,只需要在两个矩 ...

  4. P值

    https://baike.baidu.com/item/P%E5%80%BC/7083622?fr=aladdin https://baijiahao.baidu.com/s?id=15960976 ...

  5. CSS选择器及CSS3新增选择器

    转自:http://www.cnblogs.com/libingql/p/4375354.html 1. CSS1定义的选择器 选择器 类型 说明 E 类型选择器 选择指定类型的元素 E#id ID选 ...

  6. 关于fragment点击能穿透问题

    本人在做项目的过程中遇到的这个问题,然后就在网上百度了一下,之后也是在csdn上看到博友发过此类问题的解决办法,所以特此重新总结一下,顺便也给自己提个醒,避免出现此类问题.好!下面我们说一下问题: 举 ...

  7. 基于js的地理数据的几何运算turfjs

    Doc: http://turfjs.org/static/docs/global.html Openlayers3 Sample: http://jsfiddle.net/d6o81vc7/

  8. 【洛谷 P1337】[JSOI2004]平衡点 / 吊打XXX (模拟退火)

    题目链接 正解就算了吧,谁叫我理生化 语数外 政史地都菜呢 模拟退火真玄学,不知道发生了什么就跑出答案了,原理就算了吧,能用(pianfen)就好. 当重物平衡时,势能一定是最小的,于是当我随机出一个 ...

  9. 之江学院第0届校赛 qwb与支教 (容斥公式)

    description qwb同时也是是之江学院的志愿者,暑期要前往周边地区支教,为了提高小学生的数学水平.她把小学生排成一排,从左至右从1开始依次往上报数. 玩完一轮后,他发现这个游戏太简单了.于是 ...

  10. HDU1174(空间点到直线的距离,用叉积)

    爆头 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...