Submission #489603

#TimeUsernameProblemLanguageResultExecution timeMemory
489603cpp219Index (COCI21_index)C++14
110 / 110
620 ms41000 KiB
#include<bits/stdc++.h>
#define ll int
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
const ll N = 2e5 + 9;
const ll inf = 1e9 + 7;

vector<ll> g[N],num[N];
ll n,Q,a[N],l[N],h[N],bit[N];
LL q[N];

void upd(ll p,ll val){
    for (ll i = p;i <= n;i += i & -i) bit[i] += val;
}
ll Get(ll p){
    ll res = 0;
    for (ll i = p;i > 0;i -= i & -i) res += bit[i];
    return res;
}
ll Query(ll l,ll r){
    return Get(r) - Get(l - 1);
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "tst"
    if (fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        //freopen(task".out","w",stdout);
    }
    cin>>n>>Q;
    for (ll i = 1;i <= n;i++) cin>>a[i],num[a[i]].push_back(i);
    for (ll i = 1;i <= Q;i++){
        cin>>q[i].fs>>q[i].sc;
        l[i] = 1; h[i] = n;
    }
    while(1){
        bool flag = 0; memset(bit,0,sizeof(bit));
        for (ll i = 1;i <= Q;i++){
            ll mid = (l[i] + h[i])/2;
            if (l[i] <= h[i])
                g[mid].push_back(i),flag = 1;
        }
        if (!flag) break;
        for (ll i = N - 1;i > 0;i--){
            for (auto j : num[i]) upd(j,1);
            for (auto j : g[i]){
                //debug(Query(q[j].fs,q[j].sc));
                if (Query(q[j].fs,q[j].sc) >= i) l[j] = i + 1;
                else h[j] = i - 1;
            }
            g[i].clear();
        }
    }
    for (ll i = 1;i <= Q;i++) cout<<h[i]<<"\n";
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Compilation message (stderr)

index.cpp: In function 'int main()':
index.cpp:32:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   32 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...