正文
增加 3 行删除 3 行
47 行未改动48495051525354555623 行未改动80818283848586878862 行未改动15115215315415515615715815984 行未改动47 行未改动Here, you are given two sequences $(x_1,x_2,\dots,x_n)$ and $(y_1,y_2,\dots,y_n)$. In this problem, you have to find the number of $j<i$ that satisfied $x_j<x_i$ and $y_j<y_i$. The time complexity should be $O(n\log^2 n)$.What's the problem? It's obvious that the old ways fail. One solution is to use complex data structures like trees on trees, but there are many drawbacks, for instance, the high space complexity up to $O(n\log^2 n)$. Is there any way to decrease the space complexity and make the code concise?This is a 3-d problem. As we know, Divide and Conquer can make a 3-d problem into some 2-d problem. So we just need to solve a 2-d problem at each level of the Divide and Conquer.In detail, we divide all the contributions into three parts: left to left, right to right, and left to right. We first solve both the left part and right part. So we only need to handle the contribution from the left half to the right half. So in every level of the divide and conquer, we use the Fenwick Tree to maintain the information of the left half, and use this infomation of the left half to answer the queries for right half of the sequence.If the time complexity of solving a sequence of length $n$ is $T(n)$, then $T(n)=2T(\frac{n}{2})+O(n\log n)$. By the master theorem, we get $O(n\log^2 n)$.23 行未改动::::## Sample problem### T1::::success[[Dynamic inversions](https://www.luogu.com.cn/problem/P3157)]For a sequence $a$, the number of inversions is defined as the number of elements in the set$$\{(i,j) \mid i < j \wedge a_i > a_j \}.$$62 行未改动- type 1: change the number of $a_x$- type 2: query the different colors in the range $[l,r]$::::::::success[solution]For every position, we maintain the numbers appear time $t_1$ and disappear time $t_2$ and the nearest $j<i$ with the same color. We use `std::set<int>` to maintain the position of each color. For each query $(t,l,r)$, we are finding the numbers of $i$ that satisfied $t_1\le t\le t_2$ and $lst_i<l$ and $l\le i\le r$. This is in the form of 3-d point counting problem. In detail, we divide each point into two, add $1$ when it appear, and add $-1$ when it disappear. We divide the query $(l,r)$ into $(1,r)-(1,l-1)$.::::::::success[AC Code]```cppconst int N=1e6+5;84 行未改动47 行未改动48495051525354555623 行未改动80818283848586878862 行未改动15115215315415515615715815984 行未改动47 行未改动Here, you are given two sequences $(x_1,x_2,\dots,x_n)$ and $(y_1,y_2,\dots,y_n)$. In this problem, you have to find the number of $j<i$ that satisfied $x_j<x_i$ and $y_j<y_i$. The time complexity should be $O(n\log^2 n)$.What's the problem? It's obvious that the old ways fail. One solution is to use complex data structures like trees on trees, but there are many drawbacks, for instance, the high space complexity up to $O(n\log^2 n)$. Is there any way to decrease the space complexity and make the code concise?This is a 3-d problem. As we know, Divide and Conquer can make a 3-d problem into some 2-d problem. So we just need to solve a 2-d problem at each level of the Divide and Conquer.In detail, we divide all the contributions into three parts: left to left, right to right, and left to right. We first solve both the left part and right part. So we only need to handle the contribution from the left half to the right half. So in every level of the divide and conquer, we use the Fenwick Tree to maintain the information of the left half, and use this infomation of the left half to answer the queries for right half of the sequence.If the time complexity of solving a sequence of length $n$ is $T(n)$, then $T(n)=2T(\frac{n}{2})+O(n\log n)$. By the master theorem, we get $O(n\log^2 n)$.23 行未改动::::## Sample problem### T1::::success[[Dynamic inversions](https://www.luogu.com.cn/problem/P3157)]For a sequence $a$, the number of inversions is defined as the number of elements in the set$$\{(i,j) \mid i < j \wedge a_i > a_j \}.$$62 行未改动- type 1: change the number of $a_x$- type 2: query the different colors in the range $[l,r]$::::::::success[solution]For every position, we maintain the numbers appear time $t_1$ and disappear time $t_2$ and the nearest $j<i$ with the same color. We use `std::set<int>` to maintain the position of each color. For each query $(t,l,r)$, we are finding the numbers of $i$ that satisfied $t_1\le t\le t_2$ and $lst_i<l$ and $l\le i\le r$. This is in the form of 3-d point counting problem. In detail, we divide each point into two, add $1$ when it appear, and add $-1$ when it disappear. We divide the query $(l,r)$ into $(1,r)-(1,l-1)$.::::::::success[AC Code]```cppconst int N=1e6+5;84 行未改动