forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathC.cpp
More file actions
105 lines (92 loc) · 2.17 KB
/
C.cpp
File metadata and controls
105 lines (92 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
using namespace std;
const int SZ = 170;
int n, a[33333], value[333], active[333];
long long block[333];
int get(int x)
{
int bx = x / SZ;
return active[bx] ? value[bx] : a[x];
}
void assign(int b)
{
for (int i = 0; i < SZ; i++)
a[b * SZ + i] = value[b];
active[b] = 0;
}
int getAverage(long long sum, int len, int roundUp)
{
if (sum >= 0)
return (sum + roundUp * (len - 1)) / len;
return (sum - (1 - roundUp) * (len - 1)) / len;
}
int main()
{
ios::sync_with_stdio(0);
freopen("death.in", "r", stdin);
freopen("death.out", "w", stdout);
int m, x, y;
long long originalSum = 0;
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> a[i];
originalSum += a[i];
block[i / SZ] += a[i];
}
int numBlock = (n + SZ - 1) / SZ;
while (m--)
{
cin >> x >> y;
x--;
y--;
if (x > y) swap(x, y);
int bx = x / SZ, by = y / SZ, len = y - x + 1;
long long sum = 0, sumAll = 0;
for (int i = 0; i < numBlock; i++)
sumAll += block[i];
if (active[bx])
assign(bx);
if (active[by])
assign(by);
if (bx == by)
{
for (int i = x; i <= y; i++)
sum += get(i);
int average = getAverage(sum, len, sumAll <= originalSum);
for (int i = x; i <= y; i++)
{
block[bx] += average - a[i];
a[i] = average;
}
}
else
{
for (int i = x; i < (bx + 1) * SZ; i++)
sum += get(i);
for (int i = bx + 1; i < by; i++)
sum += block[i];
for (int i = by * SZ; i <= y; i++)
sum += get(i);
int average = getAverage(sum, len, sumAll <= originalSum);
for (int i = x; i < (bx + 1) * SZ; i++)
{
block[bx] += average - a[i];
a[i] = average;
}
for (int i = bx + 1; i < by; i++)
{
block[i] = 1LL * average * SZ;
value[i] = average;
active[i] = 1;
}
for (int i = by * SZ; i <= y; i++)
{
block[by] += average - a[i];
a[i] = average;
}
}
}
for (int i = 0; i < n; i++)
cout << get(i) << ' ';
}