Turing Tree

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5014    Accepted Submission(s): 1777

Problem Description
After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick thing happens again...

Now given a sequence of N numbers A1, A2, ..., AN and a number of Queries(i, j) (1≤i≤j≤N). For each Query(i, j), you are to caculate the sum of distinct values in the subsequence Ai, Ai+1, ..., Aj.

 
Input
The first line is an integer T (1 ≤ T ≤ 10), indecating the number of testcases below.
For each case, the input format will be like this:
* Line 1: N (1 ≤ N ≤ 30,000).
* Line 2: N integers A1, A2, ..., AN (0 ≤ Ai ≤ 1,000,000,000).
* Line 3: Q (1 ≤ Q ≤ 100,000), the number of Queries.
* Next Q lines: each line contains 2 integers i, j representing a Query (1 ≤ i ≤ j ≤ N).
 
Output
For each Query, print the sum of distinct values of the specified subsequence in one line.
 
Sample Input
2
 
 
3
1 1 4
2
1 2
2 3
5
1 1 2 1 3
3
1 5
2 4
3 5
 
Sample Output
1
5
6
3
6

题目链接:HDU 3333

对于莫队式暴力就没啥好说的了,时间慢不说代码量也比较大,还是离线+BIT快

代码:

#include <stdio.h>
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 30010;
const int M = 200010;
struct info
{
int k, l, r, flag, id;
info() {}
info(int _k, int _l, int _r, int _flag, int _id): k(_k), l(_l), r(_r), flag(_flag), id(_id) { }
bool operator<(const info &rhs)const
{
return k < rhs.k;
}
};
info Q[M];
LL T[N], ans[M >> 1];
int arr[N];
map<int, int>last; void init()
{
CLR(T, 0);
CLR(ans, 0);
last.clear();
}
void add(int k, LL v)
{
while (k < N)
{
T[k] += v;
k += (k & -k);
}
}
LL getsum(int k)
{
LL ret = 0;
while (k)
{
ret += T[k];
k -= (k & -k);
}
return ret;
}
int main(void)
{
int tcase, n, m, i;
scanf("%d", &tcase);
while (tcase--)
{
init();
scanf("%d", &n);
for (i = 1; i <= n; ++i)
scanf("%d", &arr[i]);
scanf("%d", &m);
int qcnt = 0;
for (i = 0; i < m; ++i)
{
int l, r;
scanf("%d%d", &l, &r);
Q[qcnt++] = info(l, l, r, 0, i);
Q[qcnt++] = info(r, l, r, 1, i);
}
sort(Q, Q + qcnt);
int x = 1;
for (i = 0; i < qcnt; ++i)
{
while (x <= Q[i].k)
{
if (last[arr[x]])
add(last[arr[x]], -arr[x]);
add(x, arr[x]);
last[arr[x]] = x;
++x;
}
if (Q[i].flag)
ans[Q[i].id] += getsum(Q[i].r) - getsum(Q[i].l - 1);
else
{
add(x - 1, -arr[x - 1]);
ans[Q[i].id] -= getsum(Q[i].r) - getsum(Q[i].l - 1);
add(x - 1, arr[x - 1]);
}
}
for (i = 0; i < m; ++i)
printf("%I64d\n", ans[i]);
}
return 0;
}

HDU 3333 Turing Tree(离线树状数组)的更多相关文章

  1. HDU 3333 Turing Tree(树状数组/主席树)

    题意 给定一个长度为 \(n​\) 的序列,\(m​\) 个查询,每次查询区间 \([L,R]​\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...

  2. HDU3333 Turing Tree 离线树状数组

    题意:统计一段区间内不同的数的和 分析:排序查询区间,离线树状数组 #include <cstdio> #include <cmath> #include <cstrin ...

  3. HDU 3333 Turing Tree 离线 线段树/树状数组 区间求和单点修改

    题意: 给一个数列,一些询问,问你$[l,r]$之间不同的数字之和 题解: 11年多校的题,现在属于"人尽皆知傻逼题" 核心思想在于: 对于一个询问$[x,R]$ 无论$x$是什么 ...

  4. HDU 3333 Turing Tree 线段树+离线处理

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...

  5. HDU 4417 Super Mario ( 离线树状数组 )

    把数值和查询放在一起从小到大排序,纪录每个数值的位置,当遇到数值时就更新到树状数组中,遇到查询就直接查询该区间和. #include <cstdio> #include <cstri ...

  6. HDU 3333 Turing Tree (线段树)

    Turing Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)

    题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...

  8. HDU 3333 Turing Tree (主席树)

    题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, ...

  9. hdu 4638 Group(离线+树状数组)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  10. Hdu-3333 Turning Tree (离线树状数组/线段树)

    Hdu-3333 Turning Tree 题目大意:先给出n个数字.面对q个询问区间,输出这个区间不同数的和. 题解:这道题有多重解法.我另一篇博客写了分块的解法  HDU-3333 Turing ...

随机推荐

  1. 第22题:链表中倒数第k个结点

    题目描述 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯,本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点,从头结点开始它们的值依次是1.2.3.4.5 ...

  2. 牛客小白月赛5 G 异或(xor) 【找规律】

    题目链接: https://www.nowcoder.com/acm/contest/135/g 题目描述 从前,Apojacsleam家的水族箱里,养了一群热带鱼. 在这几条热带鱼里,Apojacs ...

  3. 五、Linux 远程登录

    Linux 远程登录 Linux一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的Linux服务器. 这时我们就需要远程登录到Linux服务器来管理维护系统. Linux系统中是通过ss ...

  4. C盘扩容 更改C盘大小

    最近对xamarin有点兴趣,虽然网上的评论嘘声一片, 对于只想试一试的心态来说,对于网上所说的什么开发后的程序卡顿,可以用的三方库很少等, 我只想说,你们说的我不信,我要试一试看 我本来已经安装了v ...

  5. Redis学习笔记(三)

    一.数据备份与恢复 数据备份: localhost:> save OK 该命令会在redis的安装目录中创建文件dump.rdb,并把数据保存在该文件中 查看redis的安装目录: localh ...

  6. __setitem__,__getitem,__delitem__的作用

    class Foo: def __init__(self, name): self.name = name def __getitem__(self, item): print('obj[key]时, ...

  7. Terrorist’s destroy HDU - 4679

    Terrorist’s destroy HDU - 4679 There is a city which is built like a tree.A terrorist wants to destr ...

  8. Windows下如何用CMD命令跳转到指定的目录下

    以Window7为例说明,想要跳转到I:\adt-bundle-windows-x86-20130219\sdk\platform-tools目录下. 1.在运行中打开CMD命令窗口如下图所示: 2. ...

  9. Python虚拟机类机制之descriptor(三)

    从slot到descriptor 在Python虚拟机类机制之填充tp_dict(二)这一章的末尾,我们介绍了slot,slot包含了很多关于一个操作的信息,但是很可惜,在tp_dict中,与__ge ...

  10. pip install 报错 Could not fetch URL

    Could not fetch URL https://pypi.python.org/simple/xxx/: There was a problem confirming the ssl cert ...