Submission #477509

# Submission time Handle Problem Language Result Execution time Memory
477509 2021-10-02T10:52:29 Z 2548631 Poklon (COCI17_poklon) C++17
0 / 140
5000 ms 12296 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<vii> vvii;

#define fastIO ios::sync_with_stdio(false), cin.tie(NULL)
#define forw(i, l, r) for( int i = (l) ; i < (r) ; i++ )
#define forb(i, r, l) for( int i = (r) ; i >= (l) ; i-- )
#define log2i(x) (64 - __builtin_clzll(1ll * (x)) - 1)
#define numBit(x) (__builtin_popcountll(1ll * (x)))
#define getBit(x, i) ((x) >> (i) & 1)
#define Pi acos(-1.0l)
#define sz(x) int(x.size())
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define debug(x) cerr << #x << " = " << x << '\n';

const int N = 5e5 + 7;
const int block = 800;
int n, q;
int a[N], pos[N], ans[N], cnt[N];
vi tmp;
pii qry[N];

bool cmp(int x, int y) {
    if(qry[x].fi / block == qry[y].fi / block) {
        if(qry[x].fi / block & 1) return (qry[x].se > qry[y].se);
        else return (qry[x].se < qry[y].se);
    }
    return qry[x].fi / block < qry[y].fi / block;
}

int main() {
    fastIO;
#ifndef ONLINE_JUDGE
    //freopen("btd.inp", "r", stdin);
    //freopen("btd.out", "w", stdout);
#endif

    cin >> n >> q;
    forw(i, 1, n + 1) cin >> a[i], tmp.pb(a[i]);
    sort(all(tmp)); tmp.resize(unique(all(tmp)) - tmp.begin());
    forw(i, 1, n + 1) a[i] = lower_bound(all(tmp), a[i]) - tmp.begin();

    forw(i, 0, q) cin >> qry[i].fi >> qry[i].se, pos[i] = i;
    sort(pos, pos + q, cmp);

    forw(i, qry[pos[0]].fi, qry[pos[0]].se + 1) {
        if(cnt[a[i]] == 2) ans[pos[0]]--;
        cnt[a[i]]++;
        if(cnt[a[i]] == 2) ans[pos[0]]++;
    }
    int l = qry[pos[0]].fi, r = qry[pos[0]].se;
    forw(i, 1, q) {
        ans[pos[i]] = ans[pos[i - 1]];
        forw(j, min(l, qry[pos[i]].fi), max(l, qry[pos[i]].fi)) {
            if(cnt[a[j]] == 2) ans[pos[i]]--;
            cnt[a[j]] += (l < qry[pos[i]].fi ? -1 : 1);
            if(cnt[a[j]] == 2) ans[pos[i]]++;
        }

        forw(j, min(r, qry[pos[i]].se) + 1, max(r, qry[pos[i]].se) + 1) {
            if(cnt[a[j]] == 2) ans[pos[i]]--;
            cnt[a[j]] += (r > qry[pos[i]].se ? -1 : 1);
            if(cnt[a[j]] == 2) ans[pos[i]]++;
        }
    }

    forw(i, 0, q) cout << ans[i] << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 332 KB Output isn't correct
2 Incorrect 2 ms 332 KB Output isn't correct
3 Incorrect 5 ms 332 KB Output isn't correct
4 Incorrect 85 ms 500 KB Output isn't correct
5 Execution timed out 5047 ms 2844 KB Time limit exceeded
6 Execution timed out 5049 ms 2756 KB Time limit exceeded
7 Execution timed out 5049 ms 5152 KB Time limit exceeded
8 Execution timed out 5046 ms 7440 KB Time limit exceeded
9 Execution timed out 5044 ms 9900 KB Time limit exceeded
10 Execution timed out 5050 ms 12296 KB Time limit exceeded