当前位置:网站首页>Overall dichotomy?
Overall dichotomy?
2022-07-27 07:18:00 【lAWTYl】
The whole score is two points.
Last time C D Q CDQ CDQ Divide and conquer has not been written yet , Now let's write the whole dichotomy qwq.
A brief introduction
wss: As the name suggests, the whole dichotomy is the dichotomy of the whole .
say concretely , If these conditions are met , We can consider using the whole dichotomy to solve this problem :
- Multiple sets of asking
- Every question can be solved with a two-point answer
- The answer can be contributed in batches
This third point may be a little abstract , It may be better to understand with specific topics later , So I won't explain too much here .
Take a look at a simple topic
Portal :luogu1533 Poor dog
The main idea of the topic
Give you a sequence ( Each number is not equal ), Give you a question every time , Ask you the interval number k k k Small .
analysis
Isn't this a board , Direct chairman tree ( Balance tree is also ok qwq) Walk up .
Today we are going to learn the whole dichotomy , So take a moment , Let's take a look at the analysis idea of overall dichotomy .
First , Let's see if the above three conditions are satisfied . First, the first condition , Obviously satisfied ( There should be no explanation ). Then there is the second condition , Also obviously satisfied . Let's consider such a binary answer , Divide one in two on the value range m i d mid mid. c h e c k check check Scan the array once when you are ready , Sweep appears when it is less than m i d mid mid The number of r e s res res. If r e s = k − 1 res = k - 1 res=k−1, that a n s = m i d ans = mid ans=mid, Just came out . If r e s < k − 1 res < k - 1 res<k−1 that l = m i d l = mid l=mid, If r e s > k − 1 res > k - 1 res>k−1, that r = m i d r = mid r=mid. In this way, the complexity of each inquiry is obviously O ( n log n ) O(n\log n) O(nlogn) Of , So the total complexity is O ( n m log n ) O(nm\log n) O(nmlogn) Of .
Then there is the third condition , The answer can be contributed in batches , How to understand specifically , Let's consider the process of dichotomy .
First we find one m i d mid mid, It is found that this array is less than m i d mid mid The number of r e s < k − 1 res < k - 1 res<k−1 individual , Then it means that the answer we ask for is better than m i d mid mid Big . So we put l = m i d l = mid l=mid, Keep bisection . This is a very normal step in the two-point answer , Right , If it's still a normal two-point answer , We will repeat this process until we find r e s = k − 1 res = k - 1 res=k−1 until . But let's consider another approach :
We have r e s res res The number is less than m i d mid mid, And the number we will divide in two next time m i d ′ mid' mid′ Yes r e s ′ res' res′ The number is less than m i d ′ mid' mid′, Then there is r e s ′ o p t k − 1 res' \; opt \; k - 1 res′optk−1, Then we subtract from both sides of the equation at the same time r e s res res, That is to say r e s ′ − r e s o p t k − 1 − r e s res' - res \;opt\; k - 1 - res res′−resoptk−1−res.
Now the left formula means less than m i d ′ mid' mid′ Less than m i d mid mid The number of , It's satisfaction a i ∈ [ m i d , m i d ′ ) ∧ i ∈ [ l , r ] a_i \in[mid, mid') \land i \in[l, r] ai∈[mid,mid′)∧i∈[l,r] The number of . Then this number should be compared with the number we originally want to compare k − 1 k - 1 k−1, Subtract the number we find in the middle r e s res res Got k − 1 − r e s k - 1 - res k−1−res Compare . This is the meaning of batch contribution , Here we need to pay attention to , The contribution in batches we mentioned only refers to the contribution of the left side to the right , Imagine if our first m i d mid mid Of r e s res res Greater than k − 1 k - 1 k−1 Then the above operations cannot be carried out , So only consider the contribution of left to right .
So this problem can be done with integral dichotomy What a painful process qwq.
First of all, because the title doesn't say the range of weights , So let's discretize it . Then we take all inquiries offline , Save it in an array , Just like this. :

Every point on the left is a question , For the first i i i Questions will include l i , r i l_i, r_i li,ri and k i k_i ki, Then we will dichotomy on the value range , Now we have one in two m i d mid mid, Then we can quickly query how many numbers are less than m i d mid mid, We put less than... In the original array m i d mid mid The number of is added to a tree array ( Value range plus one ), So we can quickly query the interval [ l , r ] [l, r] [l,r] How many numbers in are less than m i d mid mid 了 .
Then we consider making a query for each query , I found that there are r e s i res_i resi The number satisfies a i ∈ [ 1 , m i d ∧ i ∈ [ l , r ] ) a_i \in [1, mid \land i \in [l, r]) ai∈[1,mid∧i∈[l,r]). Then take r e s i res_i resi and k i − 1 k_i - 1 ki−1 Make a comparison .
Then this is the key step of overall dichotomy , The results of the comparison can be divided into two categories :
- r e s i ≥ k i + 1 res_i \geq k_i + 1 resi≥ki+1, For this part, ask , We classify them into the first category .
- r e s i < k i + 1 res_i < k_i + 1 resi<ki+1, For this part, ask , We classify them into the second category .
Then we do the following for these two kinds of inquiries :
- The first kind of inquiry , Put it on “ On the left ”, Whatever it is .
- The second kind of inquiry , Put it on “ On the right ”, also k i − = r e s i k_i -= res_i ki−=resi.
The purpose of doing this is actually the same as our intention of analyzing the third conditional answer batch contribution above , In this way, we continue to divide downward , Continue processing , Until finally, there is only one question in each category , Then we can return to the answer we worked out ( What you don't understand can be compared with merging and sorting ).

Code
#include<bits/stdc++.h>
using namespace std;
#define in read()
#define MAXN 300300
#define MAXM 50050
inline int read(){
int x = 0; char c = getchar();
while(c < '0' or c > '9') c = getchar();
while('0' <= c and c <= '9'){
x = x * 10 + c - '0'; c = getchar();
}
return x;
}
int a[MAXN] = {
0 };
int b[MAXN] = {
0 };
int n = 0; int m = 0;
int id[MAXN] = {
0 };
int q1[MAXN] = {
0 };
int q2[MAXN] = {
0 };
int pos[MAXN] = {
0 };
int ans[MAXN] = {
0 };
struct Tque{
int k;
int l, r;
}q[MAXM];
int c[MAXN << 2] = {
0 };
inline int lowbit(int x) {
return x & -x; }
void add(int x, int y) {
for(; x <= n; x += lowbit(x)) c[x] += y; }
int query(int x){
int ans = 0;
for(; x; x -= lowbit(x)) ans += c[x];
return ans;
}
void solve(int l, int r, int ql, int qr){
if(l == r){
for(int i = ql; i <= qr; i++) ans[id[i]] = l;
return;
}
int mid = (l + r) >> 1; int cnt1 = 0, cnt2 = 0, res;
for(int i = l; i <= mid; i++) add(pos[i], 1);
for(int i = ql; i <= qr; i++){
res = query(q[id[i]].r) - query(q[id[i]].l - 1);
if(res >= q[id[i]].k) q1[++cnt1] = id[i];
else q[id[i]].k -= res, q2[++cnt2] = id[i];
}
for(int i = l; i <= mid; i++) add(pos[i], -1); // revoke
for(int i = 1; i <= cnt1; i++) id[i + ql - 1] = q1[i];
for(int i = 1; i <= cnt2; i++) id[i + ql - 1 + cnt1] = q2[i];
solve(l, mid, ql, cnt1 + ql - 1), solve(mid + 1, r, cnt1 + ql, qr);
}
int main(){
n = in; m = in;
for(int i = 1; i <= n; i++) b[i] = a[i] = in, id[i] = i;
for(int i = 1; i <= m; i++) q[i].l = in, q[i].r = in, q[i].k = in;
sort(b + 1, b + n + 1);
int k = unique(b + 1, b + n + 1) - b - 1;
for(int i = 1; i <= n; i++)
a[i] = lower_bound(b + 1, b + k + 1, a[i]) - b, pos[a[i]] = i;
solve(1, n, 1, m);
for(int i = 1; i <= m; i++) cout << b[ans[i]] << '\n';
return 0;
}
边栏推荐
- Qi Yue: thiol modified oligodna | DNA modified cdte/cds core-shell quantum dots | DNA coupled indium arsenide InAs quantum dots InAs DNA QDs
- 大疆livox定制的格式CustomMsg格式转换pointcloud2
- Student achievement management system based on SSM
- Codeforces Round #804 (Div. 2)(5/5)
- Pytorch model
- Music website management system based on SSM
- How to learn C language? This article gives you the complete answer
- 火狐浏览器,访问腾讯云服务器的时候,出现建立安全连接失败的问题。
- 2021 interview question of php+go for Zhongda factory (1)
- Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
猜你喜欢

Automatically generate UML sequence diagram according to text (draw.io format)

Summary of APP launch in vivo application market

零号培训平台课程-2、SSRF基础

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践

Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较

Watermelon book learning Chapter 5 --- neural network

指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟

Qi Yue: thiol modified oligodna | DNA modified cdte/cds core-shell quantum dots | DNA coupled indium arsenide InAs quantum dots InAs DNA QDs

李沐动手学深度学习V2-transformer和代码实现

Drools (5): drools basic syntax (3)
随机推荐
Using docker to install and deploy redis on CentOS
Vscode creates golang development environment and debug unit test of golang
Interpretation of deepsort source code (IV)
Relevant principles of MySQL index optimization
Es compares the data difference between the two indexes
Codeforces Round #787 (Div. 3)(7/7)
“蔚来杯“2022牛客暑期多校训练营1
Dajiang livox customized format custommsg format conversion pointcloud2
Digital image processing Chapter 1 Introduction
(转帖)eureka、consul、nacos的对比1
Interpretation of deepsort source code (V)
The vscode run command reported an error: the mark "&" is not a valid statement separator in this version.
Pytorch model
tableau prep连接maxcompute,只是书写很简单的sql,为啥报这个错误呢?
(posted) comparison of Eureka, consumer and Nacos 1
Gbase 8C - SQL reference 6 SQL syntax (11)
指令集 x 数澜科技丨加速政企数字化转型,打造DT领域独角兽企业联盟
仿真模型简单介绍
MySQL optimization SQL related (continuous supplement)
The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars