We need to count for each color c, the number of intervals [l, r] (1-indexed, non-empty) such that the dominant color of that interval is c.
Definition: For an interval, consider the frequencies of each color in that interval. The dominant color is the one with maximum frequency; if there is a tie (multiple colors have the same max frequency), the smallest color number is chosen.
We need to count over all O(n^2) intervals, n up to 5000, so O(n^2) algorithms are acceptable if constant is low. But we need to compute per interval the dominant color. Straight O(n^3) is too slow.
We need O(n^2) or O(n^2 log n) approach.
Since n=5000, n^2 = 25e6, which is borderline but possibly okay in C++ if operations are simple. However we need to compute dominant color for each interval efficiently. Perhaps we can iterate over left index l, and maintain frequencies as we extend r to the right, updating the current dominant color. But updating the dominant color when adding one element: we increment count of color t_r. Then we need to know if that color becomes the new max frequency, or ties, and if ties, the smallest index among those with max freq.
We can maintain the current frequency array freq[1..n], and also maintain the current max frequency value maxfreq, and perhaps a data structure to know which colors have that maxfreq and what is the smallest index among them. But since we extend r from l to n, the total steps are O(n^2). For each step (l,r) we can update in O(1) maybe with some tracking.
Idea: For each left l, start with freq all zeros, maxfreq=0, dominantColor=0. Then for r=l..n:
- add color c = t_r; freq[c]++.
- After increment, compare freq[c] with maxfreq.
- If freq[c] > maxfreq: maxfreq = freq[c]; dominantColor = c.
- Else if freq[c] == maxfreq: if c < dominantColor, set dominantColor = c.
- Then increment answer[dominantColor] by 1.
But wait, is that correct? Consider case where there is a tie between several colors and the maxfreq remains same, but after increment, maybe the previous maxfreq color is still at maxfreq but the new color ties, and its index might be smaller than previous dominantColor. Then we should update dominantColor accordingly. That's covered: if freq[c]==maxfreq and c<dominantColor -> update.
What about scenario where after increment, the maxfreq remains same but the previous dominantColor may have been a color that is still at maxfreq but maybe the new color is larger index, we keep the same. Good.
But there is also scenario where maxfreq might be increased to a new value from a color that is not the one we just incremented? Actually maxfreq can only increase when we increment a color; if that color becomes the sole highest frequency, then it's correct; if it ties with previous max, we handle tie; if it becomes lower than maxfreq, no change. So it seems updating as above works.
But is there a case where after increment, the maxfreq does not change, but the previous dominantColor might no longer be among colors with maxfreq? That would happen if the previous dominantColor was not actually at maxfreq? Actually dominantColor is defined as the color among those with current maxfreq that has smallest number. So if after increment, the maxfreq remains same (meaning no color exceeded previous maxfreq), then all colors with maxfreq remain at maxfreq (the newly incremented color might become maxfreq if it ties, but previous maxfreq colors are still there). But could it be that the previous dominantColor's frequency is less than maxfreq? That would mean that previously we had a maxfreq M, and dominantColor was a color with freq=M. After increment, maxfreq remains M, but maybe the previous dominantColor's frequency is still M (since we didn't decrement it). So it's still among the maxfreq colors. Unless we incremented some other color and that caused maxfreq to increase? That would have changed maxfreq. So the only possible change is that new color joins the maxfreq tie; we need to possibly change to a smaller color. The update condition covers that.
So the algorithm per left start works: O(n^2) total updates. For each left l, we reset frequency array of size n to zero. That's O(n) per left if we memset, but that would be O(n^2) anyway; but we can do resetting by using timestamps or just reinitialize because n=5000 and n^2=25e6, resetting an array of 5000 per left yields 5000*5000 = 25e6, which is okay. But careful: We would do for each l: freq[1..n] = 0; then loop r from l to n. That's n^2 assignments for resetting plus n^2 updates. That's about 50 million operations, likely okay in C++. But we need to avoid using vector etc. Use int arrays.
Alternatively we could avoid resetting by using an array of last-seen timestamps. But simplicity: just use a vector freq(n+1,0) and for each l, we loop r and after finishing we would need to clear frequencies. But we can't just assign zeros because we would have to clear all colors that were incremented. Since there are O(n) increments per left, we could clear them as we go backwards? Instead, we can reinitialize freq each left by zeroing only the colors that were actually used. But that adds complexity. But given constraints, n=5000, n^2=25e6, and resetting an array of 5000 ints for each left (5000 times) is 25e6 assignments, plus the main loops (n^2/2 approx 12.5e6 increments and checks). That's total ~37.5e6 operations, which is fine.
Memory: O(n) for answer.
We must ensure integer type: answer can be up to n*(n+1)/2 = ~12.5e6, fits in int (32-bit). Actually n=5000 gives 12.5 million, fits in int.
Edge cases: n=1: output 1 for that color, 0 others.
Let's test with sample:
n=4, t=[1,2,1,2].
For l=1:
- r=1: add 1: freq1=1,maxf=1,dom=1 -> ans[1]++ (now1)
- r=2: add2: freq2=1, freq1=1, freq2==maxf=1 and 2>1? Actually c=2,dom=1, c<dom? false, so dom stays 1. ans[1]++ (2)
- r=3: add1: freq1=2, maxf=2, dom=1, ans[1]++ (3)
- r=4: add2: freq2=2, freq2==maxf=2 and 2<1? false, dom=1, ans[1]++ (4)
So intervals starting at 1: all have dominant 1.
l=2:
- freq zero, start
- r=2: add2: maxf=1,dom=2, ans[2]++ (1)
- r=3: add1: freq1=1, freq2=1, maxf=1, dom=2 (since tie, smallest index=1? Wait dom=2 currently; we have tie: freq1=1, freq2=1. The smallest color among those with maxfreq is 1, but we haven't updated because we only update when we add a color that ties and is smaller. In this step, we added color 1. After increment, freq1=1, freq2=1, maxf=1. We need to determine if dom should change. Our algorithm: after increment of c=1, compare freq[c] (1) with maxf (1). Since freq[c]==maxf and c<dom (1<2) => dom=1. So we update. Good. So dom becomes 1. ans[1]++ (5)
- r=4: add2: freq2=2, freq1=1, now freq2=2 > maxf=1 => maxf=2, dom=2, ans[2]++ (2)
So intervals: [2,2] dom2; [2,3] dom1; [2,4] dom2.
l=3:
- r=3: add1: maxf=1,dom=1, ans[1]++ (6)
- r=4: add2: freq1=1, freq2=1, tie maxf=1. Added c=2, freq2==maxf and c<dom? 2<1 false, so dom stays 1. ans[1]++ (7)
l=4:
- r=4: add2: maxf=1,dom=2, ans[2]++ (3)
So totals: ans1=7, ans2=3, ans3=0, ans4=0. matches sample.
Second sample: n=3, all 1.
l=1: r=1->dom1; r=2->dom1; r=3->dom1 -> ans[1]+=3
l=2: r=2->dom1; r=3->dom1 -> ans[1]+=2
l=3: r=3->dom1 -> ans[1]+=1 total 6. Others 0. Works.
So algorithm is correct.
Time Complexity: O(n^2). For n=5000, 25 million inner loop iterations, each does a few integer ops: freq[c]++, some comparisons. Might be okay in 1-2 seconds in C++ with optimization (like using local variables, arrays, possibly using short int?). But we also need to reset freq array each iteration. Resetting 5000 ints per left: 5000*5000=25 million assignments. That doubles the operations to ~50 million. Still likely okay. But we can reduce by not resetting all. Instead we can maintain a global freq array and use a timestamp technique: For each color, store its frequency value and a version/timestamp indicating which left iteration it corresponds to. Or we can just allocate a new vector for each left? That would be expensive due to memory allocation.
Alternatively, we can do two nested loops and for each left, we start with freq all zeros, but we can zero only the colors that appear in the range from l to n. Since each color appears at most n times, total zeros across all left might be O(n^2) anyway. But we can avoid the O(n) resetting per left by using an array of length n+1 and a separate array "vis" to know which entries have been modified, then after each left we reset only those that were changed. In the inner loop, we modify freq[t[r]]. The number of distinct colors modified per left is at most (r-l+1) which at most n. So resetting only modified ones would be O(n) per left, which is same as resetting the whole array of length n. But resetting whole array uses memset which is efficient (maybe 5000*5000 ~ 25 million bytes, each int 4 bytes -> 100 MB of writes). That's okay.
I'll implement straightforward: for l from 1 to n: create freq array of size n+1 initialized to 0 (maybe we allocate once and use fill with zero each iteration). Use vector freq(n+1,0) then for each l, fill(freq.begin(), freq.end(), 0) which is O(n). That's fine.
But careful: Using fill on a vector of 5001 each iteration may be okay.
Alternatively, we can avoid resetting by using the approach of iterating left to right but also maintaining that we only need to keep frequencies from l to current r. Actually we can do: for each left, we extend r; we never need to reset because we start fresh for each left. So we need to reinitialize freq to all zeros for each left. So either we allocate a new vector each iteration (which may cause many allocations) or we reuse one and reset.
Implementation details:
- Read n.
- Read array t (size n, 1-indexed or 0-indexed). We'll use 1-index for simplicity.
- Create ans vector of size n+1 (1-indexed) initialized to 0.
- For l = 1 to n:
- vector freq(n+1, 0) // creates and zeros each time, O(n) constructor.
- int maxfreq = 0, dominant = 0;
- for r = l to n:
c = t[r];
++freq[c];
if (freq[c] > maxfreq) {
maxfreq = freq[c];
dominant = c;
} else if (freq[c] == maxfreq && c < dominant) {
dominant = c;
}
++ans[dominant];
- end
But this will allocate a new vector each iteration: n=5000 => 5000 vectors each size ~5001, total memory allocation ~ 500050014 = 100 MB? Actually each vector allocation is separate; we allocate and deallocate each time, which might be okay but may cause overhead. Better to reuse a single vector and assign zeros.
Simplify:
vector freq(n+1);
for l = 1..n {
fill(freq.begin(), freq.end(), 0);
maxfreq = 0; dominant = 0;
for r = l..n { ... }
}
That's fine.
Potential optimization: Use plain C array and memset (since int, need to zero bytes). But fill is okay.
Alternatively, we could use short int for freq because max frequency in any interval <= n <= 5000, fits in 16-bit (max 5000). So we could use vector to reduce memory and maybe speed. But operations are int anyway.
Now check correctness for tie-breaking: We always update dominant when we see a new color with same maxfreq and smaller index. However, consider case where the maxfreq does not change, but the previous dominant might have been the smallest among those with maxfreq, but later we increment a color that is not currently maxfreq, but that increment does not affect maxfreq. Then the dominant remains correct. So okay.
But is there a scenario where after increment, the maxfreq remains same but the previous dominant may no longer be among maxfreq? That would happen if the previous dominant's frequency was at maxfreq but after increment, the maxfreq increased? That would change maxfreq. So not.
What about when we increment a color that was previously the sole maxfreq, and after increment, its frequency becomes higher than the previous maxfreq, but there could be other colors that also have that new maxfreq? Actually if we increment the current maxfreq color, its frequency increases, making new maxfreq = old maxfreq+1. No other color can have that because the previous maxfreq was the maximum. So the dominant becomes that color (since it's the only one with new maxfreq). Our condition: freq[c] > maxfreq triggers, set dominant = c. So correct.
What about when we increment a color that is not the current maxfreq, and its new freq becomes equal to the current maxfreq? Then we have tie between that color and existing maxfreq colors. Need to set dominant to smallest index among them. Our condition: freq[c] == maxfreq, if c < dominant then set dominant = c. But what if the existing dominant is not the smallest among the maxfreq colors? Actually by our maintenance, after each step, dominant is the smallest among colors with maxfreq. Let's verify: Initially, maxfreq=0, dominant=0 (no color). After first addition, we set dominant to the color. That's smallest among maxfreq (just one). Then when we add new colors, we maintain: if freq[c] > maxfreq, set dominant=c (since it's sole max). If freq[c] == maxfreq, we compare with dominant. However, is it possible that the previous dominant was not actually the smallest among maxfreq colors? If we always update when we see a smaller color, we keep the smallest. But what if a color that was previously the smallest is no longer at maxfreq? That would happen only if that color's freq dropped, but we never decrement frequencies, only increment. So a color once reaching maxfreq stays at maxfreq? Actually if maxfreq increases, that color may no longer be at the new maxfreq, but then we update dominant accordingly. If maxfreq stays same, all colors that were at maxfreq remain at maxfreq because frequencies only increase; they never decrease. So a color that had maxfreq continues to have at least that freq. So if we keep dominant as the smallest color among those that currently have maxfreq, it remains correct as we add elements: when a new color reaches maxfreq, we compare if it's smaller than current dominant; if so, update. If not, dominant stays as the smallest among the set (which might be existing dominant, as it's <= the new color). So maintenance is correct.
Thus algorithm works.
Time complexity: O(n^2) ~ 25 million steps. With inner operations being few increments and condition checks, it should pass.
Potential improvements: We could also precompute prefix sums to answer frequency queries quickly? That would be O(1) per interval but then we'd need to compute maxfreq, which is not trivial. So the sliding window update method is good.
Edge Cases: n=5000, all colors same => for each left, we will have freq[1] increases, maxfreq increments, dominant always 1. So inner loop does simple increments and checks; will be fast.
n=5000, all colors distinct? Actually colors are 1..n each appearing once? But ti can be from 1..n, they can be permutation. Then for each interval, frequencies are all 0/1, maxfreq=1, dominant is smallest color in interval. So we need to compute count of intervals where smallest color is a given value. That could be done differently but our algorithm will compute it correctly: For each interval, we update as we go; it will maintain maxfreq=1, and dominant will be the smallest color seen so far? Let's see: Suppose we have colors 3,1,2. Starting at l=1, r=1: add3 -> maxf=1, dom=3. r=2: add1 -> freq1=1, maxf=1, compare: c=1 < dom(3) => dom=1. That's indeed smallest color in interval (1). r=3: add2 -> freq2=1, maxf=1, compare: c=2 < dom(1)? no, dom stays 1. So correct.
Thus algorithm works for all.
Now we need to output ans[1..n] separated by spaces.
Implementation details:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector t(n+1);
for (int i=1; i<=n; ++i) cin >> t[i];
vector ans(n+1, 0);
vector freq(n+1, 0);
for (int l=1; l<=n; ++l) {
fill(freq.begin(), freq.end(), 0);
int maxfreq = 0, dominant = 0;
for (int r=l; r<=n; ++r) {
int c = t[r];
++freq[c];
if (freq[c] > maxfreq) {
maxfreq = freq[c];
dominant = c;
} else if (freq[c] == maxfreq && c < dominant) {
dominant = c;
}
++ans[dominant];
}
}
for (int i=1; i<=n; ++i) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
But careful: dominant=0 initially. However we will always increment ans[dominant] after first addition? For the first element added, dominant becomes that color (c), and we increment ans[dominant] (which is valid because dominant is between 1 and n). So ans[0] is never used because we always have at least one element. Good.
But we used vector ans(n+1), index from 0..n. ans[0] stays 0.
Now about resetting: fill(freq.begin(), freq.end(), 0) is O(n). Could use memset(&freq[0], 0, (n+1)*sizeof(int)) which may be faster. But it's fine.
Alternatively, we could avoid resetting altogether by using a different approach: iterate left from n down to 1? Not needed.
Let's test with n=5000 worst-case time: 25 million inner loops, each doing a couple of operations. Might be ~0.1-0.2 seconds in C++ if optimized; but also fill of 5000 ints per left (25 million assignments) might add ~0.1 sec. So total maybe <0.5 sec, acceptable.
But some judge might be slower; still it's well within typical limits for 2 seconds.
We can also micro-opt: use array of short for freq to reduce memory bandwidth? But it's fine.
Potential bug: In the code, we reset freq for each left, but we also need to reset maxfreq and dominant. Already done.
Now confirm with some random small tests by brute force to ensure correctness.
Let's write a quick brute for small n and compare.
But since it's reasoning, I'll assume it's correct.
One more nuance: When we have maxfreq=0, dominant=0. For the first addition, we set maxfreq=1 and dominant=c. For later additions, we might encounter that freq[c] becomes > maxfreq or == maxfreq. That's fine.
Thus answer is correct.
But let's test a tricky tie scenario: colors: 2 1. Intervals: [1,1] dom2; [2,2] dom1; [1,2] frequencies: 2:1,1:1, maxfreq=1, tie, smallest index is 1? Actually colors 1 and 2, smallest number is 1, so dominant=1. So ans1=1, ans2=1. Let's simulate:
n=2 t=[2,1]
l=1: freq all zero, maxf=0,dom=0
r=1: c=2 -> freq2=1 > maxf (0) -> maxf=1,dom=2 -> ans[2]++ (1)
r=2: c=1 -> freq1=1, now maxf=1, freq1==maxf? yes, c=1, dom=2, c<dom? yes -> dom=1 -> ans[1]++ (1)
l=2:
r=2: c=1 -> freq1=1 > maxf (0) -> maxf=1,dom=1 -> ans[1]++ (2)
So ans1=2, ans2=1. But expected? Let's compute manually: Intervals: [1,1] dom2; [2,2] dom1; [1,2] frequencies: color2:1, color1:1, tie, dominant is min color number=1. So color1 appears in intervals: [2,2] and [1,2] => 2; color2 appears only in [1,1] => 1. So our result matches: ans1=2, ans2=1. Good.
Another test: colors: 2 2 1. Intervals:
- [1,1] dom2
- [2,2] dom2
- [3,3] dom1
- [1,2] dom2 (freq2=2, freq1=0) =>2
- [2,3] freq2=1, freq1=1 tie -> dom1 (since 1<2) =>1
- [1,3] freq2=2, freq1=1 => dom2
So ans2: intervals: [1,1],[2,2],[1,2],[1,3] =>4? Actually also maybe [2,3]? That's dom1. So ans2=4, ans1=2? Let's list: intervals total 6. Count per color: color1: [3,3] and [2,3] =>2; color2: the other 4 =>4. Simulate algorithm:
n=3 t=[2,2,1]
l=1:
r=1: c=2 -> dom2 -> ans2=1
r=2: c=2 -> freq2=2 > maxf=1 -> maxf=2, dom2 -> ans2=2
r=3: c=1 -> freq1=1, maxf=2 -> no change, dom2 -> ans2=3
l=2:
reset
r=2: c=2 -> dom2 -> ans2=4
r=3: c=1 -> freq1=1, freq2=1, maxf=1 -> tie, c=1, dom=2? After addition: before addition freq2=1, maxf=1, dom=2. Add c=1, freq1 becomes 1, compare: freq1==maxf and c<dom -> set dom=1 -> ans1++ (1)
l=3:
r=3: c=1 -> dom1 -> ans1++ (2)
So ans1=2, ans2=4. Good.
Thus algorithm is correct.
Now consider performance: Using vector and fill might be fine. But there is a nuance: For each left, we loop r from l to n, and within we increment freq[t[r]]. t[r] is an int between 1 and n. We also do if-else. This is straightforward.
Potential further optimization: We can precompute t array 0-indexed and use array of ints. Use plain C arrays: int freq[5001] maybe static. Since n max 5000, we can declare int freq[5001]; and for each l, we can do memset(freq,0,sizeof(freq)); but careful: sizeof(freq) = 5001*sizeof(int) ~ 20004 bytes, which is small. memset works with bytes, sets each byte to 0, which is correct for zero integer. So that's fast.
But we need to be careful: if we declare int freq[n+1]; variable length array might be allowed but not standard in C++? We can use vector.
Better: vector freq(n+1) outside and use fill with 0.
Let's code accordingly.
We need to output n integers separated by space. Example shows trailing space after last number, but that's okay.
Now I'll consider whether we can improve memory or time more? Not needed.
But there is one nuance: In the condition else if (freq[c] == maxfreq && c < dominant) we rely on dominant being set. Initially dominant is 0, but after first addition, dominant becomes a valid color (>=1). So later when we compare c<dominant, dominant is always >=1. For first addition, freq[c] > maxfreq triggers, so it's fine.
Edge case: if n=0? Not possible.
Now we produce final answer.
But wait, is there any scenario where this O(n^2) solution could exceed time limit? Let's compute approximate operations: n=5000 => number of intervals = 5000*5001/2 = 12,502,500. Actually intervals count is about 12.5 million, not 25 million. Wait: intervals [l,r] with l<=r, total = n(n+1)/2 ≈ 12.5 million. Our algorithm iterates over each left l, and for each left, iterates r from l to n. So total inner loop iterations = n(n+1)/2 ≈ 12.5 million. That's the number of intervals. So we have 12.5 million iterations, not 25 million. My earlier estimate of 25 million was for n^2 (l and r both up to n). Actually it's O(n^2/2). So about 12.5 million intervals.
But we also have the resetting cost: For each left l, we reset freq array of size n+1, that's ~5000 operations per left => 5000*5000 = 25 million assignments. So total operations ~ 37.5 million. That's still fine.
But can we reduce resetting cost? We can avoid resetting entirely by using an array of ints and a separate array of timestamps. For each left, we maintain a current "time" (l). When we access freq[c], we check if its last update was for this l; if not, we treat it as 0 and set the timestamp. This would eliminate the need to zero the whole array. But we need to be careful about maxfreq and dominant resetting. This could be slightly more efficient but not necessary.
Nevertheless, with 12.5 million inner loop and 25 million zeroing, total 37.5 million, in C++ it's trivial. Even with Python it might be borderline but we are using C++. So okay.
But wait, is there any subtlety: In the inner loop, we are using freq[t[r]]; we need to ensure t is 1-indexed and freq is of size n+1.
Implementation details: Use scanf/printf for faster IO? cin/cout with sync false might be fine. But we can use fast IO.
Let's code:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector t(n+1);
for (int i=1; i<=n; ++i) cin >> t[i];
vector ans(n+1, 0);
vector freq(n+1, 0);
for (int l=1; l<=n; ++l) {
fill(freq.begin(), freq.end(), 0);
int maxfreq = 0, dominant = 0;
for (int r=l; r<=n; ++r) {
int c = t[r];
int f = ++freq[c];
if (f > maxfreq) {
maxfreq = f;
dominant = c;
} else if (f == maxfreq && c < dominant) {
dominant = c;
}
++ans[dominant];
}
}
for (int i=1; i<=n; ++i) {
cout << ans[i] << " ";
}
cout << endl;
return 0;
}
But there's a minor optimization: we can compute f = ++freq[c] then use f, rather than freq[c] after increment.
Test with n=4 sample: works.
Now confirm with n=5000 random test: Not needed.
Potential pitfalls: ans array size n+1 and output from 1 to n.
Now, is there any possibility of overflow? ans[i] can be up to total intervals = 12.5 million, fits int.
Thus solution is ready.
But the problem might have a more efficient solution required for n up to 5000? Our solution is O(n^2) with constant factor low, and 5000 is okay.
Let's double-check constraints: n ≤ 5000, time limit unknown but typical Codeforces problems have 2 seconds. Our solution should pass.
One more thing: Could we do even better by iterating from left to right and using some DP? Possibly O(n^2) is intended.
But we must be careful about the memory layout: Using vector for freq, and resetting with fill, each left. However, fill on a vector uses assign, which loops over elements. That's okay.
Alternatively, we can avoid resetting by using the fact that for each left, we only need to keep frequencies for the current range. But as we go to the next left, we need to decrement frequencies of elements left of previous l. Could we do a two-pointer approach? Not straightforward because we need to compute dominant for all intervals, not just some property.
But there might be an O(n^2) approach that avoids resetting by iterating l from n down to 1 and then extending r from l to n, but we still need frequencies. Actually we could have a single array freq and for each l, we start from l and increment to n, then after finishing we need to reset frequencies because next l starts from l+1. But we could decrement frequencies from the left side? If we processed l from 1 to n, after finishing l, we could decrement freq[t[l]] from all subsequent intervals? That seems messy.
We could process l from n down to 1 and for each l, we start with frequencies from l to n, but we would need to gradually extend? Hmm.
Simpler: Use the current approach.
Now final answer.
But wait, I recall that there is a known Codeforces problem "Bear and Colors" (or similar) which expects O(n^2) solution. Yes, it's from Codeforces Round #318 Div.2? Actually problem "Bear and Colors" (or "Bear and Ball") I'm not sure. But the solution described in editorial is exactly O(n^2) with this incremental method.
Thus this is fine.
Now I'll craft the final response: Provide explanation and then code.
But the user asked: "Solve the problem." Possibly expects code.
We'll output solution in C++.
But let's also discuss potential pitfalls: The code uses 1-indexing; we must convert input.
Now, final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> t(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> t[i];
}
vector<int> ans(n + 1, 0);
vector<int> freq(n + 1, 0);
for (int l = 1; l <= n; ++l) {
fill(freq.begin(), freq.end(), 0);
int maxfreq = 0, dominant = 0;
for (int r = l; r <= n; ++r) {
int c = t[r];
int f = ++freq[c];
if (f > maxfreq) {
maxfreq = f;
dominant = c;
} else if (f == maxfreq && c < dominant) {
dominant = c;
}
++ans[dominant];
}
}
for (int i = 1; i <= n; ++i) {
cout << ans[i] << " ";
}
cout << "\n";
return 0;
}