Sum of Medians

题解:

对于这个题目,先想到是建立5棵Splay,然后每次更新把后面一段区间的树切下来,然后再转圈圈把切下来的树和别的树合并。

但是感觉写起来太麻烦就放弃了。

建立5棵线段树。

然后 seg[rt][i]代表的是只考虑当前所管辖的区间中的情况下, 下标对5取余之后为 i 的那些值的和。

最重要的一点是更新。

for(int i = ; i < ; ++i)
seg[i][rt] = seg[i][rt<<] + seg[((i-sz[rt<<]%)+)%][rt<<|];

我们可以通过左边的sz,来找到右边的数如果加上前面的sz之后,第i个值是哪里过来的。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int sz[N<<];
LL seg[][N<<];
int op[N], v[N];
int b[N], m;
char s[N];
int id(int x){
return lower_bound(b+, b++m, x) - b;
}
void Updata(int L, int v, int l, int r, int rt){
if(l == r){
seg[][rt] += b[L] * v;
sz[rt] += v;
return ;
}
int m = l+r >> ;
if(L <= m) Updata(L, v, lson);
else Updata(L, v, rson);
sz[rt] += v;
for(int i = ; i < ; ++i)
seg[i][rt] = seg[i][rt<<] + seg[((i-sz[rt<<]%)+)%][rt<<|];
}
int main(){
int q;
scanf("%d", &q);
for(int i = ; i <= q; ++i){
scanf("%s", s);
if(s[] == 'a') {
op[i] = ;
scanf("%d", &v[i]);
b[++m] = v[i];
}
else if(s[] == 'd'){
op[i] = -;
scanf("%d", &v[i]);
}
}
sort(b+, b++m);
m = unique(b+, b++m) - (b+);
for(int i = ; i <= q; ++i){
if(!op[i]) printf("%lld\n", seg[][]);
else
Updata(id(v[i]), op[i], , m, );
}
return ;
}
/*
4
add 1
add 2
add 3
sum */

最新文章

  1. 偶遇到 java.util.ConcurrentModificationException 的异常
  2. 王爽&lt; 汇编语言&gt;实验十二
  3. Java classpath 如何自动添加web-content /lib下的jar包
  4. IC开短路测试(open_short_test),编程器测试接触不良、开短路
  5. Linux 命令 - cp: 拷贝文件和目录
  6. 亲测PHpnow 安装环境
  7. Mono For Android离线激活
  8. Java--CyclicBarrier使用简介
  9. PowerTool(杀毒辅助工具) V4.6 中文免费绿色版
  10. thinkphp 单字母函数
  11. WeQuant交易策略—Chaikin A/D
  12. 208道面试题(JVM部分暂无答案)
  13. 【电子书分享】Learning PySpark下载,包含pdf、epub格式
  14. SQL 一列数据整合为一条数据
  15. Object类的wait方法带参数和notifyAll方法
  16. U-Mail企业邮箱如何导入授权文件
  17. 【洛谷4172】 [WC2006]水管局长(LCT)
  18. springboot使用遇到问题:Class “model.Address” is listed in the persistence.xml file but not mapped
  19. Office2013 如何安装Matlab notebook
  20. SpringBoot2 @validated 自定义效验类型

热门文章

  1. 【Java】判断字符串是否含字母
  2. python多线程详解
  3. 【转载】C/C++中long long与__int64的区别
  4. VSTO之PowerPoint(PPT)插件开发常用API汇总
  5. linux自学
  6. Go组件学习——gorm四步带你搞定DB增删改查
  7. 论文阅读 | Falcon: Balancing Interactive Latency and Resolution Sensitivity for Scalable Linked Visualizations
  8. 《HTTP权威指南》--阅读笔记(一)
  9. Compatibility模式安装windows7后改为AHCI模式无法启动Windows7的解决办法
  10. bytedance专题