题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和。

析:和求区间不同数的方法是一样,只要用主席树维护就好。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 30000 + 5;
const int maxm = maxn * 100;
const int mod = 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int a[maxn], T[maxn];
int lch[maxm], rch[maxm];
LL c[maxm];
int tot;
map<int, int> mp; int build(int l, int r){
int rt = tot++;
c[rt] = 0;
if(l == r) return rt;
int m = l + r >> 1;
lch[rt] = build(l, m);
rch[rt] = build(m+1, r);
return rt;
} int update(int rt, int pos, int val){
int newrt = tot++;
int tmp = newrt;
c[newrt] = c[rt] + val;
int l = 1, r = n;
while(l < r){
int m = l + r >> 1;
if(pos <= m){
lch[newrt] = tot++;
rch[newrt] = rch[rt];
newrt = lch[newrt];
rt = lch[rt];
r = m;
}
else {
rch[newrt] = tot++;
lch[newrt] = lch[rt];
newrt = rch[newrt];
rt = rch[rt];
l = m + 1;
}
c[newrt] = c[rt] + val;
}
return tmp;
} LL query(int rt, int pos){
LL ans = 0;
int l = 1, r = n;
while(pos < r){
int m = l + r >> 1;
if(pos <= m){
r = m;
rt = lch[rt];
}
else{
l = m + 1;
ans += c[lch[rt]];
rt = rch[rt];
}
}
return ans + c[rt];
} int main(){
int t; cin >> t;
while(t--){
scanf("%d", &n);
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
mp.clear();
tot = 0;
T[n+1] = build(1, n);
for(int i = n; i; --i){
if(mp.count(a[i])){
int tmp = update(T[i+1], mp[a[i]], -a[i]);
T[i] = update(tmp, i, a[i]);
}
else T[i] = update(T[i+1], i, a[i]);
mp[a[i]] = i;
}
scanf("%d", &m);
while(m--){
int l, r;
scanf("%d %d", &l, &r);
printf("%I64d\n", query(T[l], r));
}
}
return 0;
}

  

HDU 3333 Turing Tree (主席树)的更多相关文章

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

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

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

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

  3. HDU 3333 Turing Tree (线段树)

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

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

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

  5. hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)

    http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others)    ...

  6. HDU 3333 Turing Tree(离线树状数组)

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

  7. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  8. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  9. hdu 3333 Turing Tree(线段树+离散化)

    刚看到是3xian大牛的题就让我菊花一紧,觉着这题肯定各种高端大气上档次,结果果然没让我失望. 刚开始我以为是一个普通的线段树区间求和,然后啪啪啪代码敲完测试没通过,才注意到这个求和是要去掉相同的值的 ...

随机推荐

  1. windows下通过Git Bash使用Git常用命令

    Git跟SVN最大不同的地方就是分布式.SVN的集中式与Git的分布式决定各自的业务场景.既然是分布式的,那么大部分操作就是本地操作.一般Git操作都是通过IDE,比如Eclipse,如果装了Git ...

  2. C++ 中的 new/delete 和 new[]/delete[]

    在 C++ 中,你也许经常使用 new 和 delete 来动态申请和释放内存,但你可曾想过以下问题呢? new 和 delete 是函数吗? new [] 和 delete [] 又是什么?什么时候 ...

  3. 蓝桥杯 算法训练 ALGO-57 删除多余括号

    算法训练 删除多余括号   时间限制:1.0s   内存限制:512.0MB 问题描述 从键盘输入一个含有括号的四则运算表达式,要求去掉可能含有的多余的括号,结果要保持原表达式中变量和运算符的相对位置 ...

  4. tomcat启动时的java_home和jre_home错误

    The JRE_HOME environment variable is not defined correctlyThis environment variable is needed to run ...

  5. Lua文件操作和串行化

    function n_serialize(data) if type(data)=="number" then io.write(data,"\n") else ...

  6. Linux - 文件合并

    >:左边命令的结果覆盖右边文件的内容 cat 命令,把文件的内容覆盖另一个文件中的内容 把两个文件的内容合并到一个文件中 echo 命令 whoami 命令 >>:把左边命令执行的结 ...

  7. js操作一般文件和csv文件

    js操作一般文件和csv文件 将文本文件读成字符串 <input type="file" id="upload"> document.getElem ...

  8. Java两种延时——thread和timer

    在Java中有时候需要使程序暂停一点时间,称为延时.普通延时用Thread.sleep(int)方法,这很简单.它将当前线程挂起指定的毫秒数.如 try { Thread.currentThread( ...

  9. c# 程序调用cmd执行命令如SVN.exe

    c# 程序调用cmd执行命令如SVN.exe string str = Console.ReadLine(); System.Diagnostics.Process p = new System.Di ...

  10. spark 中文编码处理

    日志的格式是GBK编码的,而hadoop上的编码是用UTF-8写死的,导致最终输出乱码. 研究了下Java的编码问题. 网上其实对spark输入文件是GBK编码有现成的解决方案,具体代码如下 impo ...