結果

問題 No.878 Range High-Element Query
ユーザー mtsdmtsd
提出日時 2019-09-06 22:34:30
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,645 bytes
コンパイル時間 1,405 ms
コンパイル使用メモリ 99,064 KB
実行使用メモリ 20,212 KB
最終ジャッジ日時 2023-09-07 01:23:11
合計ジャッジ時間 5,162 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>
#include <deque>
#include <iomanip>
#include <cstdio>
#include <stack>
#include <numeric>
#include <cassert>
using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf 1000000007
#define mod 1000000007
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define int long long

template<typename T> class segtree{
private:
    int n,sz;
    vector<T> node;
public:
    segtree(vector<T>& v) : sz((int)v.size()){
        n = 1;
        while(n < sz){
            n *= 2;
        }
        node.resize(2*n-1,0);
        for(int i = 0; i < sz; i++){
            node[i+n-1] = v[i];
        }
        for(int i=n-2; i>=0; i--){
            node[i] = node[i*2+1]+node[i*2+2];
        }
    }
    void update(int k,T a){
    	k += n-1;
    	node[k] = a;
    	while(k>0){
            k = (k-1)/2;
            node[k] = node[2*k+1]+node[2*k+2];
    	}
    }
    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r < 0) r = n;
    	if(r <= a || b <= l){
            return 0;
    	}
    	if(a <= l && r <= b){
            return node[k];
    	}else{
            T vl = query(a,b,2*k+1,l,(l+r)/2);
            T vr = query(a,b,2*k+2,(l+r)/2,r);
            return vl+vr;
    	}
    }
    void print(){
        for(int i = 0; i < sz; i++){
            cout<<query(i,i+1)<< " ";
        }
        cout<<endl;
    }
};


signed main(){
    int n,q;
    cin >> n >> q;
    vector<int>a(n);
    rep(i,n)cin >> a[i];
    set<pair<int,int> > st;
    vector<int>b(n);
    st.insert(MP(-1,-1));
    rep(i,n){
        auto x = st.upper_bound(MP(a[i],inf));
        if(x==st.end()){
            b[i] = 0;
        }else{
            b[i] = -(*x).second+1;
        }
        st.insert(MP(a[i],-i));
        //cerr << b[i] << endl;
    }
    vector<pair<pair<int,int>,int> >  s;
    rep(i,q){
        int a,l,r;
        cin >> a >> l >> r;
        l--;
        r--;
        s.push_back(MP(MP(l,r),inf+i));
    }
    rep(i,n){
        s.push_back(MP(MP(b[i],-1),i));
    }
    sort(s.begin(),s.end());
    vector<int>c(n);
    segtree<int>sg(c);
    vector<int>res(q);
    rep(i,n+q){
        if(s[i].first.second==-1){
            //cerr << i << " " << s[i].second << endl;
            sg.update(s[i].second,1);
        }else{
            res[s[i].second-inf] = sg.query(s[i].first.first,s[i].first.second+1);
        }
    }
    rep(i,q){
        cout << res[i] << endl;
    }
    return 0;
}
0