经典颜色问题推荐博文

https://www.cnblogs.com/tyner/p/11519506.html

https://www.cnblogs.com/tyner/p/11616770.html

https://www.cnblogs.com/tyner/p/11620894.html

题意

https://www.luogu.org/problem/P4113

求一段区间中超过出现两次及以上的元素种类

分析

和其他的没啥区别,维护nxt[x], 和nxt[ nxt[x] ]即可, 还是考虑移动左端点对区间答案的影响


#include<cstdio>
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
const int MAX = 2000000+99;
inline int read() {
char ch = getchar(); int f = 1, x = 0;
while(ch<'0' || ch>'9') {if(ch=='-') f = -1; ch = getchar();}
while(ch>='0' && ch<='9') {x = x*10+ch-'0'; ch = getchar();}
return x*f;
} int n,c,m;
int nxt[MAX], lst[MAX], nnxt[MAX];
int arr[MAX], t[MAX]; struct node{
int l, r, id, ans;
}cmd[MAX];
bool cmp1(node a, node bb) { return a.l < bb.l;}
bool cmp2(node a, node bb) { return a.id < bb.id;} void add(int x, int k) {while(x <= n) t[x] += k, x += lowbit(x);}
int query(int x) {
int res = 0;
while(x) {res += t[x], x -= lowbit(x);}
return res;
} void pre() {
n = read(), c = read(), m = read();
for(int i = 1; i <= n; i++) arr[i] = read();
for(int i = n; i >= 1; i--) {
nxt[i] = lst[arr[i]];
lst[arr[i]] = i;
} for(int i = 1; i <= n; i++) nnxt[i] = nxt[nxt[i]];
for(int i = 1; i <= c; i++) if(lst[i] && nxt[lst[i]]) add(nxt[lst[i]], 1);//颜色数为C
//分清n,m
for(int i = 1; i <= m; i++) cmd[i].l=read(), cmd[i].r=read(), cmd[i].id = i;
sort(cmd+1, cmd+1+m, cmp1);
} void solve() {
int pos = 1;
for(int l = 1; l <= n; l++) {
while(cmd[pos].l == l) {
cmd[pos].ans = query(cmd[pos].r)-query(cmd[pos].l-1);
++pos;
}
if(nxt[l]) add(nxt[l], -1);
if(nnxt[l]) add(nnxt[l], 1);
} sort(cmd+1, cmd+1+m, cmp2);
for(int i = 1; i <= m; i++) printf("%d\n", cmd[i].ans);
} int main() {
pre();
solve();
return 0;
}

luoguP4113 [HEOI2012]采花的更多相关文章

  1. BZOJ 2743: [HEOI2012]采花

    2743: [HEOI2012]采花 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 2056  Solved: 1059[Submit][Status ...

  2. [bzoj2743][HEOI2012]采花(树状数组+离线)

    2743: [HEOI2012]采花 Time Limit: 15 Sec  Memory Limit: 128 MBSubmit: 1832  Solved: 954[Submit][Status] ...

  3. BZOJ 2743: [HEOI2012]采花( 离线 + BIT )

    处理出每个数下一个出现的位置, 然后按左端点排序回答询问.处理当前数去除的影响 ------------------------------------------------------------ ...

  4. BZOJ_2743_[HEOI2012]采花_离线+树状数组

    BZOJ_2743_[HEOI2012]采花_离线+树状数组 Description 萧芸斓是Z国的公主,平时的一大爱好是采花.今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花 .花园足够大 ...

  5. BZOJ 2743: [HEOI2012]采花 离线树状数组

    2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...

  6. 【BZOJ2743】[HEOI2012]采花 离线+树状数组

    [BZOJ2743][HEOI2012]采花 Description 萧芸斓是Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了n朵花, ...

  7. cogs:1619. [HEOI2012]采花/luogu P2056

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿是 ...

  8. 1619. [HEOI2012]采花

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比 时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿 ...

  9. cogs1619. [HEOI2012]采花 x

    1619. [HEOI2012]采花 ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比时间限制:5 s   内存限制:128 MB [题目描述] 萧薰儿是 ...

随机推荐

  1. linux驱动——cmdline原理及利用【转】

    转自:https://blog.csdn.net/qingzhuyuxian/article/details/82895416 最近安卓项目中想要获取内核cmdline特定的启动参数,因为我们在他的U ...

  2. 设计模式-Composite(结构型模式) 用于 递归构建 树 状 的组合结构,与Decorator的区别是 Composite旨在通过构造子类而添加新操作,而Decorator直接添加新操作。

    以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Component.h #pragma once class Component { public: Component( ...

  3. SQL Server 移位运算符

    参考链接:http://dataeducation.com/bitmask-handling-part-4-left-shift-and-right-shift/ SQL Server 官网的连接:h ...

  4. linux shell攻略学习笔记二

    1.Cat命令 这么多命令,常用的 Cat –n file  显示文件以及行数 Cat - echo 'Text through stdin' | cat - file.txt Text throug ...

  5. 一些你不知道的css特性【一】

    浏览器禁止用户在标签的style中使用js写入"!important"的特性 我们在使用jQuery设置css的时候 $('#text').css('height', '200px ...

  6. Vue 监听鼠标左键 鼠标右键以及鼠标中键修饰符click.left&contextmenu&click.middle

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. super()方法详解

    目录 一.单独调用父类的方法 二.super() 方法基本概念 2.1 描述 2.2 语法 2.3 单继承使用super() 2.4 多继承使用super() 三.注意事项 四.练习 一.单独调用父类 ...

  8. Linux常用命令之文件编辑命令vim

    vi命令 vi命令是UNIX操作系统和类UNIX操作系统中最通用的全屏幕纯文本编辑器.Linux中的vi编辑器叫vim,它是vi的增强版(vi Improved),与vi编辑器完全兼容,而且实现了很多 ...

  9. String replaceAll(String regex,String str)满足正则表达式的部分替换为给定内容

    package seday02;/*** * String replaceAll(String regex,String str)* @author xingsir*/public class Rep ...

  10. Flask 教程 第三章:Web表单

    本文翻译自 The Flask Mega-Tutorial Part III: Web Forms 这是Flask Mega-Tutorial系列的第三部分,我将告诉你如何使用Web表单. 在第二章中 ...