WA 50pts 求条
查看原帖
WA 50pts 求条
416816
TG_Space_Station楼主2025/7/19 17:24
#include <bits/stdc++.h>
#define lson tr[rt].l, l, mid
#define rson tr[rt].r, mid + 1, r
#define lxb tr[rt].l
#define rxb tr[rt].r
#define get_mid int mid = (l + r) >> 1
#define lowbit(x) (x & (-x))
#define all(x) root[x], 1, size
using namespace std;

const int N = 2e5 + 5;

struct BIT_PST
{
	struct tree_node
	{
		int l, r;
		int val;
	};
	
	private:
		int a[N], c[N];
		int size, top, rt;
		int root[N];
		int cnt[2], tmp[2][N];
		tree_node tr[N * 800];
		
		int clone(int point)
		{
			tr[++top] = tr[point];
			return top;
		}
		void pushup(int rt)
		{
			tr[rt].val = tr[lxb].val + tr[rxb].val;
		}
		int build(int rt, int l, int r)
		{
			rt = ++top;

			if(l == r)
			{
				tr[rt].val = 0;
				return rt;
			}
			
			get_mid;
			tr[rt].l = build(lson);
			tr[rt].r = build(rson);
			pushup(rt);
			return rt;
		}
		int query(int l, int r, int k)
		{
			get_mid, i, res, x = 0;
			for(i = 1; i <= cnt[1]; i++) x += tr[tr[tmp[1][i]].l].val;
			for(i = 1; i <= cnt[0]; i++) x -= tr[tr[tmp[0][i]].l].val;

		    if(l == r)
		        return l;

		    if(k <= x)
		    {
		    	for(i = 1; i <= cnt[1]; i++) tmp[1][i] = tr[tmp[1][i]].l;
				for(i = 1; i <= cnt[0]; i++) tmp[0][i] = tr[tmp[0][i]].l;
		    	res = query(l, mid, k);
			}
		    else
		    {
		    	for(i = 1; i <= cnt[1]; i++) tmp[1][i] = tr[tmp[1][i]].r;
				for(i = 1; i <= cnt[0]; i++) tmp[0][i] = tr[tmp[0][i]].r;
		    	res = query(mid + 1, r, k - x);
			}

			return res;
		}
		int update(int rt, int l, int r, int pos, int val)
		{
			rt = clone(rt);

			if(l == r)
			{
				tr[rt].val += val;
				return rt;
			}

			get_mid;

			if(pos <= mid)
				tr[rt].l = update(lson, pos, val); 
			else
				tr[rt].r = update(rson, pos, val);
			
			pushup(rt);
			return rt;
		}
	public:
		int que(int l, int r, int k)
		{
			int i;
			l--;
			cnt[0] = cnt[1] = 0;
			for(i = r; i; i -= lowbit(i)) tmp[1][++cnt[1]] = root[c[i]];
			for(i = l; i; i -= lowbit(i)) tmp[0][++cnt[0]] = root[c[i]];
			return query(1, size, k);
		}
		void change(int pos, int val)
		{
			int i;

			for(i = pos; i <= size; i += lowbit(i))
			{
				if(a[pos])
				{
					root[++rt] = update(all(c[i]), a[pos], -1);
					c[i] = rt;
				}

				root[++rt] = update(all(c[i]), val, +1);
				c[i] = rt;
			}
			
			a[pos] = val;
		}
		void get(int n)
		{
			size = n;
			top = rt = 0;
			root[0] = build(1, 1, n);
		}
}arr;

int n, m, a[N];
vector <int> vec;
struct node { char opt; int l, r, k; } q[N];
char opt; int l, r, k;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	 
	int i;
	cin >> n >> m;
	arr.get(n + m);
	
	for(i = 1; i <= n; i++)
		cin >> a[i], vec.push_back(a[i]);
	for(i = 1; i <= m; i++)
	{
		cin >> q[i].opt;
		
		if(q[i].opt == 'Q')
			cin >> q[i].l >> q[i].r >> q[i].k;
		if(q[i].opt == 'C')
		{
			cin >> q[i].l >> q[i].k;
			vec.push_back(q[i].k);
		}
	}
	
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());
	
	for(i = 1; i <= n; i++)
		a[i] = lower_bound(vec.begin(), vec.end(), a[i]) - vec.begin() + 1;
	for(i = 1; i <= m; i++)
		if(q[i].opt == 'C')
			q[i].k = lower_bound(vec.begin(), vec.end(), q[i].k) - vec.begin() + 1;
	
	for(i = 1; i <= n; i++)
		arr.change(i, a[i]);
	
	for(i = 1; i <= m; i++)
	{
		opt = q[i].opt;
		
		if(opt == 'Q')
		{
			l = q[i].l, r = q[i].r, k = q[i].k;
			cout << vec[arr.que(l, r, k) - 1] << "\n";
		}
		if(opt == 'C')
		{
			l = q[i].l, k = q[i].k;
			arr.change(l, k); 
		}
	}
	return 0;
}
2025/7/19 17:24
加载中...