当前位置:网站首页>[Luogu] p1972 HH Necklace
[Luogu] p1972 HH Necklace
2022-07-24 02:13:00 【Record algorithm solution】
Title address :
https://www.luogu.com.cn/problem/P1972
Title Description :
HH There is a necklace made of all kinds of beautiful shells .HH I believe different shells will bring good luck , So after every walk , He would take out a piece of shell at will , Think about what they mean .HH Constantly collecting new shells , therefore , His necklace is getting longer and longer . one day , He suddenly asked a question : In a piece of shell , How many different shells are there ? This question is hard to answer …… Because the necklace is too long . therefore , He had to turn to you for help , To solve this problem .
Input format :
One positive integer per line n n n, Indicates the length of the necklace .
The second line n n n A positive integer a i a_i ai, Indicates the... In the necklace i i i A kind of shell .
The third line is an integer m m m, Express HH Number of queries .
Next m m m That's ok , Two integers per line l , r l,r l,r, The interval of inquiry .
Output format :
Output m m m That's ok , One integer per row , Ask the corresponding answer in turn .
Data range :
about 20 % 20\% 20% The data of , 1 ≤ n , m ≤ 5000 1\le n,m\leq 5000 1≤n,m≤5000;
about 40 % 40\% 40% The data of , 1 ≤ n , m ≤ 1 0 5 1\le n,m\leq 10^5 1≤n,m≤105;
about 60 % 60\% 60% The data of , 1 ≤ n , m ≤ 5 × 1 0 5 1\le n,m\leq 5\times 10^5 1≤n,m≤5×105;
about 100 % 100\% 100% The data of , 1 ≤ n , m , a i ≤ 1 0 6 1\le n,m,a_i \leq 10^6 1≤n,m,ai≤106, 1 ≤ l ≤ r ≤ n 1\le l \le r \le n 1≤l≤r≤n.
This question may need a faster reading method , The maximum data point is about 20 20 20MB.
Mo team solution reference https://blog.csdn.net/qq_46105170/article/details/125827776. Next, we introduce the tree array solution .
First, sort all queries from small to large according to the right endpoint . The key to solving the problem is if a certain number x x x There have been many times , We only reserve the last time in this inquiry section . The method is , Open a pointer i i i, Dynamically maintain each number in i i i Positions that have appeared before f [ a i ] f[a_i] f[ai]( If it doesn't exist f [ a i ] = 0 f[a_i]=0 f[ai]=0), When asked [ l , r ] [l,r] [l,r] When , If i ≤ r i\le r i≤r, It will be i i i Sweep to r r r, At the same time, every time in the tree array , If f [ a i ] ≠ 0 f[a_i]\ne 0 f[ai]=0, Then the position f [ a i ] f[a_i] f[ai] reduce 1 1 1; Every time the position in the tree array i i i Add 1 1 1. In the array maintained by the tree array , Depending on the r r r For the right border , Each number is marked only at its rightmost position 1 1 1. In this way, you only need to find the interval of the array maintained by the tree array [ l , r ] [l,r] [l,r] And . The code is as follows :
#include <iostream>
#include <algorithm>
#define lowbit(x) (x & -x)
using namespace std;
const int N = 1e6 + 10;
int n, m, a[N], res[N], f[N];
struct Query {
int id, l, r;
} q[N];
int tr[N];
inline void add(int k, int x) {
for (; k <= n; k += lowbit(k)) tr[k] += x;
}
inline int sum(int k) {
int res = 0;
for (; k; k -= lowbit(k)) res += tr[k];
return res;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 0; i < m; i++) {
scanf("%d%d", &q[i].l, &q[i].r);
q[i].id = i;
}
sort(q, q + m, [&](Query &q1, Query &q2) {
return q1.r < q2.r;
});
for (int i = 1, k = 0; k < m; k++) {
int r = q[k].r, l = q[k].l, id = q[k].id;
for (; i <= n && i <= r; i++) {
if (f[a[i]]) add(f[a[i]], -1);
f[a[i]] = i;
add(i, 1);
}
res[id] = sum(r) - sum(l - 1);
}
for (int i = 0; i < m; i++) printf("%d\n", res[i]);
}
Preprocessing time complexity O ( m log m ) O(m\log m) O(mlogm), Each inquiry averages O ( log n + n / m ) O(\log n+n/m) O(logn+n/m), Space O ( n + max i a i ) O(n+\max_i a_i) O(n+maxiai).
边栏推荐
- The combination sum of C language power deduction question 39. Backtracking method and traversal method
- What is naked SQL? What middleware or plug-in is good for express to operate MySQL?
- Hospital network security architecture
- Hundred million financing events account for more than 30%. Where is the next stop for super automation? -- Manfu Technology
- On Domain Driven Design
- Perlin noise and random terrain
- What are the principal guaranteed financial products with an annual interest rate of about 6%?
- 毕业设计校园信息发布平台网站源码
- Exchange 2010 wildcard SSL certificate installation document
- 2022.7.22 JS entry common data types and methods
猜你喜欢

Hundred million financing events account for more than 30%. Where is the next stop for super automation? -- Manfu Technology

Study and use of burpsuite plug-in

架构实战营模块二作业

In depth understanding - wechat developer tools

Small volume stock trading record | based on multi task crawler technology, realize level1 sampling of A-share real-time market

Performance optimization of wechat applet (subcontracting, operation process details, simplified structure, native component communication)
![[bdsec CTF 2022] partial WP](/img/00/25c1953a324fbf04e3baf592079978.png)
[bdsec CTF 2022] partial WP

View binding confusion. I have been studying confusion for two days.

Preliminary use of 145 keep alive

Writing of graph nodes that trigger different special effects during the day and at night in Tiktok
随机推荐
Computer room construction data
Digicert code signing certificate
Mysql database UDF authorization learning
深入理解微信小程序的底层框架(二)组件系统、Exparser
新红包封面平台可搭建分站独立后台的源码
Responsive layout a web page displays different effects on different devices) meta:vp
Perlin noise and random terrain
Troisième semaine d'été
ACM SIGIR 2022 | interpretation of selected papers of meituan technical team
2022-07-22: what is the output of the following go language code? A:1; B:1.5; C: Compilation error; D:1.49。 package main import “fmt“ func main() { var i
hdu-7141 Ball (bitset)
Distributed resource management and task scheduling framework yarn
[C language operation of linked list (initialization, establishment, length calculation, addition, deletion, and output of linked list)]
Canvas drawing (mouse click to draw and lift to end)
Detailed comparison between graphic array and linked list, performance test
Preliminary use of 145 keep alive
Try to run this command from the system terminal Make sure that you use the correct
Redis 6.0 source code learning simple dynamic string
STM32概念和安装【第一天】
原生组件、小程序与客户端通信原理、video、map、canvas、picker等运行原理