题意:给定上一个序列,然后有一些询问,求区间 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. 我的HibernateSearch笔记

    话不多说,直接上代码: 实体类: package com.smt.pojo; import java.io.Serializable; import javax.persistence.Column; ...

  2. 字节序(byte order)和位序(bit order)

    字节序(byte order)和位序(bit order)  在网络编程中经常会提到网络字节序和主机序,也就是说当一个对象由多个字节组成的时候需要注意对象的多个字节在内存中的顺序.  以前我也基本只了 ...

  3. MySQL error : Deadlock found when trying to get lock; try restarting transaction

    在使用 MySQL 时,我们有时会遇到这样的报错:“Deadlock found when trying to get lock; try restarting transaction”. 在 14. ...

  4. java代码对按钮进行监听---------------打印出每次点击按钮的次数

    其实,我真不会写嗯? package com.a.b; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ...

  5. java代码逆序输出再连篇

    总结:思维方式关键 package com.dfd; import java.util.Scanner; //逆序输出数字 public class fdad { public static void ...

  6. 消息队列函数(msgget、msgctl、msgsnd、msgrcv)及其范例

    消息队列函数由msgget.msgctl.msgsnd.msgrcv四个函数组成.下面的表格列出了这四个函数的函数原型及其具体说明. 1.   msgget函数原型 msgget(得到消息队列标识符或 ...

  7. Python web框架 Tornado(二)异步非阻塞

    异步非阻塞 阻塞式:(适用于所有框架,Django,Flask,Tornado,Bottle) 一个请求到来未处理完成,后续一直等待 解决方案:多线程,多进程 异步非阻塞(存在IO请求): Torna ...

  8. Python多线程-信号量

    信号量就是一个线程中有多个线程 # -*- coding:utf-8 -*- __author__ = "MuT6 Sch01aR" import threading import ...

  9. Node.js实用知识点

    本文介绍如何使用nodejs 简单的HttpServer 调试nodejs 基础路由 nodejs配置开发和生产环境 nodejs核心模块一览 express用法 文件I/O nodejs模块 nod ...

  10. redis学习六 集群的原理(转载)

    转载自 http://shift-alt-ctrl.iteye.com/blog/2285470 一.Redis Cluster主要特性和设计     集群目标 1)高性能和线性扩展,最大可以支撑到1 ...