一、题目

  Turing Tree

二、分析

  这题主要还是在区间的处理上。

  为了保证区间内的数没有重复的,那么可以对区间按右端点从小到大排序,这样对原数组处理时,尽量保证不重复的元素靠右(可以假设右端点固定考虑),就可以保证区间求出来的值是不重复的,对于重复的就把前面位置出现的这个数减掉(即赋值为0)即可。

  由于原数组的数比较大,要记录其之前的位置无法直接开数组,所以需要离散化。后面的就是普通的线段树的单点修改和区间查询了。

三、AC代码

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define Min(a,b) ((a)>(b)?(b):(a))
#define Max(a,b) ((a)>(b)?(a):(b))
#define lson (rt<<1)
#define rson (rt<<1|1)
#define P pair<int, int>
const int MAXN = 3e4;
const int MAXQ = 1e5;
ll A[MAXN + 13], Sum[MAXN<<2];
int Pre[MAXN + 13];
vector<ll> vec;
struct node
{
int L, R, id;
bool operator < (const node &t) const {
return R < t.R;
}
}B[MAXQ + 13];
ll Ans[MAXQ + 13]; int getPos(ll x)
{
return lower_bound(vec.begin(), vec.end(), x) - vec.begin();
}
void Push_up(int rt)
{
Sum[rt] = Sum[lson] + Sum[rson];
return;
}
void Update(int rt, int l, int r, int p, ll val)
{
if(l == r) {
Sum[rt] = val;
return;
}
int mid = (l + r) >> 1;
if(p <= mid)
Update(lson, l, mid, p, val);
else
Update(rson, mid + 1, r, p, val);
Push_up(rt);
}
ll Query(int rt, int l, int r, int L, int R)
{
if(L <= l && r <= R) {
return Sum[rt];
}
int mid = (l + r) >> 1;
ll ans = 0;
if(L <= mid)
ans += Query(lson, l, mid, L, R);
if(R > mid)
ans += Query(rson, mid + 1, r, L, R);
return ans;
} int main()
{
//freopen("input.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int T;
scanf("%d", &T);
while(T--) {
int N, Q;
scanf("%d", &N);
vec.clear();
for(int i = 1; i <= N; i++) {
scanf("%lld", &A[i]);
vec.push_back(A[i]);
}
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
scanf("%d", &Q);
for(int i = 0; i < Q; i++) {
scanf("%d%d", &B[i].L, &B[i].R);
B[i].id = i;
}
sort(B, B + Q);
memset(Sum, 0, sizeof(Sum));
memset(Pre, -1, sizeof(Pre)); //某个数字之前出现的位置
for(int i = 1, j = 0; i <= N; i++) {
int p = getPos(A[i]);
if(Pre[p] != -1) {
Update(1, 1, N, Pre[p], 0);
}
Pre[p] = i;
Update(1, 1, N, i, A[i]);
for(; j < Q; j++) {
if(B[j].R != i) {
break;
}
Ans[B[j].id] = Query(1, 1, N, B[j].L, B[j].R);
}
}
for(int i = 0; i < Q; i++) {
printf("%lld\n", Ans[i]);
}
}
return 0;
}

HDU_3333 Turing Tree 【线段树 + 离散化】的更多相关文章

  1. HDU 3333 Turing Tree (线段树)

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

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

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

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

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

  4. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  5. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  6. 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex

    题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...

  7. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

  8. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  9. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  10. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

随机推荐

  1. 转载-cookie和session的窃取

    一.cookie的基本特性 如果不了解cookie,可以先到 wikipedia 上学习一下. http request 浏览器向服务器发起的每个请求都会带上cookie: GET /index.ht ...

  2. 如何使用 js 实现一个树组件

    如何使用 js 实现一个树组件 tree component const arr = [ { id: 1, value: 1, level: 1, parentId: 0, }, { id: 2, v ...

  3. CSS style color all in one

    CSS style color all in one https://developer.mozilla.org/en-US/docs/Web/CSS/color_value /* Hexadecim ...

  4. HTTP/HTTPS Proxy & Charles

    HTTP/HTTPS Proxy & Charles Charles https://www.charlesproxy.com/ https://www.jianshu.com/p/53d2c ...

  5. WiFi 测速

    WiFi 测速 shit 联通 20M => 电信 20M ? https://zhuanlan.zhihu.com/p/86140645 shit 房东 中国电信网络测速 50M http:/ ...

  6. js types & primitive & object

    js types & primitive & object js 数据类型 typeof null // "object" typeof undefined // ...

  7. css var all in one & html & root & :root

    css var all in one number :root{ --num: 0; } html{ --num: 0; } let html = document.querySelector(`ht ...

  8. React Hooks & Context API

    React Hooks & Context API responsive website https://reactjs.org/docs/hooks-reference.html https ...

  9. VSCode & Node.js & debugger & inspector

    VSCode & Node.js & debugger & inspector F5 ws 元信息 (UUID) ws://127.0.0.1:46912/efa91bda-1 ...

  10. [C#] (原创)一步一步教你自定义控件——06,MaskLayer(遮罩层)

    一.前言 技术没有先进与落后,只有合适与不合适. 本篇的自定义控件是:遮罩层(MaskLayer). 遮罩层对软件的美观与易用性上的提高是很大的,在日常使用过程中也会经常看到各种遮罩层,虽然WinFo ...