「CodeForces 558E」A Simple Task

给定一个字符串,每次将一个子串排序,问最后的字符串是什么。

链接

题解

一个比较好的思维题。
用线段树维护区间内$26$个字母的出现次数。对于排序,将区间内每个字母出现的次数查出来,然后一个一个遍历就好了。如果是升序就从小往大覆盖,反之从大往小覆盖就好了。时间复杂度$O(26n\log n)$这里常数不能省
还有,最坑爹的一点是,线段树开$4\times 10^5$过不去,会RE #9,要开$8\times 10^5$……

代码

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include<bits/stdc++.h>
using namespace std;
typedef int ll;
const ll MAXN=2e5+51;
struct Occur{
ll num[28];
Occur()
{
memset(this->num,0,sizeof(this->num));
}
inline bool operator !=(const ll &rhs)const
{
for(register int i=1;i<=26;i++)
{
if(num[i]!=rhs)
{
return 1;
}
}
return 0;
}
inline void clear()
{
memset(num,0,sizeof(num));
}
};
struct SegmentTree{
ll l,r;
Occur sum,cover;
};
SegmentTree tree[MAXN<<2];
ll cnt,qcnt,l,r,x;
char str[MAXN];
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline Occur operator +(Occur x,Occur y)
{
Occur res;
for(register int i=1;i<=26;i++)
{
res.num[i]=x.num[i]+y.num[i];
}
return res;
}
inline Occur operator *(Occur x,ll y)
{
Occur res;
for(register int i=1;i<=26;i++)
{
res.num[i]=x.num[i]*y;
}
return res;
}
inline void update(ll node)
{
tree[node].sum=tree[node<<1].sum+tree[(node<<1)|1].sum;
}
inline void create(ll l,ll r,ll node)
{
tree[node].l=l,tree[node].r=r;
if(l==r)
{
tree[node].sum.num[str[l-1]-'a'+1]=1;
return;
}
ll mid=(l+r)>>1;
create(l,mid,node<<1);
create(mid+1,r,(node<<1)|1);
update(node);
}
inline void spread(ll node)
{
ll ls=node<<1,rs=ls|1;
if(tree[node].cover!=0)
{
tree[ls].sum=tree[node].cover*(tree[ls].r-tree[ls].l+1);
tree[rs].sum=tree[node].cover*(tree[rs].r-tree[rs].l+1);
tree[ls].cover=tree[node].cover;
tree[rs].cover=tree[node].cover;
tree[node].cover.clear();
}
}
inline void cover(ll l,ll r,Occur val,ll node)
{
if(l<=tree[node].l&&r>=tree[node].r)
{
tree[node].sum=val*(tree[node].r-tree[node].l+1);
tree[node].cover=val;
return;
}
ll mid=(tree[node].l+tree[node].r)>>1;
spread(node);
if(l<=mid)
{
cover(l,r,val,node<<1);
}
if(r>mid)
{
cover(l,r,val,(node<<1)|1);
}
update(node);
}
inline Occur query(ll l,ll r,ll node)
{
if(l<=tree[node].l&&r>=tree[node].r)
{
return tree[node].sum;
}
ll mid=(tree[node].l+tree[node].r)>>1;
Occur res;
spread(node);
if(l<=mid)
{
res=res+query(l,r,node<<1);
}
if(r>mid)
{
res=res+query(l,r,(node<<1)|1);
}
return res;
}
inline void sorts(ll l,ll r,ll cmp)
{
ll cur=l;
Occur val=query(l,r,1),curr;
if(!cmp)
{
for(register int i=1;i<=26;i++)
{
curr.num[i]=1,curr.num[i-1]=0;
if(val.num[i])
{
cover(l,l+val.num[i]-1,curr,1);
l=l+val.num[i];
}
}
}
else
{
for(register int i=26;i;i--)
{
curr.num[i]=1,curr.num[i+1]=0;
if(val.num[i])
{
cover(l,l+val.num[i]-1,curr,1);
l=l+val.num[i];
}
}
}
}
inline void spreadAll(ll node)
{
spread(node);
if(tree[node].l==tree[node].r)
{
return;
}
spreadAll(node<<1);
spreadAll((node<<1)|1);
update(node);
}
inline void preorder(ll node)
{
if(tree[node].l==tree[node].r)
{
for(register int i=1;i<=26;i++)
{
if(tree[node].sum.num[i])
{
putchar((char)(i+'a'-1));
break;
}
}
return;
}
preorder(node<<1);
preorder((node<<1)|1);
}
int main()
{
cnt=read(),qcnt=read();
scanf("%s",&str);
create(1,cnt,1);
for(register int i=0;i<qcnt;i++)
{
l=read(),r=read(),x=read();
sorts(l,r,!x);
}
spreadAll(1),preorder(1);
}