QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#19009#1877. Matryoshka DollsZ_charRE 0ms0kbC++203.7kb2022-01-27 18:21:402022-05-06 03:33:17

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-05-06 03:33:17]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2022-01-27 18:21:40]
  • 提交

answer

#include <bits/stdc++.h>

template<class T> 
inline T read() {
    T x = 0; int f = 1; char ch = getchar();
    for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
    for (;  isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
    return x * f;
}
template<class T> inline void read(T &x) { x = read<T>(); }
template<class T, class... Args> inline void read(T &fir, Args&... args) { read(fir), read(args...); }
template<class T> 
inline void write(T x, char ch = '\n') {
    static int stk[32];
    if (x < 0) putchar('-'), x = -x;
    int top = 0;
    do stk[++top] = x % 10, x /= 10; while(x);
    while (top) putchar(stk[top--] + 48);
    putchar(ch);
}
template<class T, class... Args> inline void write(T fir, Args... args) { write(fir, ' '), write(args...); }

using ll = long long;
const int N = 5e5 + 8;

int n, m, a[N], B;

struct Yiw {
	Yiw() { l = r = id = 0; }
	Yiw(int l, int r, int id) : l(l), r(r), id(id) {}
	int l, r, id;
	friend bool operator<(const Yiw &a, const Yiw &b) {
		if (a.l / B != b.l / B) {
			return a.l < b.l;
		}
		if ((a.l / B) & 1) {
			return a.r < b.r;
		}
		else {
			return a.r > b.r;
		}
	}
};

signed main() {
	freopen("rrads.in", "r", stdin), freopen("rrads.out", "w", stdout);
	read(n, m);
	for (int i = 1; i <= n; ++i) {
		a[i] = read<int>();
	}
	if (n <= 10000) {
		std::vector<std::vector<std::pair<int, int>>> ask(n + 1);
		std::vector<int> ans(m + 1);
		for (int i = 1; i <= m; ++i) {
			int l, r;
			read(l, r);
			ask[r].push_back({l, i});
		}
		std::set<std::pair<int, int>> set;
		for (int _r = 1, ret = 0; _r <= n; ++_r, ret = 0) {
			std::sort(ask[_r].begin(), ask[_r].end());
			for (int _l = _r; _l >= 1; --_l) {
				auto it = set.insert({a[_l], _l}).first;
				if (it != set.begin()) {
					ret += std::abs(_l - (*std::prev(it)).second);
				}
				if (std::next(it) != set.end()) {
					ret += std::abs(_l - (*std::next(it)).second);
				}
				if (it != set.begin() && std::next(it) != set.end()) {
					ret -= std::abs((*std::prev(it)).second - (*std::next(it)).second);
				}
				while (ask[_r].size() && ask[_r].back().first == _l) {
					ans[ask[_r].back().second] = ret;
					ask[_r].pop_back();
				}
			}
			set.clear();
		}
		for (int i = 1; i <= m; ++i) {
			write(ans[i], '\n');
		}
	}
	else {
		B = std::sqrt(n);
		std::vector<Yiw> yiw(m);
		std::vector<ll> ans(m);
		for (int i = 0; i < m; ++i) {
			int l, r;
			read(l, r);
			yiw[i] = Yiw(l, r, i);
		}
		std::sort(yiw.begin(), yiw.end());
		std::set<std::pair<int, int>> set; ll ret = 0;
		auto add = [&](int x) {
			auto it = set.insert({a[x], x}).first;
			if (it != set.begin()) {
				ret += std::abs(x - (*std::prev(it)).second);
			}
			if (std::next(it) != set.end()) {
				ret += std::abs(x - (*std::next(it)).second);
			}
			if (it != set.begin() && std::next(it) != set.end()) {
				ret -= std::abs((*std::prev(it)).second - (*std::next(it)).second);
			}
		};
		auto del = [&](int x) {
			auto it = set.find({a[x], x});
			if (it != set.begin()) {
				ret -= std::abs(x - (*std::prev(it)).second);
			}
			if (std::next(it) != set.end()) {
				ret -= std::abs(x - (*std::next(it)).second);
			}
			if (it != set.begin() && std::next(it) != set.end()) {
				ret += std::abs((*std::prev(it)).second - (*std::next(it)).second);
			}
			set.erase(it);
		};
		for (int L = 1, R = 0, _ = 0; _ < m; ++_) {
			int l = yiw[_].l, r = yiw[_].r, id = yiw[_].id;
			while (l < L) {
				add(--L);
			}
			while (R < r) {
				add(++R);
			}
			while (L < l) {
				del(L++);
			}
			while (r < R) {
				del(R--);
			}
			ans[id] = ret;
		}
		for (int i = 0; i < m; ++i) {
			write(ans[i], '\n');
		}
	}
}
/*
5 2
5 4 2 3 1
3 4
2 5
*/

詳細信息

Test #1:

score: 0
Dangerous Syscalls

input:

5 5
1 5 2 4 3
1 5
1 4
1 3
1 2
1 1

output:


result: