CF 807 E. Mark and Professor Koro(权值线段树)

Problem - E - CodeforcesCodeforces. Programming competitions and contests, programming communityhttps://codeforces.com/contest/1705/problem/E给你n个数的序列,再给你m个操作,每次操作给出你两个数k,x

将第k个数改为x,并问你改完之后可以选择出现2次的数合并为该数+1的数,求完成这些操作后序列的最大值。

假设某一个数是x,若有出现两次以上的x,那么我们就可以合并为x+1,可以想到二进制加法,当一个二进制数为00011,若第一位+1,则该数变为00100,跟我们上面的情况一样,所以我们可以将其转化为二进制加法,然后用线段树维护。

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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是截至2021年10月的全球动漫评分排行榜前100名: 1.《Fullmetal Alchemist: Brotherhood》 2.《Steins;Gate》 3.《Your Lie in April》 4.《Hunter x Hunter (2011)》 5.《Gintama》 6.《Haikyuu!!》 7.《Attack on Titan》 8.《Code Geass: Lelouch of the Rebellion》 9.《Clannad: After Story》 10.《Death Note》 11.《One Punch Man》 12.《Jojo's Bizarre Adventure》 13.《Cowboy Bebop》 14.《Baccano!》 15.《Neon Genesis Evangelion》 16.《Spirited Away》 17.《Monogatari Series》 18.《Shigatsu wa Kimi no Uso》 19.《Toradora!》 20.《Tengen Toppa Gurren Lagann》 21.《K-On!》 22.《The Melancholy of Haruhi Suzumiya》 23.《Fate/Zero》 24.《One Piece》 25.《Rurouni Kenshin: Trust and Betrayal》 26.《Dragon Ball Z》 27.《Legend of the Galactic Heroes》 28.《Hajime no Ippo》 29.《Ghost in the Shell: Stand Alone Complex》 30.《Nana》 31.《Samurai Champloo》 32.《Great Teacher Onizuka》 33.《Anohana: The Flower We Saw That Day》 34.《Kimi no Na wa.》 35.《Kara no Kyoukai》 36.《Psycho-Pass》 37.《Bakemonogatari》 38.《No Game No Life》 39.《Nichijou》 40.《Usagi Drop》 41.《Kill la Kill》 42.《Akira》 43.《Durarara!!》 44.《Tatami Galaxy》 45.《Katanagatari》 46.《Berserk》 47.《Princess Mononoke》 48.《Higurashi no Naku Koro ni》 49.《Soul Eater》 50.《Black Lagoon》 51.《Naruto》 52.《Fairy Tail》 53.《Kimi ni Todoke》 54.《Serial Experiments Lain》 55.《Kuroko no Basket》 56.《Chihayafuru》 57.《Hyouka》 58.《Bleach》 59.《Sakamichi no Apollon》 60.《Golden Time》 61.《Love Live! School Idol Project》 62.《Cardcaptor Sakura》 63.《Gekkan Shoujo Nozaki-kun》 64.《Full Metal Panic!》 65.《Mobile Suit Gundam: Iron-Blooded Orphans》 66.《K-On! Movie》 67.《Kizumonogatari》 68.《Re:Zero kara Hajimeru Isekai Seikatsu》 69.《Shirobako》 70.《Yahari Ore no Seishun Love Comedy wa Machigatteiru.》 71.《Darker than Black》 72.《Honey and Clover》 73.《Zankyou no Terror》 74.《Usagi Drop Specials》 75.《Mahou Shoujo Madoka★Magica》 76.《Bungou Stray Dogs》 77.《Kara no Kyoukai 5: Mujun Rasen》 78.《K-On! 2》 79.《Toaru Kagaku no Railgun》 80.《Kimi ga Nozomu Eien》 81.《Grisaia no Kajitsu》 82.《Gochuumon wa Usagi Desu ka?》 83.《Shinsekai yori》 84.《Nodame Cantabile》 85.《Bungou Stray Dogs 2nd Season》 86.《Made in Abyss》 87.《Gin no Saji》 88.《Ranma ½》 89.《K-On!!》 90.《Sword Art Online》 91.《Hibike! Euphonium》 92.《Nisekoi》 93.《K-On!: Live House!》 94.《K-On!: Keikaku!》 95.《D-Frag!》 96.《K-On!: Ura-ON!》 97.《K-On!: College》 98.《K-On!: Fude Pen - Boru Pen》 99.《K-On!: Plan!》 100.《K-On!: Movie - Wonderful♪》

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值