| Task: | Muutokset |
| Sender: | Yytsi |
| Submission time: | 2025-11-09 15:21:27 +0200 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | 100 |
| group | verdict | score |
|---|---|---|
| #1 | ACCEPTED | 20 |
| #2 | ACCEPTED | 30 |
| #3 | ACCEPTED | 50 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
| #2 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
| #3 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
| #4 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
| #5 | ACCEPTED | 0.01 s | 1, 2, 3 | details |
| #6 | ACCEPTED | 0.01 s | 2, 3 | details |
| #7 | ACCEPTED | 0.01 s | 2, 3 | details |
| #8 | ACCEPTED | 0.01 s | 2, 3 | details |
| #9 | ACCEPTED | 0.01 s | 2, 3 | details |
| #10 | ACCEPTED | 0.08 s | 3 | details |
| #11 | ACCEPTED | 0.08 s | 3 | details |
| #12 | ACCEPTED | 0.09 s | 3 | details |
| #13 | ACCEPTED | 0.06 s | 3 | details |
Code
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define N (1<<17)
ll dp_forward[N], dp_backward[N];
int tree[2*N];
int cntOf[N];
ll curAns;
int sumq(int a, int b) {
a += N;
b += N;
int res = 0;
while (a <= b) {
if (a % 2 == 1) res += tree[a++];
if (b % 2 == 0) res += tree[b--];
a /= 2; b /= 2;
}
return res;
}
void add(int pos, int val) {
pos += N;
tree[pos] += val;
for (pos /= 2; pos >= 1; pos /= 2) {
tree[pos] = tree[pos*2] + tree[pos*2+1];
}
}
ll sumStair(ll a, ll b) {
if (a > b) return 0LL;
return (b*(b+1LL)/2LL)-(a*(a-1LL)/2LL);
}
ll getGroupCost(ll gc) {
ll h = sumq(gc+1, N-1);
return gc * sumStair(h+1, h + sumq(gc, gc));
}
void addNumber(int x) {
int gc = cntOf[x];
if (!gc) {
curAns -= getGroupCost(1);
add(1, 1);
curAns += getGroupCost(1);
return;
}
// x moves from gc to gc+1
// (g) (x, y, z)
// 2 1
// (g,x) (y, z)
curAns -= getGroupCost(gc);
curAns -= getGroupCost(gc+1);
add(gc, -1);
add(gc+1, 1);
curAns += getGroupCost(gc);
curAns += getGroupCost(gc+1);
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
vector<int> reqs(m);
for (int i = 0; i < m; i++) cin>>reqs[i];
for (int i = 0; i < m; i++) {
int x = reqs[i];
addNumber(x);
cntOf[x]++;
dp_forward[i] = curAns;
}
// reset states
for (int i = 0; i < N; i++) {
cntOf[i] = 0;
}
for (int i = 2*N-1; i >= 1; i--) tree[i] = 0;
curAns = 0;
for (int i = m-1; i >= 0; i--) {
int x = reqs[i];
addNumber(x);
cntOf[x]++;
dp_backward[i] = curAns;
}
for (int i = 0; i < m-1; i++) {
cout << dp_forward[i] + dp_backward[i+1] << " ";
}
}Test details
Test 1
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 1 1 1 |
| correct output |
|---|
| (empty) |
| user output |
|---|
| (empty) |
Test 2
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1000 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| correct output |
|---|
| 1000 1000 1000 1000 1000 1000 ... |
| user output |
|---|
| 1000 1000 1000 1000 1000 1000 ... |
Test 3
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1000 1 2 2 2 1 1 1 1 1 1 1 1 1 2 1 ... |
| correct output |
|---|
| 1488 1488 1487 1486 1487 1488 ... |
| user output |
|---|
| 1488 1488 1487 1486 1487 1488 ... |
Test 4
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1000 7 8 2 4 8 3 3 10 9 7 7 6 8 7 2... |
| correct output |
|---|
| 5107 5107 5103 5099 5098 5102 ... |
| user output |
|---|
| 5107 5107 5103 5099 5098 5102 ... |
Test 5
Group: 1, 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100 1000 23 85 3 99 63 79 38 37 67 28 7... |
| correct output |
|---|
| 41676 41672 41621 41587 41589 ... |
| user output |
|---|
| 41676 41672 41621 41587 41589 ... |
Test 6
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100000 1000 2 1 2 1 1 2 2 2 1 2 2 2 2 2 2 ... |
| correct output |
|---|
| 1484 1485 1484 1485 1485 1485 ... |
| user output |
|---|
| 1484 1485 1484 1485 1485 1485 ... |
Test 7
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100000 1000 10 8 4 4 3 8 4 7 5 9 7 1 5 3 4... |
| correct output |
|---|
| 5279 5275 5277 5276 5272 5268 ... |
| user output |
|---|
| 5279 5275 5277 5276 5272 5268 ... |
Test 8
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100000 1000 82 63 63 58 57 46 58 40 23 47 ... |
| correct output |
|---|
| 41444 41381 41301 41284 41279 ... |
| user output |
|---|
| 41444 41381 41301 41284 41279 ... |
Test 9
Group: 2, 3
Verdict: ACCEPTED
| input |
|---|
| 100000 1000 50231 31135 38003 12048 55578 ... |
| correct output |
|---|
| 498503 497507 496513 495521 49... |
| user output |
|---|
| 498503 497507 496513 495521 49... |
Test 10
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 1 2 1 2 1 1 2 1 1 1 1 2 1 1 2 ... |
| correct output |
|---|
| 149988 149988 149988 149988 14... |
| user output |
|---|
| 149988 149988 149988 149988 14... |
Test 11
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 4 2 6 2 2 2 2 5 9 5 3 5 1 9 1 ... |
| correct output |
|---|
| 547169 547164 547157 547151 54... |
| user output |
|---|
| 547169 547164 547157 547151 54... |
Test 12
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 15 9 37 82 72 45 42 96 67 99 6... |
| correct output |
|---|
| 4959855 4959845 4959773 495975... |
| user output |
|---|
| 4959855 4959845 4959773 495975... |
Test 13
Group: 3
Verdict: ACCEPTED
| input |
|---|
| 100000 100000 30941 71402 45742 82863 71830 ... |
| correct output |
|---|
| 2378217706 2378191367 23781909... |
| user output |
|---|
| 2378217706 2378191367 23781909... |
