bzoj 2743 树状数组离线查询
我们按照询问的右端点排序,然后对于每一个位置,记录同颜色
上一个出现的位置,每次将上上位置出现的+1,上次出现的-1,然后
用树状数组维护就好了
/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/
//By BLADEVIL
var
n, m, t, l :longint;
pre, other, num :array[..] of longint;
last, ans, first, a, pred, c :array[..] of longint;
procedure connect(x,y,tot:longint);
begin
inc(l);
pre[l]:=last[x];
last[x]:=l;
other[l]:=y;
num[l]:=tot;
end;
procedure add(x,y:longint);
begin
while (x<=n) do
begin
inc(c[x],y);
x:=x+(x and -x);
end;
end;
function ask(x:longint):longint;
var
sum :longint;
begin
sum:=;
while x> do
begin
sum:=sum+c[x];
x:=x-(x and -x);
end;
exit(sum);
end;
procedure init;
var
i, j :longint;
x, y :longint;
begin
readln(n,t,m);
for i:= to n do
begin
read(a[i]);
if first[a[i]]<> then pred[i]:=first[a[i]];
first[a[i]]:=i;
end;
for i:= to m do
begin
readln(x,y);
connect(y,x,i);
end;
end;
procedure main;
var
i :longint;
q, p :longint;
begin
for i:= to n do
begin
add(pred[pred[i]]+,);
add(pred[i]+,-);
q:=last[i];
while q<> do
begin
p:=other[q];
ans[num[q]]:=ask(p);
q:=pre[q];
end;
end;
for i:= to m do writeln(ans[i]);
end;
begin
init;
main;
end.
bzoj 2743 树状数组离线查询的更多相关文章
- HDU 4630 No Pain No Game 树状数组+离线查询
思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...
- 【树状数组+离线查询】HDU 3333 Turing Tree
https://www.bnuoj.com/v3/contest_show.php?cid=9149#problem/H [题意] 给定一个数组,查询任意区间内不同数字之和. (n<=30000 ...
- HDU 3333 树状数组离线查询
题目大意: 询问区间内不同种类的数的数值之和 这里逐个添加最后在线查询,会因为相同的数在区间内导致冲突 我们总是希望之后添加的数不会影响前面,那么我们就在添加到第i个数的时候,把所有在1~i 的区间的 ...
- BZOJ 2743 树状数组
不能用分块. #include <bits/stdc++.h> using namespace std; ; struct Info{int l,r,Id;}Q[Maxn]; int a[ ...
- SPOJ DQUERY树状数组离线or主席树
D-query Time Limit: 227MS Memory Limit: 1572864KB 64bit IO Format: %lld & %llu Submit Status ...
- D-query SPOJ 树状数组+离线
D-query SPOJ 树状数组+离线/莫队算法 题意 有一串正数,求一定区间中有多少个不同的数 解题思路--树状数组 说明一下,树状数组开始全部是零. 首先,我们存下所有需要查询的区间,然后根据右 ...
- Necklace HDU - 3874 (线段树/树状数组 + 离线处理)
Necklace HDU - 3874 Mery has a beautiful necklace. The necklace is made up of N magic balls. Each b ...
- 2016 Multi-University Training Contest 5 1012 World is Exploding 树状数组+离线化
http://acm.hdu.edu.cn/showproblem.php?pid=5792 1012 World is Exploding 题意:选四个数,满足a<b and A[a]< ...
- BZOJ 4999: This Problem Is Too Simple! DFS序+LCA+树状数组+离线
Code: #include<bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) , ...
随机推荐
- NotePad++相关设置
Notepad++去掉红色下划线: 插件->DSpellCheck->Auto-check Document 前面的勾去掉 Notepad++自动换行: 视图(View)——>自动换 ...
- Check Box Select/Deselect All on Grid
The below function is to be used on a grid with multiple check boxes. Place the code behind a FieldC ...
- C# DataGridViewComboBoxColumn 数据绑定
dataGridView1.Columns.Clear(); dataGridView1.AutoGenerateColumns = false; dataGridView1.DataSource = ...
- mariadb的explain分析及InnoDB存储引擎
id: 当前查询语句中,每个SELECT语句的编号, id: 1 表示简单类型的查询 复杂类型的查询有三种:简单子查询,用于FROM中的子查询,联合查询:UNION 注意:UNION查询的分 ...
- 批量删除的js代码
<script type="text/javascript"> function seltAll(){ var chckBoxSign = document.getEl ...
- 1)Java JDK和JRE
>JRE : Java Runtime Enviroment Java的运行环境.面向Java程序的使用者,而不是开发者.如果你仅下载并安装了JRE,那么你的系统只能运行Java程序(不能 ...
- xml文件对应的DTD学习
DTD文件: 1.DTD文档主要由(元素,属性,实体,PCDATA,CDATA) 2.声明一个元素:<!ELEMENT 元素名称 (元素内容)> eg: <!ELEMENT pers ...
- nexcel 读取 excel
procedure TfrmUserV3.ImportUser(const AFileName: string); var book :IXLSWorkBook; ws : IXLSWorkSheet ...
- mysql 1093 错误
1093错误: 要更新某表,同时该表有字段值又来自该表的查询语句. 例如: INSERT INTO m_bulletincategory ( OrganizationKey , CategoryNam ...
- 9.css背景
这节也是关于盒模型的扩展,而我个人喜欢把盒模型想象成图画.元素的尺寸调整就是画布大小的调整,边框的设置就是画框的镶嵌.但是,作为一个最终要将画作展现到美术馆(网页)的艺术家来说,仅仅是这样还是不够的, ...