您的位置:首页 > 健康 > 美食 > 门户网站建设思维导图_千万别学服装设计_免费代理上网网站_网站推广公司大家好

门户网站建设思维导图_千万别学服装设计_免费代理上网网站_网站推广公司大家好

2025/1/3 0:31:58 来源:https://blog.csdn.net/fks143/article/details/144807559  浏览:    关键词:门户网站建设思维导图_千万别学服装设计_免费代理上网网站_网站推广公司大家好
门户网站建设思维导图_千万别学服装设计_免费代理上网网站_网站推广公司大家好

题目:1649. 通过指令创建有序数组 - 力扣(LeetCode)

经典线段树问题

struct Node {Node* lc = nullptr;Node* rc = nullptr;int l;int r;int v = 0;Node(int l, int r) {this->l = l;this->r = r;}~Node() {if (lc) {delete lc;}if (rc) {delete rc;}}
};
class Solution {
public:static const int MAX = 1000000007;void BuildTree(Node* node) {int l = node->l;int r = node->r;if (l == r) {return;}int m = (l + r) / 2;node->lc = new Node(l, m);node->rc = new Node(m + 1, r);BuildTree(node->lc);BuildTree(node->rc);}int Sum(Node* t, int l, int r) {if (l <= t->l && r >= t->r) {return t->v;}if (l > t->r || r < t->l) {return 0;}int64_t ret = 0;if (l <= t->lc->r) {ret += Sum(t->lc, l, r);}if (r >= t->rc->l) {ret += Sum(t->rc, l, r);}if (ret >= MAX) ret %= MAX;return (int) ret;}void Add(Node* t, int p) {if (p >= t->l && p <= t->r) {++t->v;if (t->lc && p <= t->lc->r) {Add(t->lc, p);return;}if (t->rc && p >= t->rc->l) {Add(t->rc, p);}}}int createSortedArray(vector<int>& instructions) {if (instructions.size() <= 2) {return 0;}size_t n = instructions.size();int limL = instructions[0];int limR = instructions[0];for (int i = 0; i < n; i++) {int val = instructions[i];if (val < limL) limL = val;if (val > limR) limR = val;}Node* root = new Node(limL, limR);BuildTree(root);int64_t ret = 0;int64_t ls, rs;int min = instructions[0];int max = instructions[0];for (int i = 0; i < n; i++) {int val = instructions[i];if (val < min) {min = val;Add(root, val);continue;}if (val > max) {max = val;Add(root, val);continue;}if (val > limL) {ls = Sum(root, limL, val - 1);} else {ls = 0;}if (val < limR) {rs = Sum(root, instructions[i] + 1, limR);} else {rs = 0;}if (ls <= rs) {ret += ls;} else {ret += rs;}if (ret >= MAX) ret %= MAX;Add(root, val);}delete root;return (int) ret;}
};

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com