当前位置:网站首页>CF 807 E. mark and Professor Koro (weight segment tree)
CF 807 E. mark and Professor Koro (weight segment tree)
2022-07-19 14:26:00 【eva_ can(not)survive】
Problem - E - CodeforcesCodeforces. Programming competitions and contests, programming community
https://codeforces.com/contest/1705/problem/E Here you are. n Sequence of Numbers , Here you are m Operations , Each operation gives you two numbers k,x
Will be the first k The number is changed to x, And ask you to choose to appear after you change 2 The number of times is merged into this number +1 Number of numbers , Find the maximum value of the sequence after these operations .
Suppose a number is x, If it occurs more than twice x, Then we can merge into x+1, You can think of binary addition , When a binary number is 00011, If first +1, Then the number becomes 00100, Just like the situation above , So we can convert it into binary addition , And then maintain it with a line tree .
const int N = 2e5 + 106;
struct node {
int l, r;
int add;
int sum;
} tr[MAXN * 3];
int cnt[MAXN], a[MAXN];
void push_up(int q) {
tr[q].sum = tr[q << 1].sum + tr[q << 1 | 1].sum;
}
void update(int q, int add) {
tr[q].sum += (tr[q].r - tr[q].l + 1) * add;
tr[q].add += add;
}
void push_down(int q) {
if (tr[q].add) {
update(q << 1, tr[q].add), update(q << 1 | 1, tr[q].add);
tr[q].add = 0;
}
}
void build(int q, int l, int r) {
tr[q].l = l, tr[q].r = r;
if (l == r)
return void(tr[q].sum = cnt[r]);
int mid = l + r >> 1;
build(q << 1, l, mid);
build(q << 1 | 1, mid + 1, r);
push_up(q);
}
void modify(int q, int l, int r, int add) {
// printf("2\n");
if (tr[q].l >= l && tr[q].r <= r) {
update(q, add);
return;
}
push_down(q);
int mid = tr[q].l + tr[q].r >> 1;
if (l <= mid)
modify(q << 1, l, r, add);
if (r > mid)
modify(q << 1 | 1, l, r, add);
push_up(q);
}
int query0(int q, int l) {
// printf("3\n");
if (tr[q].r < l || tr[q].sum == tr[q].r - tr[q].l + 1)
return -1;
if (tr[q].l == tr[q].r)
return tr[q].r;
push_down(q);
int t = query0(q << 1, l);
if (~t)
return t;
return query0(q << 1 | 1, l);
}
int query1(int q, int l) {
// printf("4\n");
if (tr[q].r < l || !tr[q].sum)
return -1;
if (tr[q].l == tr[q].r)
return tr[q].r;
push_down(q);
int t = query1(q << 1, l);
if (~t)
return t;
return query1(q << 1 | 1, l);
}
int querymax(int q) {
// printf("5\n");
if (tr[q].l == tr[q].r)
return tr[q].r;
push_down(q);
if (tr[q << 1 | 1].sum)
return querymax(q << 1 | 1);
return querymax(q << 1);
}
int query(int q, int x) {
if (tr[q].l == tr[q].r)
return tr[q].r;
push_down(q);
int mid = tr[q].l + tr[q].r >> 1;
if (x <= mid)
return query(q << 1, x);
return query(q << 1 | 1, x);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
cnt[a[i]]++;
}
for (int i = 1; i <= N; i++) {
cnt[i + 1] += cnt[i] / 2;
cnt[i] %= 2;
}
build(1, 1, N);
while (m--) {
int k, x;
cin >> k >> x;
int pos = query1(1, a[k]);
modify(1, pos, pos, -1);
if (pos != a[k])
modify(1, a[k], pos - 1, 1);
a[k] = x;
// printf("===\n");
pos = query0(1, a[k]);
modify(1, pos, pos, 1);
if (pos != a[k])
modify(1, a[k], pos - 1, -1);
cout << querymax(1) << '\n';
}
return 0;
}边栏推荐
- 第二届「绿树杯」数学竞赛排名与评析
- 洛谷:P3092 [USACO13NOV]No Change G(状压+二分,独特的状态定义,不写会后悔一辈子的题)
- 00 后博士获聘南大特任副研究员,曾 4 岁上小学,14 岁考入南大!
- matplotlib绘制多折线图(解决matplotlib中文无法显示问题)
- Can ping command still play like this?
- 揭开服务网格~Istio Service Mesh神秘的面纱
- BiShe - online reservation and registration system based on SSM
- js刷题练习---牛客网
- 数据库的增删改查
- 智康护物业养老服务方案
猜你喜欢

iVX低代码平台系列详解 -- 概述篇(二)

智康护物业养老服务方案

Rotation formula of coordinate simulation matrix

Redis源码与设计剖析 -- 3.字典

ShanDong Multi-University Training #3

4 a company has branches in six cities C1, C2, C3... C6. The connection between cities Ci and CJ (I, j=1,2,3,... 6) and the cost are listed in the following weighted adjacency matrix C

O'Neill's RPS curve compilation method (original by Dr. Tao)

topy库的安装(拓扑优化软件)

After 2000, he was hired as a special associate researcher of Nanjing University. He went to primary school at the age of 4 and was admitted to Nanjing University at the age of 14!

A review of classical must see for Nonconvex Optimization Problems "from symmetry to geometry", University of Rochester, et al
随机推荐
Can ping command still play like this?
Interview with Android development companies, make these three preparations and plan yourself
The manual is not complete. How to manually dig out the monitoring information of tongweb?
273. 分级 - AcWing题库【DP】
Huawei wireless device configuration intelligent roaming
O'Neill's RPS curve compilation method (original by Dr. Tao)
Huawei technologies:jonathan Krolikowski | from design to deployment, zero contact deep reinforcement learning WLANs
Addition, deletion, modification and query of database
华为无线设备配置动态负载均衡
Zhikanghu property elderly care service plan
Overview report of Chinese AI medical imaging industry in 2022
智康护物业养老服务方案
Chrome plug-ins for information collection
Event preview | Apache Doris x Apache seatunnel joint meetup to start registration!
Compréhension initiale de la fonction - partie 2
Codeforces Round #808 (Div. 1)(A~C)
Okaleido或杀出NFT重围,你看好它吗?
Redis源码与设计剖析 -- 4.跳跃表
si446使用记录(三):MATCH功能
Colliding Mice碰撞老鼠工程分析