Sorting

https://www.zhixincode.com/contest/21/problem/I?problem_id=324

题目描述

你有一个数列a_1, a_2, \dots, a_na1​,a2​,…,an​,你要模拟一个类似于快速排序的过程。有一个固定的数字xx。

你要支持三种操作:

  • 询问区间[l, r][l,r]之间的元素的和,也就是\sum_{i=l}^r a_i∑i=lr​ai​。
  • 对区间[l,r][l,r]进行操作,也就是说你把区间中所有的数字拿出来,然后把小于等于xx的数字按顺序放在左边,把大于xx的数字按顺序放在右边,把这些数字接起来,放回到数列中。比如说x=3x=3,你的区间里的数字是1,5,3,2,41,5,3,2,4,那么操作完之后区间里面的数字变为1,3,2,5,41,3,2,5,4。
  • 对区间[l,r][l,r]进行操作,也就是说你把区间中所有的数字拿出来,然后把大于xx的数字按顺序放在左边,把小于等于xx的数字按顺序放在右边,把这些数字接起来,放回到数列中。
 
 

输入描述

第一行三个整数n, q, x ( 1\leq n, q \leq 2*10^5, 0\leq x\leq 10^9)n,q,x(1≤n,q≤2∗105,0≤x≤109)表示元素的个数和询问的个数。

接下来一行nn个整数a_1, a_2, \dots, a_n(1\leq a_i\leq 10^9)a1​,a2​,…,an​(1≤ai​≤109)。

接下来qq行,每行三个正整数p, l, r (1\leq p\leq 3), 1\leq l\leq r\leq np,l,r(1≤p≤3),1≤l≤r≤n表示操作种类和区间。

输出描述

对于每个第一种操作,输出一行,表示答案。

样例输入 1

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

样例输出 1

15
1
3
2
5
4
15

首先,排序后小于等于x和大于x的数字的相对顺序是不变的,所以我们可以用0和1分别代替小于等于x和大于x的值。

先记录下小于等于x的数字和大于x的数字的前缀和,然后当区间修改的时候,我们就可以用前缀和来计算区间的值

区间修改的过程:当p==2时,把区间前半部分赋值为0,后半部分赋值为1,p==3时相反

 #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define maxn 200005 int n,q;
ll x; int tree[maxn<<],lazy[maxn<<];
ll a[maxn]; //tree中记录了1的个数 void push_up(int rt){
tree[rt]=tree[rt<<]+tree[rt<<|];
} void build(int l,int r,int rt){
lazy[rt]=-;
if(l==r){
if(a[l]<=x) tree[rt]=;
else tree[rt]=;
return;
}
int mid=l+r>>;
build(lson);
build(rson);
push_up(rt);
} void push_down(int len,int rt){
if(lazy[rt]!=-){
if(lazy[rt]==){
lazy[rt<<]=;
lazy[rt<<|]=;
tree[rt<<]=;
tree[rt<<|]=;
}
else{
lazy[rt<<]=;
lazy[rt<<|]=;
tree[rt<<]=len-len/;
tree[rt<<|]=len/;
}
lazy[rt]=-;
}
} //区间置0和置1
void add(int L,int R,int v,int l,int r,int rt){
if(L<=l&&R>=r){
if(v==) tree[rt]=;
else tree[rt]=r-l+;
lazy[rt]=v;
push_down(r-l+,rt);
return;
}
push_down(r-l+,rt);
int mid=l+r>>;
if(L<=mid) add(L,R,v,lson);
if(R>mid) add(L,R,v,rson);
push_up(rt);
} int query(int L,int R,int l,int r,int rt){///查询在L前面1的数量
if(L<=l&&R>=r){
return tree[rt];
}
push_down(r-l+,rt);
int mid=l+r>>;
int ans=;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
push_up(rt);
return ans;
} ll small[maxn],big[maxn]; int main(){
std::ios::sync_with_stdio(false);
cin>>n>>q>>x;
int s_co=,b_co=;
for(int i=;i<=n;i++){
cin>>a[i];
if(a[i]<=x) small[s_co]=small[s_co-]+a[i],s_co++;
else big[b_co]=big[b_co-]+a[i],b_co++;
}
build(,n,);
int p,l,r;
for(int i=;i<=q;i++){
cin>>p>>l>>r;
if(p==){
int one1,one2,one,zero1,zero2,zero;
one1=query(,r,,n,);
if(>l-) one2=;
else one2=query(,l-,,n,);
zero1=r-one1;
if(>l-) zero2=;
else zero2=l--one2;
cout<<small[zero1]-small[zero2]+big[one1]-big[one2]<<endl;
}
else if(p==){
int one=query(l,r,,n,);
int zero=r-l+-one;
if(l<=l+zero-) add(l,l+zero-,,,n,);
add(l+zero,r,,,n,);
}
else{
int one=query(l,r,,n,);
int zero=r-l+-one;
if(l<=l+one-) add(l,l+one-,,,n,);
add(l+one,r,,,n,);
}
}
}

Sorting(好题)的更多相关文章

  1. hdu 5427 A problem of sorting 水题

    A problem of sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contest ...

  2. K.Bro Sorting(思维题)

    K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)T ...

  3. BNUOJ-29364 Bread Sorting 水题

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29364 题意:给一个序列,输出序列中,二进制1的个数最少的数.. 随便搞搞就行了,关于更多 ...

  4. UVALive 6088 Approximate Sorting 构造题

    题目链接:点击打开链接 题意: 给定一个n*n的01矩阵 我们跑一下例子== 4 0111 0000 0100 0110 0123 \|____ 0|0111 1|0000 2|0100 3|0110 ...

  5. 【转】POJ百道水题列表

    以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...

  6. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  7. PAT甲级目录

    树(23) 备注 1004 Counting Leaves   1020 Tree Traversals   1043 Is It a Binary Search Tree 判断BST,BST的性质 ...

  8. 2019 CCPC-Wannafly Winter Camp Day5(Div2, onsite)

    solve 5/11 补题:7/11 A Cactus Draw Code:zz Thinking :zz 题意:要在n*n的网格内画上一棵节点数为n树,使得没有边相交. 很好想的构造题,因为网格有n ...

  9. A@GC*014

    A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...

  10. Educational Codeforces Round 67

    Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...

随机推荐

  1. 2018-2019 20165226 网络对抗 Exp1 PC平台逆向破解

    2018-2019 20165226 网络对抗 Exp1 PC平台逆向破解 目录 一.逆向及Bof基础实践说明 二.直接修改程序机器指令,改变程序执行流程 三.通过构造输入参数,造成BOF攻击,改变程 ...

  2. python 可视化 散点图。柱状图、等高线

    matplolib.org可查到更多画图方法等 散点图 import matplotlib.pyplot as plt import numpy as np #n个点 n = 1024 #平均值是0, ...

  3. Neutron 理解 (1): Neutron 所实现的网络虚拟化 [How Neutron Virtualizes Network]

    学习 Neutron 系列文章: (1)Neutron 所实现的网络虚拟化 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  4. Flex 学习

    Flex案例一: <html> <head> <meta http-equiv="Content-Type" content="text/h ...

  5. Linux系统文件名字体不同的颜色都代表什么

    Linux系统文件名字体不同的颜色都代表什么   在Linux中,文件的颜色都是有含义的.   其中, Linux中文件名颜色不同,代表文件类型不一样.   如下所示: www.2cto.com   ...

  6. numpy的shuffle函数

    import numpy as np from numpy.random import shuffle import pandas as pd df = pd.DataFrame([[1,2,3],[ ...

  7. django框架ajax

    参考 博文https://www.cnblogs.com/yuanchenqi/articles/9070966.html Ajax 简单示例: file_put文件上传页面: <!DOCTYP ...

  8. string hashcode 解读

    偶尔看到string hashcode方法如下 public int hashCode() { int h = hash; if (h == 0 && value.length > ...

  9. JAVA WebSocKet ( 实现简单的前后端即时通信 )

    1, 前端代码 HTML5 部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"& ...

  10. jsp 回车代替tab 自动切换text焦点

    方法一keyCode (IE11以后失效) <html> <head> <meta http-equiv="Content-Type" content ...