QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#442223#8749. 贸易FAKUMARER#TL 2ms9024kbC++141.5kb2024-06-15 10:20:252024-06-15 10:20:26

Judging History

This is the latest submission verdict.

  • [2024-06-15 10:20:26]
  • Judged
  • Verdict: TL
  • Time: 2ms
  • Memory: 9024kb
  • [2024-06-15 10:20:25]
  • Submitted

answer

#include <bits/stdc++.h>

const int MAXN = 100005;

using namespace std;

int N, M, Cnt;

int A[MAXN], C[MAXN], E[MAXN];

struct Node {
	int L, R, id;
	
	inline bool operator < (const Node &a) const {
		return R < a.R;
	}
} Q[MAXN];

struct node {
	int L, R;
	
	inline bool operator < (const node &a) const {
		return R < a.R;
	}
} I[MAXN];

std::vector <int> P[MAXN];

namespace BIT {
	int T[MAXN];
	
	#define lowbit(x) (x & -x)
	
	inline void Add (int x) {
		while (x <= N) T[x] ++, x += lowbit (x);
	}
	
	inline int Que (int x) {
		int Ret = 0;
		while (x) Ret += T[x], x -= lowbit (x);
		return Ret;
	}
	
	inline int Sum (const int L, const int R) {
		return Que (R) - Que (L - 1);
	}
}

int Ans[MAXN];

int main () {
	
	cin >> N >> M;
	
	for (int i = 1; i <= N; ++ i) cin >> A[i];
	
	for (int i = 1; i <= N; ++ i) cin >> C[i];
	
	for (int i = 1; i <= N; ++ i) {
		if (A[i] == 0) P[C[i]].push_back (i);
		else if (P[C[i]].size ()) E[i] = P[C[i]].back (), P[C[i]].pop_back ();
	}
	
	for (int i = 1; i <= N; ++ i) 
		if (E[i]) I[++ Cnt] = {E[i], i};
	
	for (int i = 1; i <= M; ++ i) 
		cin >> Q[i].L >> Q[i].R, Q[i].id = i;
	
	sort (I + 1, I + Cnt + 1);
	sort (Q + 1, Q + M   + 1);
	
	int P1 = 1, P2 = 1;
	
	for (int i = 1; i <= N; ++ i) {
		while (P1 <= Cnt && I[P1].R <= i) BIT::Add (I[P1].L), ++ P1;
		while (P2 <= M   && Q[P2].R <= i) Ans[Q[P2].id] = BIT::Sum (Q[P2].L, i), ++ P2;
	}
	
	for (int i = 1; i <= M; ++ i) cout << Ans[i] << '\n';
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 9024kb

input:

10 5
1 1 0 0 0 0 0 1 1 1
1 1 1 1 1 1 1 1 1 1
4 6
2 4
2 6
7 10
4 7

output:

0
0
0
1
0

result:

ok 5 lines

Test #2:

score: -100
Time Limit Exceeded

input:

20 500000
1 0 0 1 0 0 0 0 1 0 1 0 1 0 1 0 1 0 0 0
1 2 1 2 2 1 1 2 1 1 1 2 2 1 2 2 2 1 1 2
13 20
8 9
1 7
5 13
3 10
3 19
14 15
1 5
9 17
7 10
6 6
8 20
1 17
13 20
4 6
16 20
7 14
2 16
3 17
11 12
1 1
15 20
11 15
2 12
2 15
8 16
9 12
9 13
10 19
12 19
9 13
4 8
2 2
19 19
9 17
4 20
4 14
4 8
6 13
13 17
15 16
13...

output:


result: