Submission #490166

#TimeUsernameProblemLanguageResultExecution timeMemory
490166Killer2501Index (COCI21_index)C++14
110 / 110
420 ms38432 KiB
#include<bits/stdc++.h>
#define pll pair<ll, ll>
#define fi first
#define se second
#define pb push_back
#define task "test"
#define pii pair<pll, pll>
using namespace std;
using ll = int;
const int  N = 2e5+5;
const ll mod = 1e8+7;
const ll base = 113;
const ll inf = 1e9;
ll m, n, t, k, a[N], tong, d[N], b[N], h[N], ans, x[N], y[N], l[N], r[N];
vector<ll> adj[N];
vector<ll> kq, ask[N];
vector<pll> val;
ll pw(ll k, ll n, ll md)
{
	ll total = 1;
	for(; n; n >>= 1)
	{
		if(n&1)total = total * k % md;
		k = k * k % md;
	}
	return total;
}
struct BIT
{
    ll n;
    vector<ll> fe;
    BIT(ll _n)
    {
        n = _n;
        fe.resize(n+1, 0);
    }
    void reset()
    {
        fill(fe.begin(), fe.end(), 0);
    }
    void add(ll id, ll x)
    {
        for(; id <= n; id += id & -id)fe[id] += x;
    }
    ll get(ll id)
    {
        ll total = 0;
        for(; id ; id -= id & -id)total += fe[id];
        return total;
    }
};
string s;
void sol()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        val.pb({a[i], i});
    }
    sort(val.begin(), val.end());
    for(int i = 1; i <= m; i ++)
    {
        cin >> x[i] >> y[i];
        l[i] = 1;
        r[i] = n;
    }
    bool ok = true;
    BIT bit(n);
    while(ok)
    {
        ok = false;
        for(int i = 1; i <= m; i ++)
        {
            if(l[i] <= r[i])
            {
                ok = true;
                adj[(l[i]+r[i])/2].pb(i);
            }
        }
        for(int i = n, j = n-1; i > 0; i --)
        {
            while(j >= 0 && val[j].fi >= i)
            {
                bit.add(val[j].se, 1);
                --j;
            }
            for(ll p: adj[i])
            {
                if(bit.get(y[p])-bit.get(x[p]-1) >= i)l[p] = i+1;
                else r[p] = i-1;
            }
            adj[i].clear();
        }
        bit.reset();
    }
    for(int i = 1; i <= m; i ++)cout << r[i] << '\n';
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    if(fopen(task".inp", "r"))
    {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int ntest = 1;
    //cin >> ntest;
    while(ntest -- > 0)
    sol();
}

Compilation message (stderr)

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