Sorting(好题)
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=lrai。
- 对区间[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(好题)的更多相关文章
- hdu 5427 A problem of sorting 水题
A problem of sorting Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contest ...
- K.Bro Sorting(思维题)
K.Bro Sorting Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)T ...
- BNUOJ-29364 Bread Sorting 水题
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29364 题意:给一个序列,输出序列中,二进制1的个数最少的数.. 随便搞搞就行了,关于更多 ...
- UVALive 6088 Approximate Sorting 构造题
题目链接:点击打开链接 题意: 给定一个n*n的01矩阵 我们跑一下例子== 4 0111 0000 0100 0110 0123 \|____ 0|0111 1|0000 2|0100 3|0110 ...
- 【转】POJ百道水题列表
以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- PAT甲级目录
树(23) 备注 1004 Counting Leaves 1020 Tree Traversals 1043 Is It a Binary Search Tree 判断BST,BST的性质 ...
- 2019 CCPC-Wannafly Winter Camp Day5(Div2, onsite)
solve 5/11 补题:7/11 A Cactus Draw Code:zz Thinking :zz 题意:要在n*n的网格内画上一棵节点数为n树,使得没有边相交. 很好想的构造题,因为网格有n ...
- A@GC*014
A@GC*014 A Cookie Exchanges 卡时跑了1s就输出-1 每次操作会使三个数的极差缩小一半,所以最多\(\log\)次之后就会出现\(A=B=C\)的情况,可以直接判掉 B Un ...
- Educational Codeforces Round 67
Educational Codeforces Round 67 CF1187B Letters Shop 二分 https://codeforces.com/contest/1187/submissi ...
随机推荐
- Response、Request、QueryString,修改,Cookies
Response对象:响应请求Response.Write("<script>alert('添加成功!')</script>");Response.Redi ...
- windows,linux下SVN实现自动更新WEB目录
通过SVN进行版本库管理,每次提交后,都要在SVN服务器更新最新上传的版本到WEB目录进行同步.操作比较烦琐,而且效率也低.使用SVN钩子脚本进行WEB目录同步,可很好的解决这方面的问题.由于测试机器 ...
- Matlab2013a打开M文件乱码解决
win10开发者英文版x64 Matlab2013a 解决方法:format改为chinese
- ubuntu18.04修改时区
运行如下命令: sudo tzselect 然后选择亚洲Asia,继续选择中国China,最后选择北京Beijing. 然后创建时区软链 sudo ln -sf /usr/share/zoneinfo ...
- 学习MongoDB 六: MongoDB查询(游标操作、游标信息)(三)
一.简介 db.collection.find()可以实现根据条件查询和指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段.并返回到匹配文档的游标,可以随意修改查询限制.跳跃.和排序顺序的 ...
- 《图像处理实例》 之 目标旋转矫正(基于区域提取、DFT变换)
目标:1.把矩形旋转正. 2.把文字旋转校正. ...
- UVA-755-排序
奇怪,我怎么还有一个排序题目没过 题意如下: 公司喜欢有难忘的电话号码,一个让电话号码变得难忘的方式是有一个拼读起来难忘的单词,比如,你可以呼叫University of Waterloo通过拨打难忘 ...
- 管道| , <<<重定向
https://blog.csdn.net/stormbjm/article/details/19173011
- python 网页爬虫,下载网络图片
# coding=utf-8 import lxml,bs4,re,requests csvContent='' file = open('D:\\tyc_demo.html','rb') soup ...
- Spring MVC 重定向
@RequestMapping("/testRedirect") public String testRedirect(){ System.out.println("te ...