Submission #1276133

#TimeUsernameProblemLanguageResultExecution timeMemory
1276133chanhchuong123Poklon (COCI17_poklon)C++20
140 / 140
349 ms32880 KiB
#include<bits/stdc++.h>
using namespace std;

const bool multiTest = false;
#define task "C"
#define fi first
#define se second
#define MASK(i) (1LL << (i))
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define BIT(mask, i) ((mask) >> (i) & 1)

template<typename T1, typename T2> bool minimize(T1 &a, T2 b) {
	if (a > b) a = b; else return 0; return 1;
}
template<typename T1, typename T2> bool maximize(T1 &a, T2 b) {
	if (a < b) a = b; else return 0; return 1;
}

const int MAX = 500050;
int N, Q;
int A[MAX];
int pre[MAX];
int ans[MAX];
int bit[MAX];
int last[MAX];
vector<int> values;
vector<pair<int, int>> queries[MAX];

void update(int id, int delta) {
    for (; id <= N; id += id & -id)
        bit[id] += delta;
}

int get(int id) {
    int res = 0;
    for (; id > 0; id -= id & -id)
        res += bit[id];
    return res;
}

void process(void) {
    cin >> N >> Q;
    for (int i = 1; i <= N; ++i) {
        cin >> A[i];
        values.push_back(A[i]);
    }
    for (int i = 1; i <= Q; ++i) {
        int l, r; cin >> l >> r;
        queries[r].emplace_back(l, i);
    }

    sort(all(values)); values.resize(unique(all(values)) - values.begin());
    for (int i = 1; i <= N; ++i) {
        A[i] = lower_bound(all(values), A[i]) - values.begin() + 1;
        if (last[A[i]]) {
            if (pre[pre[last[A[i]]]]) update(pre[pre[last[A[i]]]], +1);
            if (pre[last[A[i]]]) update(pre[last[A[i]]], -2);
            update(last[A[i]], +1);
        }
        for (auto &[j, id]: queries[i]) {
            ans[id] = get(i) - get(j - 1);
        }
        pre[i] = last[A[i]]; last[A[i]] = i;
    }

    for (int i = 1; i <= Q; ++i) {
        cout << ans[i] << '\n';
    }
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	if (fopen(task".inp", "r")) {
		freopen(task".inp", "r",  stdin);
		freopen(task".out", "w", stdout);
	}

	int nTest = 1; if (multiTest) cin >> nTest;
	while (nTest--) {
		process();
	}

	return 0;
}

Compilation message (stderr)

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