WA #1玄关求条
查看原帖
WA #1玄关求条
1175869
Noob_Oler楼主2025/7/28 15:20
#include <bits/stdc++.h>
#define int long long
#define N 1000005
using namespace std;
int n,q,a[N],bl[N],tag[N],L[N],R[N],s[N],b[N],len;
void cp(int x)
{
    for(int i = L[x]; i <= R[x]; i++) b[i] = a[i];
    sort(b + L[x],b + R[x] + 1);
}
void build()
{
	len = sqrt(n);
	for(int i = 1; i <= n; i++)
	{
		bl[i] = (i - 1) / len + 1;
		s[bl[i]] += a[i];
	}
	for(int i = 1; i <= bl[n]; i++)
	{
		L[i] = R[i - 1] + 1;
		R[i] =  min(len * i,n);
        sort(b + L[i],b + R[i] + 1);
	}
}
int get(int l,int r,int c)
{
	int res = 0;
	if(bl[l] == bl[r])
	{
		for(int i = l; i <= r; i++) res += (b[i] + tag[bl[i]] >= c);
	}
	else
	{
		for(int i = l; i <= R[bl[l]]; i++)
        {
            res += (b[i] + tag[bl[i]] >= c);
        }
		for(int i = bl[l] + 1; i < bl[r]; i++)
        {
            int pos = lower_bound(b + L[i],b + R[i] + 1,c - tag[i]) - b;
            res += R[i] - pos + 1;
        }
		for(int i = L[bl[r]]; i <= r; i++)
        {
            res += (b[i] + tag[bl[i]] >= c);
        }
	}
	return res;
}
void add(int l,int r,int w)
{
	if(bl[l] == bl[r])
	{
		for(int i = l; i <= r; i++) 
		{
			a[i] += w;
			s[bl[i]] += a[i];
		}
        cp(bl[l]);
	}
	else
	{
		for(int i = l; i <= R[bl[l]]; i++) 
        {
			a[i] += w;
			s[bl[i]] += a[i];
        }
        cp(bl[l]);
		for(int i = bl[l] + 1; i < bl[r]; i++) tag[i] += w;
		for(int i = L[bl[r]]; i <= r; i++)
        {
			a[i] += w;
			s[bl[i]] += a[i];          
        }
        cp(bl[r]);
	}	
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0); 
	cin >> n >> q;
	for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) b[i] = a[i];
	build();
	for(int i = 1; i <= q; i++)
	{
        char opt;
		int l,r,c;
		cin >> opt >> l >> r >> c;
		if(opt == 'M')
		{
			add(l,r,c);
		}
		else
		{
            cout << get(l,r,c) << "\n";
        }
	}
	return 0;
}
2025/7/28 15:20
加载中...