卡常求助(违规紫衫)
查看原帖
卡常求助(违规紫衫)
804607
rainygame楼主2023/1/19 18:42

打了一个全暴力,最快的话有 1515 分(#14 测试点 4.60ms4.60ms)。问一下有没有更快的方法。

#include <cstdio>
#include <string.h>
#include <algorithm>

int n, m, l, r, sum, i;
inline void read(int& x){
	char ch;
	x = 0;
	while ((ch = getchar()) > 47) x = (x<<1)+(x<<3)+(ch^48);
}

inline void write(int x){
	int tmp = x/10;
	if (x>9) write(tmp);
	putchar(x-(tmp<<1)-(tmp<<3)+'0');
}

struct Node{
	int pos, v;
}n1[500001], n2[500001];

inline bool cmp(Node a, Node b){
	return a.v < b.v;
}

int main(){
	// freopen("1.txt", "r", stdin);
	read(n);
	read(m);
	while (n--){
		++i;
		read(n1[i].v);
		n2[i].v = n1[i].v;
		n1[i].pos = n2[i].pos = i;
	}

	while (m--){
		read(l);
		read(r);
		std::sort(n1+l, n1+1+r, cmp);
		sum = 0;
		for (; l<r; ++l){
			sum += ((n1[l].pos-n1[l+1].pos)^((n1[l].pos-n1[l+1].pos)>>31))-((n1[l].pos-n1[l+1].pos)>>31);
			memcpy(&n1[l], &n2[l], 8);
		}
		memcpy(&n1[r], &n2[r], 8);
		write(sum);
		putchar('\n');
	}

	return 0;
}
2023/1/19 18:42
加载中...