SPOJ D-query 【主席树】
一 题目
二 分析
主席树的运用。
这题首先应该考虑的是,如何分出种类数?再就是考虑如何维护区间信息?
最开始想的是直接离散化后用权值线段树建主席树,发现不行,因为假如$ [l,r] $的值在$l$之前已经出现了,那么直接用历史版本的相减肯定会出问题。所以排除此方法。
所以在维护历史版本的时候要同时更新各个种类值的最新位置。这样就保证了,在给定$[l,r]$后就可以根据$r$位置的权值线段树,找到比$l$大的数目就是答案。
三 AC代码
1 #include <bits/stdc++.h>
2
3 using namespace std;
4
5 const int MAXN = 3e4 + 15;
6 int n, q, a[MAXN], root[MAXN], tot;
7 struct Node
8 {
9 int l, r, c;
10 }T[MAXN*30];
11
12 void build(int &rt, int l, int r)
13 {
14 T[++tot].c = 0;
15 rt = tot;
16 if(l == r) return;
17 int mid = (l+r)>>1;
18 build(T[rt].l, l, mid);
19 build(T[rt].r, mid + 1, r);
20 }
21 void update(int &cur, int pre,int l, int r, int pos, int val)
22 {
23 T[++tot] = T[pre];
24 cur = tot;
25 T[cur].c += val;
26 if(l == r) return;
27 int m = (l + r) >> 1;
28 if(pos <= m)
29 update(T[cur].l, T[pre].l, l, m, pos, val);
30 else
31 update(T[cur].r, T[pre].r, m + 1, r, pos, val);
32 }
33 int query(int rt, int l, int r, int pos)
34 {
35
36 if(l >= pos) return T[rt].c;
37 int ans = 0;
38 int m = (l + r) >>1;
39 if(pos <= m)
40 {
41 ans += T[T[rt].r].c;
42 ans += query(T[rt].l, l, m, pos);
43 }
44 else
45 {
46 ans += query(T[rt].r, m + 1, r, pos);
47 }
48 return ans;
49 }
50 int main()
51 {
52 //freopen("in.txt", "r", stdin);
53 scanf("%d", &n);
54 for(int i = 1; i <= n; i++)
55 {
56 scanf("%d", &a[i]);
57 }
58 tot = 0;
59 build(root[0], 1, n);
60 map<int, int> mp;
61 for(int i = 1; i <= n; i++)
62 {
63 if(mp.find(a[i]) == mp.end())
64 update(root[i], root[i-1], 1, n, i, 1);
65 else
66 {
67 int tmp;
68 update(tmp, root[i-1], 1, n, mp[a[i]], -1);
69 update(root[i], tmp, 1, n, i, 1);
70 }
71 mp[a[i]] = i;
72 }
73 scanf("%d", &q);
74 for(int i = 0; i < q; i++)
75 {
76 int x, y;
77 scanf("%d%d", &x, &y);
78 printf("%d\n", query(root[y], 1, n, x));
79 }
80 return 0;
81 }
SPOJ D-query 【主席树】的更多相关文章
- SPOJ - 3267. D-query 主席树求区间个数
SPOJ - 3267 主席树的又一种写法. 从后端点开始添加主席树, 然后如果遇到出现过的元素先把那个点删除, 再更新树, 最后查询区间就好了. #include<bits/stdc++.h& ...
- SPOJ DQUERY (主席树求区间不同数个数)
题意:找n个数中无修改的区间不同数个数 题解:使用主席树在线做,我们不能使用权值线段树建主席树 我们需要这么想:从左向右添加一到主席树上,添加的是该数字处在的位置 但是如果该数字前面出现过,就在此版本 ...
- 【学术篇】SPOJ COT 树上主席树
这是学完主席树去写的第二道题_(:з」∠)_ 之前用树上莫队水过了COT2... 其实COT也可以用树上莫队水过去不过好像复杂度要带个log还是怎么样可能会被卡常数.. 那就orz主席吧.... 写了 ...
- EC Round 33 F. Subtree Minimum Query 主席树/线段树合并
这题非常好!!! 主席树版本 很简单的题目,给一个按照指定节点的树,树上有点权,你需要回答给定节点的子树中,和其距离不超过k的节点中,权值最小的. 肯定首先一想,按照dfs序列建树,然后按照深度为下标 ...
- Count on a tree SPOJ - COT (主席树,LCA)
You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer ...
- CF893F Subtree Minimum Query 主席树
如果是求和就很好做了... 不是求和也无伤大雅.... 一维太难限制条件了,考虑二维限制 一维$dfs$序,一维$dep$序 询问$(x, k)$对应着在$dfs$上查$[dfn[x], dfn[x] ...
- SPOJ - DQUERY (主席树求区间不同数的个数)
题目链接:https://vjudge.net/problem/SPOJ-DQUERY 题目大意:给定一个含有n个数的序列,有q个询问,每次询问区间[l,r]中不同数的个数. 解题思路:从左向右一个一 ...
- SPOJ - DQUERY D-query 主席树
题意; 给一个数列$\{ a_i\}$ 一些询问$(l_i,r_i)$ 问你$[l,r]$有多少个不同元素 题解: 其实本质思路和离线化处理询问的线段树/树状数组写法差不多,对区间$[x,r]$来说, ...
- SPOJ Query on a tree III (树剖(dfs序)+主席树 || Splay等平衡树)(询问点)
You are given a node-labeled rooted tree with n nodes. Define the query (x, k): Find the node whose ...
随机推荐
- 微服务架构Day03-SpringBoot之web开发配置
概述 SpringBoot开发: 1.创建SpringBoot应用,选中需要的场景模块. 2.SpringBoot已经默认将场景模块配置好,只需要在配置文件中指定少量的配置(数据库地址,用户名,密码) ...
- Asp.Net Core Grpc 入门实践
Grpc简介 gRPC 是一种与语言无关的高性能远程过程调用 (RPC) 框架. 在 gRPC 中,客户端应用程序可以直接调用不同计算机上的服务器应用程序上的方法,就像它是本地对象一样,从而更轻松地创 ...
- 如何使用 js 实现相似图片搜索
如何使用 js 实现相似图片搜索 以图搜图 https://www.google.com/imghp?hl=en https://www.google.com/imghp?hl=zh https:// ...
- 使用 js 实现一个简易版的模版引擎
使用 js 实现一个简易版的模版引擎 regex (function test() { this.str = str; })( window.Test = ...; format() { let ar ...
- TypeScript & React & Jest
TypeScript & React & Jest create-react-app Jest ``tsx import React from 'react'; import { re ...
- Web Vitals
Web Vitals https://www.youtube.com/watch?v=2k1QbgLAFdI&feature=youtu.be $ npm i -S web-vitals ht ...
- how to write an ebook that can support published by format PDF, Epub, Mobi and so on
how to write an ebook that can support published by format PDF, Epub, Mobi and so on 如何写一本自己的电子书,支持各 ...
- js in depth: Object & Function & prototype & __proto__ & constructor & classes inherit
js in depth: Object & Function & prototype & proto & constructor & classes inher ...
- uniapp设置不同的主题(Theme)
App.vue: <style lang="stylus"> @css { html { --primary: blue; --bg-image: url(https: ...
- vue的filter用法,检索内容
var app5 = new Vue({ el: '#app5', data: { shoppingList: [ "Milk", "Donuts", &quo ...