forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathG.cpp
More file actions
115 lines (103 loc) · 2.2 KB
/
G.cpp
File metadata and controls
115 lines (103 loc) · 2.2 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
106
107
108
109
110
111
112
113
114
115
#include <bits/stdc++.h>
using namespace std;
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
if(!*inp && !REACHEOF) { \
memset(BUF,0,sizeof BUF);\
int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
if (inpzzz != BUFSIZE) REACHEOF = true;\
inp=BUF; \
} \
INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
AM=0;\
GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
if (INP=='-') {AM=1;GETCHAR(INP);} \
j=INP-'0'; GETCHAR(INP); \
while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
if (AM) j=-j;\
}
int n, m, id[50500], V, cntV[200200];
long long a[100100], v[50500], realValues[200200], tree[200200];
map <long long,int> values;
multiset <long long> s;
void add(int x, long long v)
{
for (int i = x; i <= V; i += i & -i)
tree[i] += v;
}
long long get(int x)
{
long long res = 0;
for (int i = x; i; i -= i & -i)
res += tree[i];
return res;
}
int solve()
{
int res = 0;
long long sum = 0, last = 0;
while (1)
{
auto it = s.lower_bound(max(sum, last + 1));
if (it == s.end()) break;
int id = values[*it];
if (id > 1)
sum = get(id - 1);
if (realValues[id] >= sum)
{
last = realValues[id];
res++;
}
if (cntV[id] > 1 && !sum)
res *= 2;
sum += realValues[id] * cntV[id];
}
return res;
}
int main()
{
GN(n);
for (int i = 1; i <= n; i++)
{
GN(a[i]);
values[a[i]] = 0;
}
GN(m);
for (int i = 1; i <= m; i++)
{
GN(id[i]);
GN(v[i]);
values[v[i]] = 0;
}
V = 0;
for (auto u : values)
{
values[u.first] = ++V;
realValues[V] = u.first;
}
for (int i = 1; i <= n; i++)
{
s.insert(a[i]);
a[i] = values[a[i]];
cntV[a[i]]++;
add(a[i], realValues[a[i]]);
}
printf("%d\n", solve());
for (int i = 1; i <= m; i++)
{
int x = id[i];
cntV[a[x]]--;
s.erase(s.find(realValues[a[x]]));
add(a[x], -realValues[a[x]]);
a[x] = values[v[i]];
cntV[a[x]]++;
s.insert(v[i]);
add(a[x], v[i]);
printf("%d\n", solve());
}
}