vuqa multiset+lower_bound TLE为什么啊
查看原帖
vuqa multiset+lower_bound TLE为什么啊
877860
Just_Love_You楼主2023/8/31 16:27
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
constexpr int N=1e6+10;
constexpr int mod=1e9+7;
void solve()
{
    int n,k,q;
    cin >> n >> k >> q;
    multiset<int,greater<> > mst;
    vector<int> f(n+1);
    int sum=0;
    for(int i=0;i<q;i++)
    {
        int a,b;
        cin >> a >> b;
        if(mst.size()<k)
        {
            sum-=f[a];
            if(!mst.empty()&&f[a])
            {
            	auto o=lower_bound(mst.begin(),mst.end(),f[a],greater<>());
                mst.erase(o);
            }
            f[a]=b;
            sum+=f[a];
            mst.emplace(f[a]);
        }else
        {
            auto tmp=mst.begin();
            advance(tmp,k-1);
            if(*tmp>f[a])
            {
                if(*tmp<b)
                {
                    sum-=*tmp;
                    sum+=b;
                }
                if(f[a])
                {
                	auto o=lower_bound(mst.begin(),mst.end(),f[a],greater<>());
	                mst.erase(o);
				}
                f[a]=b;
                mst.emplace(f[a]);
            }else
            {
                sum-=f[a];
                if(f[a])
                {
                	auto o=lower_bound(mst.begin(),mst.end(),f[a],greater<>());
	                mst.erase(o);
				}
                f[a]=b;
                mst.emplace(f[a]);
                auto p=mst.begin();
                advance(p,k-1);
                if(*p<=f[a])
                    sum+=f[a];
                else sum+=*p;
            }
        }
        mst.erase(0);
        cout << sum << endl;
    }
}
signed main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    // int o;
    // cin >> o;
    // while(o--)
    solve();
    return 0;
}
//
//⠀⠀⠀             ⠀⢸⣿⣿⣿⠀⣼⣿⣿⣦⡀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⠀⠀⠀ ⠀⢸⣿⣿⡟⢰⣿⣿⣿⠟⠁
//⠀⠀⠀⠀⠀⠀⠀⢰⣿⠿⢿⣦⣀⠀⠘⠛⠛⠃⠸⠿⠟⣫⣴⣶⣾⡆
//⠀⠀⠀⠀⠀⠀⠀⠸⣿⡀⠀⠉⢿⣦⡀⠀⠀⠀⠀⠀⠀ ⠛⠿⠿⣿⠃
//⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣦⠀⠀⠹⣿⣶⡾⠛⠛⢷⣦⣄⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣧⠀⠀⠈⠉⣀⡀⠀ ⠀⠙⢿⡇
//⠀⠀⠀⠀⠀⠀⢀⣠⣴⡿⠟⠋⠀⠀⢠⣾⠟⠃⠀⠀⠀⢸⣿⡆
//⠀⠀⠀⢀⣠⣶⡿⠛⠉⠀⠀⠀⠀⠀⣾⡇⠀⠀⠀⠀⠀⢸⣿⠇
//⢀⣠⣾⠿⠛⠁⠀⠀⠀⠀⠀⠀⠀⢀⣼⣧⣀⠀⠀⠀⢀⣼⠇ 
//⠈⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⡿⠋⠙⠛⠛⠛⠛⠛⠁
//⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣾⡿⠋⠀
//⠀⠀⠀⠀⠀⠀⠀⠀⢾⠿⠋⠀
//
2023/8/31 16:27
加载中...