求助,wa在test#17,用的离线二分查询+st表维护
查看原帖
求助,wa在test#17,用的离线二分查询+st表维护
795672
Azhe_iwnl楼主2023/10/26 00:52
#include<iostream>
#include<functional>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<math.h>
#include<map>
#include<set>
#include<queue>
#include<cstring>
#include<stack>
#include <utility>
#define int long long
using namespace std;
const int p = 2e5 + 10;
const int mod = 1e9 + 7;
using i64 = long long;
vector<pair<int,int>>ans;
int stlist[p][30];
vector<pair<int, int>>qr;
int logn[p];
void ini() {
    logn[1] = 0, logn[2] = 1;
    for (int i = 3; i <= p - 1; i++)logn[i] = logn[i / 2] + 1;
}
int Query(int l, int r)
{
    int k = logn[r - l + 1];
    return max(stlist[l][k], stlist[r - (1 << k) + 1][k]);
}
void solve() {
    int c, q; cin >> c >> q;
    ini();
    int sum = 0;
    int qry = 0;
    for (int i = 1; i <= q; ++i) {
        int a; cin >> a;
        if (a == 1) {
            int x; cin >> x;
            sum += x;
            ans.push_back({ sum,x });
        }
        else {
            if (a == 4) {
                qr.push_back({ 4,ans.size()});
            }
            else {
                if (a == 2) {
                    int y; cin >> y;
                    qry += y;
                    qr.push_back({ a,qry });
                }
                else {
                    int z; cin >> z;
                    z += qry;
                    qr.push_back({ a,z });
                }
            }
        }
    }
    for (int i = 0; i < ans.size(); ++i) {
        stlist[i+1][0] = ans[i].second;
    }
    for (int j = 1; j <= 18; j++)
        for (int i = 1; i + (1 << j) - 1 <= ans.size() + 1; i++)//注意这里要控制边界 
            stlist[i][j] = max(stlist[i][j - 1], stlist[i + (1 << (j - 1))][j - 1]);
    int nowpop = 0;
    int nowsum = 0;
    for (auto& q : qr) {
        int k = q.first;
        int now = q.second;
        if (k == 2) {
            nowpop = now;
        }
        if (k == 3) {
            nowsum = now;
            int pos = lower_bound(ans.begin(), ans.end(), make_pair(nowsum,0LL)) - ans.begin();

            int rem = ans[pos].first - nowsum;
            cout << ans[pos].second - rem << "\n";
        }
        if (k == 4) {
            int pos = upper_bound(ans.begin(), ans.end(), make_pair(nowpop, 0LL)) - ans.begin();
            int l = pos + 1, r = now;
            cout <<Query(l,r)<<"\n";
        }
    }
}
signed main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n = 1; //cin >> n;
    while (n--)solve();
    return 0;
}

https://www.luogu.com.cn/paste/fbd7f75h

2023/10/26 00:52
加载中...