P3369 学习笔记

省流:树状数组退火。。

题目传送门

树状数组大法好!!!

虽然这是一道平衡树,但是蒟蒻由于太飞舞了不会平衡树,在看了 这篇文章 后也是用树状数组轻松水果。

code
/**********************************************************
 * Author        : dingziyang888
 * Website       : https://www.luogu.com.cn/problem/
 * Created Time  :
 * FileName      :
 * Warning!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 * 1.MLE?
 * 2.array size enough?
 * 3.long long?
 * 4.overflow long long?
 * 5.multiple task cleaned?
 * 6.freopen?
 * 7.TLE?
 * *******************************************************/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <climits>
#define I using
#define AK namespace
#define IOI std
#define A return
#define C 0
#define Ofile(s) freopen(s".in", "r", stdin), freopen (s".out", "w", stdout)
#define Cfile(s) fclose(stdin), fclose(stdout)
#define fast ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
I AK IOI;

using ll = long long;
using uint = unsigned int;
using ull = unsigned long long;
using lb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;

constexpr int mod = 998244353;
constexpr ll maxk = 10000005;
constexpr ll maxn = (maxk << 1);

int n, opt, x;

int a[maxn];

ll lowbit(int i){
	return i & (-i);
}

ll cnt(ll x){
	ll sum = 0;
	for (int i = x + maxk; i; i -= lowbit(i))
		sum += a[i];
	return sum;
}

ll num(ll x){
	ll sum = 0;
	for (ll i = 24; i >= 0; i--)
		if (sum + (1 << i) < maxn && a[sum + (1 << i)] < x)
			sum += (1 << i), x -= a[sum];
	return sum + 1 - maxk;
}

void update1(ll x){
	for (ll i = x + maxk; i < maxn; i += lowbit(i))
		a[i]++;
}

void update2(ll x){
	for (ll i = x + maxk; i < maxn; i += lowbit(i))
		a[i]--;
}

/*********************以上均为树状数组常规操作*************************/

int main() {
	freopen("std.in", "r", stdin);
	freopen("std.out", "w", stdout);
	fast;
	cin >> n;
	while (n--){
		cin >> opt >> x;
		if (opt == 1)
			update1(x); // 直接 update 即可
		else if (opt == 2)
			update2(x); // 直接 update 即可
		else if (opt == 3)
			cout << cnt(x - 1) + 1 << endl;
		else if (opt == 4)
			cout << num(x) << endl;
		else if (opt == 5)
			cout << num(cnt(x - 1)) << endl;
		else 
			cout << num(cnt(x) + 1) << endl; // 这几个操作按题目要求 query 即可
	}
	A C;
}
posted @ 2026-02-10 10:09  constexpr_ll  阅读(3)  评论(0)    收藏  举报