답안 #818824

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
818824 2023-08-10T06:51:50 Z vjudge1 Poklon (COCI17_poklon) C++17
140 / 140
663 ms 68376 KB
#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()
#define MASK(n) (1 << (n))
#define BIT(mask,x) ((mask << (x)) & 1)
#define TASK "task"

using namespace std;
const int mxN = 5e5 + 7;
const int base = 512;
const int inf = 1e9 + 6969;
const int Mod = 1e9 + 7;
const long long infll = 1e18 + 6969;

template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
     if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
     if (a < b) {a = b; return true;} return false;
}
#define int long long

int n;
int q;
int a[mxN];
int idx[mxN];
int pre[mxN];
int ans[mxN];
vector<int>v;
vector<pair<int,int>>queries[mxN];

struct segment {
	int n;
	vector<int>Node;

	segment (int _n) {
		n = _n;
		Node.assign(4 * n,0);
	}

	void update(int id,int l,int r,int u,int v) {
		if(u < l || u > r) return;
		if(l == r) {
			Node[id] += v;
			return;
		}
		int mid = (l + r) >> 1;
		update(id * 2,l,mid,u,v);
		update(id * 2 + 1,mid + 1,r,u,v);
		Node[id] = Node[id * 2] + Node[id * 2 + 1];
	}

	int get(int id,int l,int r,int u,int v) {
		if(v < l || u > r) return 0;
		if(u <= l && r <= v) return Node[id];
		int mid = (l + r) >> 1;
		int lef = get(id * 2,l,mid,u,v);
		int rig = get(id * 2 + 1,mid + 1,r,u,v);
		return lef + rig;
	}
};

void solve() {
	cin >> n >> q;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		v.pb(a[i]);
	}
	sort(all(v));
	segment seg(n + 2);
	for (int i = 1; i <= q; i++) {
		int u,v;
		cin >> u >> v;
		queries[v].pb(mp(u,i));
	}
	for (int i = 1; i <= n; i++) {
		int p = lower_bound(all(v),a[i]) - v.begin() + 1;
		idx[i] = pre[p];
		pre[p] = i;
	}
	for (int r = 1; r <= n; r++) {
		int i = idx[r];
		int j = idx[i];
		int z = idx[j];
		if(i > 0) seg.update(1,1,n,i,1);
		if(j > 0) seg.update(1,1,n,j,-2);
		if(z > 0) seg.update(1,1,n,z,1);
		for (auto it : queries[r]) ans[it.se] = seg.get(1,1,n,it.fi,r);
	}
	for (int i = 1; i <= q; i++) cout << ans[i] << "\n";
}

signed main() {
	ios_base :: sync_with_stdio(0);
	cin.tie(0);
	int tc = 1;
	//cin >> tc;
	while(tc--) {
		solve();
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11988 KB Output is correct
2 Correct 6 ms 12124 KB Output is correct
3 Correct 6 ms 12116 KB Output is correct
4 Correct 10 ms 12560 KB Output is correct
5 Correct 95 ms 22732 KB Output is correct
6 Correct 103 ms 22936 KB Output is correct
7 Correct 285 ms 35008 KB Output is correct
8 Correct 358 ms 46620 KB Output is correct
9 Correct 510 ms 57520 KB Output is correct
10 Correct 663 ms 68376 KB Output is correct