题目链接

题意

对序列进行三种操作:

1、区间求和。

2、将区间小于等于$x$的数不改变相对顺序的前提下放到$x$左边,用同样规则将比$x$大的放到右边。

3、将区间大于$x$的数不改变相对顺序的前提下放到$x$左边,用同样规则将小于等于$x$的放到右边。

思路

将小于等于和大于$x$的数字分成两类,发现同类之间的相对顺序不改变,可以通过线段树维护区间内同类数字的数量和位置来获得区间真实值,使用前缀和维护两类数字序列的任意子段和。

代码

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm> #define IOS ios::sync_with_stdio(0),cin.tie(0);
#define DBG(x) cerr << #x << " = " << x << endl; using namespace std; typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL; const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const double eps = 1e-8;
const double pi = acos(-1.0); void file(){
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
} namespace BakuretsuMahou{ const int maxn = 2e5+5; int n,q,x;
LL a[maxn];
LL p1[maxn],p0[maxn],top1,top0; struct node{
int l,r;
LL val,laz;
}tree[maxn<<2]; void pushup(int u){
tree[u].val=tree[u<<1].val+tree[u<<1|1].val;
} void pushdown(int i,int len){
if(tree[i].laz){
tree[i<<1].laz=tree[i].laz;
tree[i<<1|1].laz=tree[i].laz;
tree[i<<1].val=(len-(len>>1))*(tree[i].laz-1);
tree[i<<1|1].val=(len>>1)*(tree[i].laz-1);
tree[i].laz=0;
}
} void build(int i,int l,int r){
tree[i].l=l;
tree[i].r=r;
tree[i].laz=0;
if(l == r){
tree[i].val=a[r];
return;
}
int mid=(l+r)>>1;
build(i<<1,l,mid);
build(i<<1|1,mid+1,r);
pushup(i);
} void update(int i,int l,int r,int L,int R,int c){
if(L <= l && r <= R){
tree[i].val=(r-l+1)*c;
tree[i].laz=c+1;
return;
}
pushdown(i,r-l+1);
int mid=(l+r)>>1;
if(L <= mid)update(i<<1,l,mid,L,R,c);
if(R > mid)update(i<<1|1,mid+1,r,L,R,c);
pushup(i);
} LL query(int i,int l,int r,int L,int R){
LL res=0;
if(L <= l && r <= R){
res=tree[i].val;
return res;
}
pushdown(i,r-l+1);
int mid=(l+r)>>1;
if(L <= mid)res+=query(i<<1,l,mid,L,R);
if(R > mid)res+=query(i<<1|1,mid+1,r,L,R);
return res;
} void Explosion(){
scanf("%d%d%d",&n,&q,&x);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
if(a[i] > x)p1[++top1]=a[i],a[i]=1;
else p0[++top0]=a[i],a[i]=0;
}
for(int i=2;i<=top1;i++)p1[i]+=p1[i-1];
for(int i=2;i<=top0;i++)p0[i]+=p0[i-1];
build(1,1,n);
while(q--){
int op,l,r;
scanf("%d%d%d",&op,&l,&r);
if(op == 1){
int num1=query(1,1,n,l,r),num0=r-l+1-num1;
int pre1=0,pre0=0;
if(l > 1){
pre1=query(1,1,n,1,l-1);
pre0=l-1-pre1;
}
printf("%lld\n",p1[pre1+num1]-p1[pre1]+p0[pre0+num0]-p0[pre0]);
}
if(op == 2){
int num1=query(1,1,n,l,r),num0=r-l+1-num1;
update(1,1,n,l,l+num0-1,0);
update(1,1,n,l+num0,r,1);
}
if(op == 3){
int num1=query(1,1,n,l,r);
update(1,1,n,l,l+num1-1,1);
update(1,1,n,l+num1,r,0);
}
}
}
} int main(){
//IOS
//file();
BakuretsuMahou::Explosion();
return 0;
}

在$camp$补的第一个题,懒惰啊。

CCPC-Wannafly Winter Camp Day5 (Div2, onsite) Sorting(线段树)的更多相关文章

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

    Replay: Dup4: 时间复杂度算不对? 一点点思路不经过验证就激动的要死? 浪费自己一个小时还浪费别人一个小时? 对1e3不敏感? 1e3 * 1e3是多少? 模拟建边跑dp不写非要写个大模拟 ...

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

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

  3. 2020 CCPC Wannafly Winter Camp Day1 C. 染色图

    2020 CCPC Wannafly Winter Camp Day1 C. 染色图 定义一张无向图 G=⟨V,E⟩ 是 k 可染色的当且仅当存在函数 f:V↦{1,2,⋯,k} 满足对于 G 中的任 ...

  4. CCPC-Wannafly Winter Camp Day3 (Div2, onsite)

    Replay Dup4: 没想清楚就动手写? 写了两百行发现没用?想的还是不够仔细啊. 要有莽一莽的精神 X: 感觉今天没啥输出啊, 就推了个公式?抄了个板子, 然后就一直自闭A. 语文差,题目没理解 ...

  5. 2019 CCPC-Wannafly Winter Camp Day1 (Div2, onsite)

    solve:4/11 补题:6/11 A 机器人 补题:zz 这是一道分类讨论的题目,有一个规律就是如果必须要从第一个区到第二个区,那么最多转区两次(1到2一次,2到1一次),然后分类讨论即可,只要细 ...

  6. 2019 CCPC-Wannafly Winter Camp Day7(Div2, onsite)

    solve 6/11 补题: A.迷宫 Code:zz Thinking:zz kk 把每个节点的深度都处理出来,同一深度的点的冲突度为 (x-1),x为同层次点数减一. 然后冲突度不断下传(冲突度为 ...

  7. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  8. CCPC Wannafly Winter Camp Div2 部分题解

    Day 1, Div 2, Prob. B - 吃豆豆 题目大意 wls有一个\(n\)行\(m\)列的棋盘,对于第\(i\)行第\(j\)列的格子,每过\(T[i][j]\)秒会在上面出现一个糖果, ...

  9. CCPC-Wannafly Winter Camp Day8 (Div2, onsite)

    咕咕咕.    camp补题. 传送门:https://www.zhixincode.com/contest/29/problems A.Aqours 题意:有一棵有根树,根节点为1,给出每个结点的父 ...

随机推荐

  1. bsp makefile2

    1. grep "bsp_dir" -r ./  -s  --exclude-dir "*.git" 用这个加快目录定位-- 2.编译所有子目录 for dir ...

  2. PHP奇淫技巧

    https://www.jb51.net/list/list_67_1.htm PHP技巧:https://www.jb51.net/list/list_67_13.htm mysql三范式 1NF: ...

  3. Linux系统中常见的目录名称以及相应内容

    目录名称 应放置文件的内容 /boot 开机所需文件——内核.开机菜单以及所需配置文件等等 /dev 以文件形式存放任何设备与接口 /etc 配置文件 /home 用户家目录 /bin 存放单用户模式 ...

  4. Logging 日志配置格式模板

    import osBASE_DIR = os.path.dirname(os.path.dirname(__file__))DB_PATH = os.path.join(BASE_DIR, 'db') ...

  5. zabbix 主动模式和被动模式说名

    一.zabbix agent主动模式与被动模式的区别 zabbix agent的运行模式有以下两种:1.被动模式:此模式为zabbix默认的工作模式,由zabbix server 向zabbix ag ...

  6. Kafka 详解(二)------集群搭建

    这里通过 VMware ,我们安装了三台虚拟机,用来搭建 kafka集群,虚拟机网络地址如下: hostname                      ipaddress             ...

  7. React16.x特性剪辑

    本文整理了 React 16.x 出现的耳目一新的概念与 api 以及应用场景. 更多 React 系列文章可以订阅blog 16.0 Fiber 在 16 之前的版本的渲染过程可以想象成一次性潜水 ...

  8. 根据json生成c#实体类

    vs 编辑->选择性粘贴->将json粘贴为类

  9. python语法与c++不同点

    代码结构由':'号和缩进 来标示. 函数: def 定义 不定参数: *args:   tarple可逐个传, 整体传, *拆包传 **kwargs dict可逐个传, 整体传, ** 拆包传, 有2 ...

  10. Array.prototype.reduce()

    reduce() 方法接收一个函数作为累加器(accumulator),数组中的每个值(从左到右)开始缩减,最终为一个值. arr.reduce([callback, initialValue]) c ...