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. COMMON INTERVIEW QUESTIONS

    1. What do you see yourself doing five years from now? 2. What motivates you to put forth your great ...

  2. Unity3D SerialPort处理

    using UnityEngine; using System.Collections; using System; using System.Threading; using System.Coll ...

  3. [转]Windows 注册自定义的协议

    [转自] http://blog.sina.com.cn/s/blog_86e4a51c01010nik.html 1.注册应用程序来处理自定义协议 你必须添加一个新的key以及相关的value到HK ...

  4. [转]SendKeys.Send 方法

    SendKeys.Send 方法 向活动应用程序发送击键. 转载自: https://msdn.microsoft.com/zh-cn/library/system.windows.forms.sen ...

  5. 阿里云EC2+QEMU虚拟机+ROS完全教程!

    ---恢复内容开始--- 1.安装centos6.5 x64 同时记录,当前centos分配得到的IP,子网掩码,网关,以及MAC!!! 查看IP.mac命令ip add 查看网关命令cat /etc ...

  6. 让MySql支持Emoji表情(MySQL中4字节utf8字符保存方法)

    手机端插入Emoji表情,保存到数据库时报错: Caused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' ...

  7. Survival Coxph log-rank

    Difference between survdiff log-rank and coxph log-rank Ask Question 6 1 I'm using the survival pack ...

  8. Java虚拟机汇编代码

    0:将一个常量加载到操作数栈 3:数值从操作数栈存储到局部变量表 4:将int类型的常量加载到操作数栈 5:数值从操作数栈存储到局部变量表 6:将一个局部变量加载到操作栈 7:将一个局部变量加载到操作 ...

  9. OpenCL 归约 1

    ▶ 照着书上的代码,写了几个一步归约的计算,只计算一步,将原数组归约到不超过 1024 个工作项 ● 代码 // kernel.cl __kernel void reduce01(__global u ...

  10. vue pm2守护进程

    Linux 创建一个.sh可执行脚本,例如hexo.sh 代码 12 #!/usr/bin/env bashhexo server 使用pm2 start hexo.sh执行脚本 Windows 创建 ...