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. 检测2个公网IP的GRE隧道是否通的方法,使用PPTP拨号检测。

    检测2个公网IP的GRE隧道是否通的方法,使用PPTP拨号检测. 因为PPTP是建立在GRE隧道基础上的. PPTP 防火墙开放 TCP 1723防火墙开放 IP protocol 47,即GRENA ...

  2. 在visual code的debugger for chrome中调试webpack构建的项目

    一直使用chrome中内置的调试器, 感觉世界那么美好, 自从学了react之后,使用visual code作为编辑器, 它提供了很多插件, 其中就包括debugger for chrome, 一款使 ...

  3. tornado-模板继承extend,函数和类的导入

    大 import tornado.ioloop import tornado.web import tornado.httpserver # 非阻塞 import tornado.options # ...

  4. HTML5 Canvas ( 事件交互, 点击事件为例 ) isPointInPath

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. win10/win7 笔记本 开启虚拟无线 批处理

    Microsoft Virtual WiFi Miniport Adapter 一.看网络网卡 有多出的这一项“Microsoft Virtual WiFi Miniport Adapter”,那么说 ...

  6. JavaScript加法

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  7. 关联github, 添加gitignore 规则

    1. 新建Maven项目 2. 新建github repository 3. 执行命令 echo "# se" >> README.md git init git ad ...

  8. nginx 限制ip

    转自:https://www.cmsky.com/nginx-deny-ip/ 面对垃圾留言和暴力破解,我们可以封禁IP,前文介绍过Apache环境使用.htacess来屏蔽IP,Nginx也可以做到 ...

  9. Redis 配置节

    Redis 后面的配置基于4.0.9版本=>>>不指定版本信息的配置说明都是耍流氓 比如在4.0.9中没有vm相关的及glueoutputbuf的配置信息 部分常用配置节(后面有详细 ...

  10. iPhone launch screen,self.view.frame.size

    在工程文件中找到以下设置 "Launch Screen File"只支持iOS8以上版本,如果用之,则self.view.frame.size返回的结果为正常的当前view尺寸. ...