[JSOI2018]军训列队

题目大意:

\(n(n\le5\times10^5)\)个学生排成一排,第\(i\)个学生的位置为\(a_i\)。\(m(m\le5\times10^5)\)次命令,每次将编号在\([l,r]\)之间的学生移动到\([k,k+r-l]\)位置上,每个位置站一个人,顺序自定(无需考虑原来在\([k,k+r-l]\)位置上的人)。每次的代价为每个人移动距离之和。求每次操作的最小代价。

思路:

建立主席树,维护每个区间内人数和与坐标和。

考虑所有人都在区间\([k,k+r-l]\)以左/右的情况,答案就是每个人坐标和与区间\([k,k+r-l]\)坐标和之差。

否则将学生按照坐标大小分成两部分考虑,若坐标较小的学生有\(d\)个,统计将两部分人分别放入区间\([l,l+d)\)和\([l+d,r]\)的代价即可。

时间复杂度\(\mathcal O(n\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
typedef long long int64;
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=5e5+1,M=1e6,logM=30;
class FotileTree {
private:
struct Node {
int64 sum;
int cnt,left,right;
};
Node node[N*logM];
int sz,new_node(const int &p) {
node[++sz]=node[p];
return sz;
}
int length(const int &b,const int &e) const {
return e-b+1;
}
public:
int root[N];
void insert(int &p,const int &b,const int &e,const int &x) {
p=new_node(p);
node[p].cnt++;
node[p].sum+=x;
if(b==e) return;
const int mid=(b+e)>>1;
if(x<=mid) insert(node[p].left,b,mid,x);
if(x>mid) insert(node[p].right,mid+1,e,x);
}
int64 query(const int &q,const int &p,const int &b,const int &e,const int &l,const int &r) const {
const int len=length(l,r);
const int64 dsum=node[p].sum-node[q].sum;
if(l>=e) return (int64)(l+r)*len/2-dsum;
if(r<=b) return dsum-(int64)(l+r)*len/2;
const int mid=(b+e)>>1,d=node[node[p].left].cnt-node[node[q].left].cnt;
int64 ret=0;
if(b<=mid&&l<=l+d-1) ret+=query(node[q].left,node[p].left,b,mid,l,l+d-1);
if(mid+1<=e&&l+d<=r) ret+=query(node[q].right,node[p].right,mid+1,e,l+d,r);
return ret;
}
};
FotileTree t;
int main() {
const int n=getint(),m=getint();
for(register int i=1;i<=n;i++) {
t.insert(t.root[i]=t.root[i-1],1,M,getint());
}
for(register int i=0;i<m;i++) {
const int l=getint(),r=getint(),k=getint();
printf("%lld\n",t.query(t.root[l-1],t.root[r],1,M,k,k+r-l));
}
return 0;
}

[JSOI2018]军训列队的更多相关文章

  1. BZOJ5319: [Jsoi2018]军训列队

    BZOJ5319: [Jsoi2018]军训列队 https://lydsy.com/JudgeOnline/problem.php?id=5319 分析: 易知把所有人按原本的顺序放到\([K,K+ ...

  2. BZOJ.5319.[JSOI2018]军训列队(主席树)

    LOJ BZOJ 洛谷 看错了,果然不是\(ZJOI\)..\(jry\)给\(JSOI\)出这么水的题做T3么= = 感觉说的有点乱,不要看我写的惹=-= 对于询问\(l,r,k\),设\(t=r- ...

  3. 洛谷 P4559: bzoj 5319: [JSOI2018]军训列队

    题目传送门:洛谷 P4559. 题意简述: 有 \(n\) 个学生,编号为 \(i\) 的学生有一个位置 \(a_i\). 有 \(m\) 个询问,每次询问编号在 \([l,r]\) 区间内的学生跑到 ...

  4. bzoj 5319: [Jsoi2018]军训列队

    Description Solution 最优情况可以是所有人按位置从小到大排序之后依次占到自己 \(K+\) 排名的位置上去 因为每一个休息位置不同,那么一定递增,所以一定存在一个分界点,左边的是往 ...

  5. BZOJ5319 & 洛谷4559 & LOJ2551:[JSOI2018]军训列队——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5319 https://www.luogu.org/problemnew/show/P4559 ht ...

  6. 【BZOJ5319】军训列队(主席树)

    [BZOJ5319]军训列队(主席树) 题面 BZOJ 洛谷 题解 一眼题既视感... 首先很明显,每次询问的结果显然是做一次离散. 然后直接上主席树就好了... 查询答案的方式也很简单 考虑一下那个 ...

  7. BZOJ5319/LOJ2551「JSOI2018」列队

    问题描述 作为一名大学生,九条可怜在去年参加了她人生中的最后一次军训. 军训中的一个重要项目是练习列队,为了训练学生,教官给每一个学生分配了一个休息位置.每次训练开始前,所有学生都在各自的休息位置休息 ...

  8. LOJ 2551 「JSOI2018」列队——主席树+二分

    题目:https://loj.ac/problem/2551 答案是排序后依次走到 K ~ K+r-l . 想维护一个区间排序后的结果,使得可以在上面二分.求和:二分可以知道贡献是正还是负. 于是想用 ...

  9. 【LOJ】#2551. 「JSOI2018」列队

    题解 老年选手一道裸的主席树都要看好久才看出来 首先熟练的把这个区间建成\(n\)个主席树 然后对于一个询问,我们相当于在主席树上二分一个mid,使得\(mid - K + 1\)正好和\([l,r] ...

随机推荐

  1. Vuex 基本概念

    Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 每一个 Vuex 应用的核心就是 stor ...

  2. GNU Readline 库及编程简介【转】

    转自:https://www.cnblogs.com/hazir/p/instruction_to_readline.html 用过 Bash 命令行的一定知道,Bash 有几个特性: TAB 键可以 ...

  3. Linux 内核通知链随笔【中】【转】

    转自:http://blog.chinaunix.net/uid-23069658-id-4364171.html 关于内核通知链不像Netlink那样,既可以用于内核与用户空间的通信,还能用于内核不 ...

  4. 使用 ftrace 调试 Linux 内核,第 1 部分【转】

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-ftrace1/index.html ftrace 是 Linux 内核中提供的一种调试工具.使用 ...

  5. Lodash使用示例(比较全)

    <html> <head> <meta name="viewport" content="width=device-width" ...

  6. Leetcode 之Wildcard Matching(32)

    跟上题类似,主要考虑‘*’的匹配问题.如果遇到‘*’,则跳过继续匹配,如果不匹配,则s++,重新扫描. bool isMatch2(const char *s, const char *p) { if ...

  7. ZOJ-3430

    Detect the Virus  Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his c ...

  8. Search Insert Position——二分法

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  9. LeetCode Linked List Cyle

    Problem Description Given a linked list, determine if it has a cycle in it. Follow up:Can you solve ...

  10. python安装numpy和scipy的资源

    whl资源:注意python版本和位数. http://www.lfd.uci.edu/~gohlke/pythonlibs/#numpy