Zoj 3940 Modulo Query

这个题在场外做同步的时候想到了几乎标程的写法,但是因为不会证明复杂度是nlog(n)log(M)并且以为是n^2的而没写感觉略可惜(其实就是自己太弱


具体这题为什么这么写以及复杂度,丢个链接就跑



#include
using namespace std;

map M;
vector > ask;

const int mod = 1e9+7;
#define LL long long

int main(){
    int T;
    scanf("%d",&T);
    int n,m;
    while(T-- && ~scanf("%d %d",&n,&m)){
        M.clear();
        M[m+1] = 1;
        int x;
        while(n--){
            scanf("%d",&x);
            while(M.rbegin()->first > x){
                auto v = *M.rbegin();
                M.erase(v.first);
                M[x]+=v.second*(v.first / x);
                if(v.first % x)
                    M[v.first % x] += v.second;
            }
        }
        LL ans = 0;
        int Q;
        ask.clear();
        scanf("%d",&Q);
        for(int i=1;i<=Q;i++){
            scanf("%d",&x);
            ask.push_back(make_pair(x,i));
        }
        sort(ask.rbegin(),ask.rend());
        auto its = M.rbegin();
        int cnt = 0;
        for(auto it = ask.begin();it!=ask.end();it++){
            while(its != M.rend() && its->first > it->first){
                cnt += its->second;
                its ++;
            }
            (ans += (LL)it->second * cnt) %= mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


你可能感兴趣的:(--数学---,--数据结构---)