HDU_3333 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 【线段树 + 离散化】的更多相关文章
- 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 线段树+离线处理
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3333 Turing Tree Time Limit: 6000/3000 MS (Java/Othe ...
- SPOJ D-query && HDU 3333 Turing Tree (线段树 && 区间不相同数个数or和 && 离线处理)
题意 : 给出一段n个数的序列,接下来给出m个询问,询问的内容SPOJ是(L, R)这个区间内不同的数的个数,HDU是不同数的和 分析 : 一个经典的问题,思路是将所有问询区间存起来,然后按右端点排序 ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- 主席树||可持久化线段树+离散化 || 莫队+分块 ||BZOJ 3585: mex || Luogu P4137 Rmq Problem / mex
题面:Rmq Problem / mex 题解: 先离散化,然后插一堆空白,大体就是如果(对于以a.data<b.data排序后的A)A[i-1].data+1!=A[i].data,则插一个空 ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
随机推荐
- leetcode 周赛 205 1576-5508-5509-5510
第四题比较难,看题解用并查集做比较简单,但是我觉得难度在想到用并查集,可能是最近做题少所以想不到吧. 1 替换所有的问号 class Solution { public: string modifyS ...
- codeforces 1C (非原创)
C. Ancient Berland Circus time limit per test 2 seconds memory limit per test 64 megabytes input sta ...
- 一个C++源文件从文本到可执行文件经历的过程
一个C++源文件从文本到可执行文件经历的过程 以Hello World为例进行说明 首先我们编写一个cpp源程序 test.cpp #include <iostream> using na ...
- 痞子衡嵌入式:我的博文总量终于追平了jicheng0622
自打2016年10月选定清爽又复古的博客园平台作为痞子衡的个人博客主战场之后,痞子衡就一直坚持在博客园首发原创技术文章,然后再同步到其他平台(CSDN,知乎,微信公众号...),在坚持更文近四年半(2 ...
- Makefile 流程控制(error,warning)等调试选项
1.退出码 0 ok1 错误2 使用了-q 选项 且目标不需要更新 返回2 2.选项 -f --file 指定makefile脚本 -n --just-print --dry -run -- reco ...
- Learn-JavaScript-with-MDN 系列文章: 01. var & let & const 对比
Learn-JavaScript-with-MDN 系列文章: 01. var & let & const 对比 var & let & const 区别 https: ...
- qt 移动窗口MoveWindow
RECT r; GetWindowRect(this->gameHwnd, &r); // 获取窗口的宽度和高度 int nWidth = r.right - r.left; int n ...
- python的with用法(转载)
原文地址:https://www.cnblogs.com/wanglei-xiaoshitou1/p/9238275.html 一.with语句是什么? 有一些任务,可能事先需要设置,事后做清理工作. ...
- 鸿蒙js开发7 鸿蒙分组列表和弹出menu菜单
鸿蒙入门指南,小白速来!从萌新到高手,怎样快速掌握鸿蒙开发?[课程入口]目录:1.鸿蒙视图效果2.js业务数据和事件3.页面视图代码4.跳转页面后的视图层5.js业务逻辑部分6.<鸿蒙js开发& ...
- Java后台防止客户端重复请求、提交表单
前言 在Web / App项目中,有一些请求或操作会对数据产生影响(比如新增.删除.修改),针对这类请求一般都需要做一些保护,以防止用户有意或无意的重复发起这样的请求导致的数据错乱. 常见处理方案 1 ...