HDU 3333 Turing Tree (主席树)
题意:给定上一个序列,然后有一些询问,求区间 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 (主席树)的更多相关文章
- HDU 3333 Turing Tree 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- HDU 3333 Turing Tree(树状数组/主席树)
题意 给定一个长度为 \(n\) 的序列,\(m\) 个查询,每次查询区间 \([L,R]\) 范围内不同元素的和. \(1\leq T \leq 10\) \(1 \leq n\leq 300 ...
- HDU 3333 Turing Tree (线段树)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- hdu 3333 Turing Tree 图灵树(线段树 + 二分离散)
http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Others) ...
- HDU 3333 Turing Tree(离线树状数组)
Turing Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- hdu 3333 Turing Tree(线段树+离散化)
刚看到是3xian大牛的题就让我菊花一紧,觉着这题肯定各种高端大气上档次,结果果然没让我失望. 刚开始我以为是一个普通的线段树区间求和,然后啪啪啪代码敲完测试没通过,才注意到这个求和是要去掉相同的值的 ...
随机推荐
- ubuntu crontab 不执行的解决方法
在脚本文件的第二行添加下面一句即可 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 下面是分析解决问题的步骤: 1. ...
- TI c6657开发资源
TI 官方论坛:英文 中文 http://software-dl.ti.com/processor-sdk-rtos/esd/docs/latest/rtos/index.html http://e ...
- CCPC2018-湖南全国邀请赛 K 2018
K.2018 题目描述 Given a, b, c, d , find out the number of pairs of integers ( x, y ) where a ≤ x ≤ b, c ...
- POJ_3740 Easy Finding ——精确覆盖问题,DLX模版
Easy Finding Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18790 Accepted: 5184 Des ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- 微信小程序之if操作
.wxss控制样式 .price-agent{ font-size: 25rpx; color:#ababab; float: left; position: absolute; bottom: 0; ...
- Struts2接受页面传值过程中出现input的问题
其实我在使用Struts2的时候,遇到要求返回input的时候不算少.一般我们在使用Struts2的时候,都会返回SUCCESS/ERROR,或者是NONE以到Strtuts的配置文件中再进行相应的处 ...
- java的mysql初探
在实现如下demo之前,要安装mysql的驱动mysql-connector-java-gpl-5.1.26.msi DEMO: /* * 简单数据库测试 * @李志杰 * 2013-8-4 */ p ...
- Python 小练习二 数据库MySQL、Redis
import pymysql,redis def op_mysql(host,user,password,db,sql,port=3306,charset='utf8'): conn = pymysq ...
- HDU ACM Fibonacci
Problem Description Fibonacci numbers are well-known as follow: Now given an integer N, please find ...