正文
增加 179 行删除 6 行
1234567891011121314151621 行未改动383940414243444546474849505 行未改动565758596061626364656667686970## Previous problem IntroductionYou are given a sequence $(𝑎_1,𝑎_2,\dots,𝑎_𝑛)$. For each $1\le i\le n$, you have to find the number of $j<i$ that satisfied $a_j<a_i$. $n\le 10^5$.This problem is a classic problem, and I believe Mr.Jia has told you the solution.It's clear that we have two different solutions:1. We can use the Fenwick Tree to solve it.2. We can use Divide and Conquer, which is the algorithm for merge sort.::::success[The code for algorithm 1]```cppsigned main(){read(n);rep(i,1,n) read(a[i]);rep(i,1,n){21 行未改动::::Both two algorithms are important in today's problem.## The Real ProblemHere, 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$.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 2-d problem. So we Just need to solve a 2-d problem in each floor of the Divide Conquer. In detail, we first solve both the left and right, then we use the fenwick tree to maintain the information of the left half, and use these infomation to answer the right half of the sequence.::::success[The Code]```cppinline bool cmp(node a,node b){if(a.a!=b.a) return a.a<b.a; //j<i5 行未改动}void cdq(int l,int r){if(l==r) return;int mid=(l+r)>>1;cdq(l,mid),cdq(mid+1,r);int i=l,j=mid+1;for(;j<=r;++j){while(i<=mid&&a[i].b<=a[j].b) bit.add(a[i].c,a[i].cnt),++i;a[j].ans+=bit.ask(a[j].c);}rep(j,l,i-1) bit.add(a[j].c,-a[j].cnt);inplace_merge(a+l,a+mid+1,a+r+1,cmp1);}```::::1234567891011121314151617181920212221 行未改动44454647484950515253545556575859605 行未改动66676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243## Previous problem IntroductionYou are given a sequence $(𝑎_1,𝑎_2,\dots,𝑎_𝑛)$. For each $1\le i\le n$, you have to find the number of $j<i$ that satisfied $a_j<a_i$. $n\le 10^5$.This problem is a classic problem.It's clear that we have two different solutions:1. We can use the Fenwick Tree to solve it.2. We can use Divide and Conquer, which is the algorithm for merge sort.::::success[The code for algorithm 1]```cppstruct BIT{int c[N];inline int lb(int x){return x&-x;}inline void add(int x,int k){for(;x<=m;x+=lb(x))c[x]+=k;}inline int ask(int x){int ans=0;for(;x;x-=lb(x))ans+=c[x];return ans;}}bit;signed main(){read(n);rep(i,1,n) read(a[i]);rep(i,1,n){21 行未改动::::Both two algorithms are important in today's problem.## The Real ProblemHere, 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)$.::::success[The Code]```cppinline bool cmp(node a,node b){if(a.a!=b.a) return a.a<b.a; //j<i5 行未改动}void cdq(int l,int r){if(l==r) return;int mid=(l+r)>>1;cdq(l,mid),cdq(mid+1,r); //left and rightint i=l,j=mid+1; //left to rightfor(;j<=r;++j){while(i<=mid&&a[i].b<=a[j].b) bit.add(a[i].c,a[i].cnt),++i;a[j].ans+=bit.ask(a[j].c);}rep(j,l,i-1) bit.add(a[j].c,-a[j].cnt); //to clear the fenwickinplace_merge(a+l,a+mid+1,a+r+1,cmp1);}```::::## 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 \}.$$Now, given a permutation of $ 1 \sim n $, we delete $m$ elements one by one in a certain order. Your task is, **before** each deletion, to count the number of inversions in the entire sequence.::::::::success[solution]Consider what could change the answer. For a time point $t$, only $t_i>t$ could contribute to the answer. At the time point, there are two types of contribution:1. $p_i>p_j$ and $val_i<val_j$2. $p_i<p_j$ and $val_i>val_j$We calculate them separately, then take the prefix sum of the answer array.::::::::success[AC Code]```cppstruct BIT{int c[N];inline void clear(int n){rep(i,1,n)c[i]=0;}inline int lowbit(int x){return x&-x;}inline void add(int x,int k){for(;x<=n;x+=lowbit(x))c[x]+=k;}inline int ask(int x){int ans=0;for(;x;x-=lowbit(x))ans+=c[x];return ans;}inline int query(int l,int r){return ask(r)-ask(l-1);}}bit;inline bool cmp(node a,node b){return a.p<b.p;}void cdq(int l,int r){if(l==r) return;int mid=(l+r)>>1;cdq(l,mid),cdq(mid+1,r);sort(a+l,a+mid+1,cmp),sort(a+mid+1,a+r+1,cmp);int i=l,j=mid+1;while(j<=r){while(i<=mid&&a[i].p<=a[j].p) bit.add(a[i].x,a[i].k),++i;ans[a[j].id]+=a[j].k*bit.query(a[j].x+1,n);++j;}rep(j,l,i-1) bit.add(a[j].x,-a[j].k);i=mid,j=r;while(j>mid){while(i>=l&&a[i].p>=a[j].p) bit.add(a[i].x,a[i].k),--i;ans[a[j].id]+=a[j].k*bit.query(1,a[j].x-1);--j;}rep(j,i+1,mid) bit.add(a[j].x,-a[j].k);}signed main(){read(n),read(m);rep(i,1,n){int x;read(x);pos[x]=i;a[++tot]={x,i,1,0};}rep(i,1,m){int x;read(x);a[++tot]={x,pos[x],-1,i};}cdq(1,tot);rep(i,1,m) ans[i]+=ans[i-1];rep(i,0,m-1) write(ans[i]),pc('\n');return 0;}```::::### T2::::success[problem]You are given a sequence $(a_1,a_2,\dots,a_n)$. Here are $q$ operations.- 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;int n,q,tot;int a[N],lst[N],tm[N],ans[N];unordered_map<int,int>mp;set<int>s[N];struct node{int t,pre,pos,k,id;}v[N];inline bool cmp(node a,node b){if(a.t!=b.t) return a.t<b.t;if(a.pre!=b.pre) return a.pre<b.pre;if(a.pos!=b.pos) return a.pos<b.pos;return a.id<b.id;}inline bool cmp1(node a,node b){if(a.pre!=b.pre) return a.pre<b.pre;if(a.pos!=b.pos) return a.pos<b.pos;return a.id<b.id;}struct BIT{int c[N];inline int lb(int x){return x&(-x);}inline void add(int x,int k){for(;x<=n;x+=lb(x)) c[x]+=k;}inline int ask(int x){int res=0;for(;x;x-=lb(x)) res+=c[x];return res;}}bit;void cdq(int l,int r){if(l==r) return;int mid=(l+r)>>1;cdq(l,mid),cdq(mid+1,r);int i=l,j=mid+1;for(;j<=r;++j){while(i<=mid&&v[i].pre<v[j].pre){if(!v[i].id) bit.add(v[i].pos,v[i].k);++i;}if(v[j].id) ans[v[j].id]+=bit.ask(v[j].pos)*v[j].k;}rep(j,l,i-1) if(!v[j].id) bit.add(v[j].pos,-v[j].k);inplace_merge(v+l,v+mid+1,v+r+1,cmp1);}signed main(){read(n),read(q);rep(i,1,n){read(a[i]),lst[i]=mp[a[i]];s[a[i]].insert(i);v[++tot]={0,lst[i],i,1,0};mp[a[i]]=i;}rep(i,1,q){char op;int x,y;read(op),read(x),read(y);if(op=='R'){v[++tot]={i,lst[x],x,-1,0};s[a[x]].erase(x);auto it=s[a[x]].lower_bound(x);if(it!=s[a[x]].end()){int nxt=*it;v[++tot]={i,lst[nxt],nxt,-1,0};lst[nxt]=lst[x];v[++tot]={i,lst[nxt],nxt,1,0};}a[x]=y;it=s[a[x]].upper_bound(x);if(it!=s[a[x]].end()){int nxt=*it;v[++tot]={i,lst[nxt],nxt,-1,0};lst[nxt]=x;v[++tot]={i,lst[nxt],nxt,1,0};}if(it!=s[a[x]].begin()) lst[x]=*(--it);else lst[x]=0;s[a[x]].insert(x);v[++tot]={i,lst[x],x,1,0};}else{v[++tot]={i,x,y,1,i};v[++tot]={i,x,x-1,-1,i};}}sort(v+1,v+tot+1,cmp);cdq(1,tot);rep(i,1,q) if(ans[i]) write(ans[i]),pc('\n');return 0;}```::::