BZOJ 2743: [HEOI2012]采花 [树状数组 | 主席树]
题意:
查询区间中出现次数$>2$的颜色个数
一眼主席树,区间中$l \le last[i] \le r$的个数减去$l \le last[last[i]] \le r$的个数,搞两颗主席树来做
然后就T了
因为bzoj上数据是1e6....
还是离线树状数组吧....
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lc(x) t[x].l
#define rc(x) t[x].r
const int N=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n, k, Q, a[N], last[N], pos[N], l, r;
struct ChairTree{
struct meow{int l, r, sum;} t[N*];
int sz, root[N];
void ins(int &x, int l, int r, int p) {
t[++sz] = t[x]; x=sz;
t[x].sum++;
if(l==r) return;
int mid = (l+r)>>;
if(p<=mid) ins(t[x].l, l, mid, p);
else ins(t[x].r, mid+, r, p);
}
int que(int x, int y, int l, int r, int ql, int qr) {
if(ql<=l && r<=qr) return t[y].sum - t[x].sum;
else {
int mid=(l+r)>>, ans=;
if(ql<=mid) ans += que(lc(x), lc(y), l, mid, ql, qr);
if(mid<qr) ans += que(rc(x), rc(y), mid+, r, ql, qr);
return ans;
}
}
}C1, C2;
int main() {
freopen("in","r",stdin);
n=read(); k=read(); Q=read();
for(int i=; i<=n; i++) a[i]=read(), last[i] = pos[a[i]], pos[a[i]] = i;
for(int i=; i<=n; i++) {
C1.root[i] = C1.root[i-], C1.ins(C1.root[i], , n, last[i]);
C2.root[i] = C2.root[i-], C2.ins(C2.root[i], , n, last[last[i]]);
}
for(int i=; i<=Q; i++)
l=read(), r=read(),
printf("%d\n", C1.que(C1.root[l-], C1.root[r], , n, l, r) - C2.que(C2.root[l-], C2.root[r], , n, l, r) );
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define lc(x) t[x].l
#define rc(x) t[x].r
const int N=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
} int n, k, Q, a[N], last[N], pos[N], l, r;
int c[N];
inline void add(int p, int v) {if(p!=) for(;p<=n;p+=(p&-p)) c[p]+=v;}
inline int sum(int p) {int ans=; for(;p;p-=(p&-p)) ans+=c[p]; return ans;}
struct meow{
int l, r, qid;
bool operator <(const meow &a) const {return r<a.r;}
}q[N];
int ans[N], now;
int main() {
freopen("in","r",stdin);
n=read(); k=read(); Q=read();
for(int i=; i<=n; i++) a[i]=read();
for(int i=; i<=Q; i++) l=read(), r=read(), q[i]=(meow){l, r, i};
sort(q+, q++Q);
now = ;
for(int i=; i<=n; i++) {
last[i] = pos[a[i]]; pos[a[i]] = i;
add(last[i], ); add(last[last[i]], -);
while(q[now].r == i) ans[q[now].qid] = sum(i) - sum(q[now].l-), now++;
}
for(int i=; i<=Q; i++) printf("%d\n", ans[i]);
}
BZOJ 2743: [HEOI2012]采花 [树状数组 | 主席树]的更多相关文章
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树
BZOJ_1901_Zju2112 Dynamic Rankings_树状数组+主席树 题意: 给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i, ...
- 【bzoj1146】[CTSC2008]网络管理Network 倍增LCA+dfs序+树状数组+主席树
题目描述 M公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通信网络.该网络的结构由N个路由器和N-1条高 ...
- 【bzoj3744】Gty的妹子序列 分块+树状数组+主席树
题目描述 我早已习惯你不在身边, 人间四月天 寂寞断了弦. 回望身后蓝天, 跟再见说再见…… 某天,蒟蒻Autumn发现了从 Gty的妹子树(bzoj3720) 上掉落下来了许多妹子,他发现 她们排成 ...
- BZOJ_2120_数颜色_Set+树状数组+主席树
BZOJ_2120_数颜色_Set+树状数组+主席树 Description 墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会像你发布如下指令: 1. Q L ...
- P1972 [SDOI2009]HH的项链[离线+树状数组/主席树/分块/模拟]
题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的贝壳会带来好运,所以每次散步完后,他都会随意取出一段贝壳,思考它们所表达的含义.HH 不断地收集新的贝壳,因此,他的项链 ...
- BZOJ 2743: [HEOI2012]采花 离线树状数组
2743: [HEOI2012]采花 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=2743 Description 萧芸斓是Z国的公主, ...
- BZOJ 2743: [HEOI2012]采花
2743: [HEOI2012]采花 Time Limit: 15 Sec Memory Limit: 128 MBSubmit: 2056 Solved: 1059[Submit][Status ...
- BZOJ 2743: [HEOI2012]采花( 离线 + BIT )
处理出每个数下一个出现的位置, 然后按左端点排序回答询问.处理当前数去除的影响 ------------------------------------------------------------ ...
随机推荐
- hbmy周赛1--A
Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...
- [国嵌攻略][054][NandFlash驱动设计_写]
Nand Flash支持按页写和随机写两种方式,在下面实现的是按页写.闪存在写数据时,只能写入1,不能写入0,所以写函数必须和擦除函数一起使用,并且擦除函数是按块擦除. /************** ...
- 人生苦短,我用Python 第一篇
一.变量名 自定义变量名只能用 数字.字母.下划线组成. 使用变量前,需对其先赋值 注意:1.变量名不能以数字开头: 2.不能是关键字:例如:'and', 'as', 'assert', ' ...
- 使用Git将本地项目或代码上传到GitHub上
1.要托管到github,那你就应该要有一个属于你自己的github帐号,所以你应该先到github.com注册.打开浏览器在地址栏输入地址:github.com 填写用户名.邮箱.密码,点击Sign ...
- pylint python2.7 安装记录
环境:python 2.7.11 ; 源码安装:configparser 3.5.0; 如果出现以下error,请用源码更新configparser至3.5.0版本 具体error没有抓下来,主要是报 ...
- 一个简洁的PHP可逆加密函数(分享)
http://www.jb51.net/article/38018.htm 本篇文章是对一个简洁的PHP可逆加密函数进行了详细的分析介绍,需要的朋友参考下 很多时候我们需要对数据进行加密解密,比如 ...
- 字符串API
string可以看成是多个字符组成的只读数组,也可以通过下标去访问某个字符 访问i位置的字符 : str[i] 字符个数: str.length 倒数第n个字符 : str[str.length- ...
- iOS enum 定义与使用
枚举其实很重要,特别是在应用开发初期,服务器端数据格式需要更改得情况下,枚举和宏都能是程序简洁,并且改动小. 网上有个人写的言简意赅,适合初学 转自:http://blog.csdn.net/ysy4 ...
- JAVA异步加回调的例子
package com.sunchao.callback; /** * callback interface * @author Administrator * */ public interface ...
- [知了堂学习笔记]_Jquery_Validate 表单校验的使用
一.效果图: 二.JqueryValidate的好处 在做注册.或者类似以上的表单提交的时候,大家是不是都很烦那种,把数据拿到后台去判断, 可能经过了正则表达式之类的复杂判断,然后发现数据错误.接着通 ...