bzoj 1878 SDOI2009树状数组 离线操作
本来想写2120的,结果想起来了这个
我们先对于询问左端点排序,用树状数组存区间字母个数,对于每种字母,
第一次出现的位置记录为1,剩下的记录为0,然后记录下,每种颜色
后面第一个和他相同颜色的位置
然后扫询问,对于一个询问直接输出区间和,然后假设当前询问是
l1,r1,下一询问是,l2,r2,我们把l1到l2区间内的每个位置颜色的后一颜色
赋值成1,然后继续处理下个询问就好了。
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n, m :longint;
a :array[..] of longint;
l, r :array[..] of longint;
other :array[..] of longint;
last, first :array[..] of longint;
num, ans :array[..] of longint;
c :array[..] of longint;
tot :longint; procedure swap(var a,b:longint);
var
c :longint;
begin
c:=a; a:=b; b:=c;
end; procedure qs(low,high:longint);
var
i, j, x :longint;
begin
i:=low; j:=high; x:=l[(i+j) div ];
while i<j do
begin
while l[i]<x do inc(i);
while l[j]>x do dec(j);
if i<=j then
begin
swap(l[i],l[j]); swap(r[i],r[j]);
swap(num[i],num[j]);
inc(i); dec(j);
end;
end;
if i<high then qs(i,high);
if j>low then qs(low,j);
end; procedure qs1(low,high:longint);
var
i, j, x :longint;
begin
i:=low; j:=high; x:=num[(i+j) div ];
while i<j do
begin
while num[i]<x do inc(i);
while num[j]>x do dec(j);
if i<=j then
begin
swap(l[i],l[j]); swap(r[i],r[j]);
swap(num[i],num[j]); swap(ans[i],ans[j]);
inc(i); dec(j);
end;
end;
if i<high then qs1(i,high);
if j>low then qs1(low,j);
end; procedure init;
var
i :longint;
begin
read(n);
for i:= to n do
begin
read(a[i]);
if tot<a[i] then tot:=a[i];
end; read(m);
for i:= to m do read(l[i],r[i]);
for i:= to m do num[i]:=i;
qs(,m);
for i:= to n do
if last[a[i]]<> then
begin
other[last[a[i]]]:=i;
last[a[i]]:=i;
end else
begin
last[a[i]]:=i;
first[a[i]]:=i;
end;
end; procedure add(x:longint);
begin
while x<=n do
begin
c[x]:=c[x]+;
x:=x+(x and (-x));
end;
end; function ask(x:longint):longint;
begin
ask:=;
while x> do
begin
ask:=ask+c[x];
x:=x-(x and (-x));
end;
end; procedure main;
var
i, j :longint;
ll :longint;
begin
for i:= to tot do if first[i]<> then add(first[i]);
ll:=;
for i:= to m do
begin
for j:=ll to l[i]- do if other[j]<> then add(other[j]);
ans[i]:=ask(r[i])-ask(l[i]-);
ll:=l[i];
end;
qs1(,m);
for i:= to m do writeln(ans[i]);
end; begin
init;
main;
end.
bzoj 1878 SDOI2009树状数组 离线操作的更多相关文章
- HDU3874Necklace(树状数组+离线操作)
此题的大意思说有一串珠子,每个珠子都有自己的欣赏值value,现在给你一串珠子每个的欣赏值,并给出一些询问,查询某个区间内部总欣赏值是多少,但是有一个约定就是如果这个区间内部有两个珠子的欣赏值是一样的 ...
- HDU---4417Super Mario 树状数组 离线操作
题意:给定 n个数,查询 位置L R内 小于x的数有多少个. 对于某一次查询 把所有比x小的数 ”的位置“ 都加入到树状数组中,然后sum(R)-sum(L-1)就是答案,q次查询就要离线操作了,按高 ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- Codeforces 369E Valera and Queries --树状数组+离线操作
题意:给一些线段,然后给m个查询,每次查询都给出一些点,问有多少条线段包含这个点集中的一个或多个点 解法:直接离线以点为基准和以线段为基准都不好处理,“正难则反”,我们试着求有多少线段是不包含某个查询 ...
- BZOJ 2141 排队(树状数组套主席树)
解法很多的题,可以块套树状数组,可以线段树套平衡树.我用的是树状数组套主席树. 题意:给出一段数列,m次操作,每次操作是交换两个位置的数,求每次操作后的逆序对数.(n,m<=2e4). 对于没有 ...
- hdu 3333(树状数组 + 离线操作)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- bzoj 4785: [Zjoi2017]树状数组【树套树】
参考:https://www.cnblogs.com/ljh2000-jump/p/6686960.html 由于操作反过来了,所以显然树状数组维护后缀和,所以本来想查询(1,r)-(1,l-1),现 ...
- BZOJ 3262(Treap+树状数组)
题面 传送门 分析 分三维考虑 对第一维,直接排序 对第二维和第三维,我们这样考虑 朴素的方法是建k棵Treap,第i棵Treap里存第二维值为k的第三维数值 每次查询一组(a,b,c),只要在1~b ...
- BZOJ 4765(分块+树状数组)
题面 传送门 "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些 .普通计算机能计算数列区间和,而普通计算姬能计算树中子树和.更 ...
随机推荐
- 适用于Linux的windows子系统
Windows基于图形界面的易用性是有目共睹的,这也是很多普通用户往往难以舍弃的原因.但是Linux系统更强大的网络应用开发能力,却又是Windows系统所无法比拟的.一直以来,很多人都在试图采用各种 ...
- LeetCode 81——搜索旋转排序数组 II
1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...
- HDU 3698 Let the light guide us(DP+线段树)(2010 Asia Fuzhou Regional Contest)
Description Plain of despair was once an ancient battlefield where those brave spirits had rested in ...
- C++STL——set
一.相关定义 set 集合,有唯一性,即每一个元素只有一个: 是一个有序的容器,里面的元素都是排序好的: 支持插入,删除,查找等操作. 注意 set中的元素可以是任意类型的,但是由于需要排序,所以元素 ...
- .aspx文件和.aspx.cs文件的区别与联系
http://zhidao.baidu.com/link?url=_SNw0EHJ8Wg__KanJrKQM3tVEUeFnVilZ6GGIN8ab69RLuyOWD__WyZb7Zb9dJjwDnL ...
- MvcMusicStore学习中常出现的一个BUG
BUG描述:var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genre); 前 ...
- PAT 甲级 1036 Boys vs Girls(20)
https://pintia.cn/problem-sets/994805342720868352/problems/994805453203030016 This time you are aske ...
- maven中进行go的编译
maven提供的插件maven-antrun-plugin真是个好东东,使得maven可以利用ant的很多功能. 最近需要实现在maven中实现对go代码的编译,添加如下代码在pom文件中即可. &l ...
- SRM708 div1 PalindromicSubseq(动态规划+容斥原理)
题目大意:给定一个字符串,记X[i]为包含s[i]这个字符的所有子列是回文串的个数(注意是子列而不是子串),求出所有的X[i]*(i+1),然后异或起来作为返回结果 题解: 首先用容斥来想,如果当前枚 ...
- VS查看DLL接口
应用程序Microsoft Visual Studio 2010的Visual Studio Tools文件夹中打开Visual Studio Command Prompt (2010)命令窗口 du ...