forked from t3nsor/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ae5b2.cpp
93 lines (93 loc) · 1.66 KB
/
ae5b2.cpp
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
// 2009-12-30
//Dynamic max prefix sum query with segment tree. O((n+m) lg n)
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
static int sum[1000000];
static int max_psum[1000000];
void recalc(int root)
{
sum[root]=sum[root<<1]+sum[1+(root<<1)];
max_psum[root]=max(max_psum[root<<1],
sum[root<<1]+max_psum[1+(root<<1)]);
}
void atomic_upd(int root,int val)
{
sum[root]=val;
max_psum[root]=val>0?val:0;
}
template<class RAI>
void upd(int root,RAI begin,RAI end,RAI pos,int val)
{
if (end-begin==1)
atomic_upd(root,val);
else
{
RAI mid=begin+(end-begin)/2;
if (pos<mid)
upd(root<<1,begin,mid,pos,val);
else
upd(1+(root<<1),mid,end,pos,val);
recalc(root);
}
}
template<class RAI>
void build_tree(int root,RAI begin,RAI end)
{
if (end-begin==1)
atomic_upd(root,*begin);
else
{
RAI mid=begin+(end-begin)/2;
build_tree(root<<1,begin,mid);
build_tree(1+(root<<1),mid,end);
recalc(root);
}
}
int in()
{
int res=0;
char c;
do
c=getchar_unlocked();
while (c<=32);
do
{
res=(res<<1)+(res<<3)+c-'0';
c=getchar_unlocked();
}
while (c>32);
return res;
}
int main()
{
int N,i,m,idx,val;
static int a[200001],b[200001],c[200001],d[200001];
N=in();
for (i=1; i<=N; i++)
b[i]=a[i]=in();
sort(b+1,b+N+1);
c[0]=0;
for (i=1; i<=N; i++)
c[b[i]]=i;
for (i=1; i<=N; i++)
if (!c[i])
c[i]=c[i-1];
for (i=1; i<=N; i++)
d[i]=c[i]-c[i-1]-1;
build_tree(1,d,d+N+1);
printf(max_psum[1]>0?"NIE\n":"TAK\n");
m=in();
while (m--)
{
idx=in(); val=in();
upd(1,d,d+N+1,d+val,d[val]+1);
upd(1,d,d+N+1,d+a[idx],d[a[idx]]-1);
d[val]++;
d[a[idx]]--;
a[idx]=val;
printf(max_psum[1]>0?"NIE\n":"TAK\n");
}
return 0;
}