题解:P4848 崂山白花蛇草水

· · 题解

虽然感觉是不太对的做法,但总算是手切了 OI 萌新的第二道黑...

题意

给定一个边长为 N (N\le500000) 的方阵和 Q (Q\le100000) 次询问,每次询问可能是以下两种情形:

强制在线。

解法

注意到:

所以考虑 vector 乱搞。

具体地来说,对于询问 1,直接向 vector 中以 k 为关键字插入,此时 vector 是 k-有序的;

对于询问 2,从 vector 的尾部开始计 k 个在对应区域内的点。

AC Code

#include<bits/stdc++.h>
using namespace std;

const int N=1e5+9;
int ans,q,opt,x_1,y_1,x_2,y_2,_k;
struct node{
    int x,y,k;
    bool operator< (const node& other) const{
        return k<other.k;
    }
};
vector<node> a;

void decrypt(vector<int*> v){
    for(auto at:v) *at^=ans;
}

signed main(){
    cin.tie(0)->sync_with_stdio(0);
    a.reserve(N);
    cin>>opt>>q;
    while(q--){
        cin>>opt;
        if(opt==1){
            cin>>x_1>>y_1>>_k;
            decrypt({&x_1,&y_1,&_k});
            node nd={x_1,y_1,_k};
            a.insert(upper_bound(a.begin(),a.end(),nd),nd);
        }
        else{
            cin>>x_1>>y_1>>x_2>>y_2>>_k;
            decrypt({&x_1,&y_1,&x_2,&y_2,&_k});
            int i=a.size();
            while(~--i&&_k){
                _k-=(a[i].x>=x_1&&a[i].y>=y_1&&a[i].x<=x_2&&a[i].y<=y_2);
            }
            if(_k){
                cout<<"NAIVE!ORZzyz.\n";
                ans=0;
                continue;
            }
            ans=a[i+1].k;
            cout<<ans<<'\n';
        }
    }
    return 0;
}

复杂度分析

时间复杂度 O(Q^2),空间复杂度 O(Q)。