[JLOI2013] Delete item

Put the two piles together and set the dividing point as mid. Since the deletion can only be done in descending order of numbers, in fact, as long as the useless operations are not repeated, the number of operations is certain.
First sort the numbers and record the position of the numbers. Delete from largest to smallest. According to the distance between the position of each number and the dividing point, calculate the number of moves to delete each number. After deleting a number, move the position of the dividing point accordingly.
It can be understood as: if you want to delete the current largest number, but the number in one heap is not at the top of the heap, you need to move all the smaller numbers on the same heap to another heap, and after moving, the two heaps The number of will change, and mid represents the position of the dividing point between the two piles.
When a certain number is deleted, a single point of modification is performed. Use the data structure to maintain the interval sum value and query the interval sum value.
#include <bits/stdc++.h>
#define int long long
#define lowbit(x) x&(-x)
using namespace std;
const int N=1e5+5;
int n1,n2,n,now,ans;
int c[N];
struct number{
    
    int x,pos;}num[N];

inline void change(int x,int v)
{
    
    
	while (x<=n)
	{
    
    
		c[x]+=v;
		x+=lowbit(x);
	}
}
inline int query(int x)
{
    
    
	int res=0;
	while (x)
	{
    
    
		res+=c[x];
		x-=lowbit(x);
	}
	return res;
}

inline bool cmp(number a,number b){
    
    return a.x>b.x;}

signed main(){
    
    
	scanf("%lld%lld",&n1,&n2);
	n=n1+n2+1;
	now=n1+1;
	for (register int i=n1; i>=1; --i) scanf("%lld",&num[i].x),num[i].pos=i,change(i,1);
	for (register int i=n1+2; i<=n; ++i) scanf("%lld",&num[i].x),num[i].pos=i,change(i,1);
	num[now].pos=now;
	sort(num+1,num+n+1,cmp);
	for (register int i=1; i<n; ++i)
	{
    
    
		change(num[i].pos,-1);
		if (now<num[i].pos) ans+=query(num[i].pos)-query(now);	
		else ans+=query(now)-query(num[i].pos);
		now=num[i].pos;
	}
	printf("%lld\n",ans);
return 0;	
}

Guess you like

Origin blog.csdn.net/Dove_xyh/article/details/107840518