数据结构
单调队列
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 3000005
int k,n,l,r,lmax,rmax,lmin,rmin,ans;
int a[N],qmax[N],qmin[N];
struct hp{int Max,Min;};
void push(int id)
{
while (lmax<rmax&&a[qmax[rmax]]<=a[id]) --rmax;
qmax[++rmax]=id;
while (lmin<rmin&&a[qmin[rmin]]>=a[id]) --rmin;
qmin[++rmin]=id;
}
hp pop(int id)
{
while (lmax<rmax&&qmax[lmax+1]<id) ++lmax;
int Max=a[qmax[lmax+1]];
while (lmin<rmin&&qmin[lmin+1]<id) ++lmin;
int Min=a[qmin[lmin+1]];
return (hp){Max,Min};
}
int main()
{
scanf("%d%d",&k,&n);
for (int i=1;i<=n;++i) scanf("%d",&a[i]);
l=r=1;lmax=rmax=lmin=rmin=0;push(l);
while (l<=n)
{
if (l>r)
{
r=l;lmax=rmax=lmin=rmin=0;push(l);
}
while (r<n)
{
hp now=pop(l);
if (a[r+1]>now.Max)
{if (a[r+1]-now.Min>k) break;}
else if (a[r+1]<now.Min)
{if (now.Max-a[r+1]>k) break;}
++r;push(r);
}
ans=max(ans,r-l+1);
++l;
}
printf("%d\n",ans);
}
- 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
- 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
单调栈
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 50005
int n,h[N],val[N],l[N],r[N],Max,stack[N],top,sum[N],ans;
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%d%d",&h[i],&val[i]),l[i]=1,r[i]=n;
top=0;
for (int i=1;i<=n;++i)
{
while (top&&h[stack[top]]<h[i])
{
r[stack[top]]=i-1;
--top;
}
stack[++top]=i;
}
top=0;
for (int i=n;i>=1;--i)
{
while (top&&h[stack[top]]<h[i])
{
l[stack[top]]=i+1;
--top;
}
stack[++top]=i;
}
for (int i=1;i<=n;++i) sum[l[i]-1]+=val[i],sum[r[i]+1]+=val[i];
for (int i=1;i<=n;++i) ans=max(ans,sum[i]);
printf("%d\n",ans);
}
- 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
- 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
St表
// poj3264
using namespace std;
int n,m,l,r,Max,Min;
int a[N],lg2[N],stmax[N][sz],stmin[N][sz];
int main()
{
scanf("%d%d",&n,&m);
for (int i=1,p=0;i<=n;++i)
{
while ((1<<p)<=i) ++p;
lg2[i]=p-1;
}
for (int i=1;i<=n;++i)
scanf("%d",&a[i]),stmax[i][0]=stmin[i][0]=a[i];
for (int j=1;j<sz;++j)
for (int i=1;i<=n;++i)
if (i+(1<<j)-1<=n)
{
stmax[i][j]=max(stmax[i][j-1],stmax[i+(1<<(j-1))][j-1]);
stmin[i][j]=min(stmin[i][j-1],stmin[i+(1<<(j-1))][j-1]);
}
for (int i=1;i<=m;++i)
{
scanf("%d%d",&l,&r);
if (l>r) swap(l,r);
int k=lg2[r-l+1];
Max=max(stmax[l][k],stmax[r-(1<<k)+1][k]);
Min=min(stmin[l][k],stmin[r-(1<<k)+1][k]);
printf("%d\n",Max-Min);
}
}
- 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
- 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
Lca
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100005
#define sz 17
int n,m,x,y;
int tot,point[N],nxt[N*2],v[N*2];
int f[N][sz+5],h[N],size[N];
struct hp{int pre,pt;};
void addedge(int x,int y)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x;
}
void build(int x,int fa,int dep)
{
h[x]=dep; size[x]=1;
for (int i=1;i<sz;++i)
{
if ((h[x]-(1<<i))<1) break;
f[x][i]=f[f[x][i-1]][i-1];
}
for (int i=point[x];i;i=nxt[i])
if (v[i]!=fa)
{
f[v[i]][0]=x;
build(v[i],x,dep+1);
size[x]+=size[v[i]];
}
}
int lca(int x,int y)
{
if (h[x]<h[y]) swap(x,y);
int k=h[x]-h[y];
for (int i=0;i<sz;++i)
if ((1<<i)&k) x=f[x][i];
if (x==y) return x;
for (int i=sz-1;i>=0;--i)
if (f[x][i]!=f[y][i]) x=f[x][i],y=f[y][i];
return f[x][0];
}
hp find(int x,int high)
{
int xx=x;
for (int i=sz-1;i>=0;--i)
while (h[f[x][i]]>high) xx=x,x=f[x][i];
xx=x,x=f[x][0];
hp ans;ans.pre=xx,ans.pt=x;
return ans;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<n;++i)
{
scanf("%d%d",&x,&y);
addedge(x,y);
}
build(1,0,1);
scanf("%d",&m);
for (int i=1;i<=m;++i)
{
scanf("%d%d",&x,&y);
if (h[x]<h[y]) swap(x,y);
if (x==y)
{
printf("%d\n",n);
continue;
}
int r=lca(x,y);
int len=h[x]-h[r]+h[y]-h[r];
if (len%2)
{
puts("0");
continue;
}
len/=2;
int high=h[x]-len;
if (high==h[r])
{
hp date1=find(x,high);
hp date2=find(y,high);
printf("%d\n",n-size[r]+size[r]-size[date1.pre]-size[date2.pre]);
}
else
{
hp date=find(x,high);
printf("%d\n",size[date.pt]-size[date.pre]);
}
}
}
- 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
- 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
Splay
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 300005
int n,opt,x,root,sz;
int f[N],ch[N][2],key[N],size[N],cnt[N];
void clear(int x)
{
f[x]=ch[x][0]=ch[x][1]=key[x]=size[x]=cnt[x]=0;
}
int get(int x)
{
return ch[f[x]][1]==x;
}
void update(int x)
{
size[x]=cnt[x]+size[ch[x][0]]+size[ch[x][1]];
}
void rotate(int x)
{
int old=f[x],oldf=f[old],wh=get(x);
ch[old][wh]=ch[x][wh^1];
if (ch[old][wh]) f[ch[old][wh]]=old;
ch[x][wh^1]=old;
f[old]=x;
if (oldf) ch[oldf][ch[oldf][1]==old]=x;
f[x]=oldf;
update(old);
update(x);
}
void splay(int x)
{
for (int fa;fa=f[x];rotate(x))
if (f[fa])
rotate( (get(x)==get(fa))?fa:x );
root=x;
}
void insert(int x)
{
if (!root)
{
root=++sz;
cnt[sz]=size[sz]=1;key[sz]=x;
return;
}
int now=root,fa=0;
while (1)
{
if (x==key[now])
{
cnt[now]++;
update(now);
splay(now);
break;
}
fa=now;
now=ch[now][x>key[now]];
if (!now)
{
++sz;
f[sz]=fa;ch[fa][x>key[fa]]=sz;
size[sz]=cnt[sz]=1;
key[sz]=x;
update(fa);
splay(sz);
break;
}
}
}
int find(int x)
{
int now=root,ans=0;
while (1)
{
if (x<key[now]) now=ch[now][0];
else
{
ans+=size[ch[now][0]];
if (x==key[now])
{
splay(now);
return ans+1;
}
ans+=cnt[now];
now=ch[now][1];
}
}
}
int findx(int x)
{
int now=root;
while (1)
{
if (x<=size[ch[now][0]]) now=ch[now][0];
else
{
x-=size[ch[now][0]];
if (x<=cnt[now])
{
splay(now);
return key[now];
}
x-=cnt[now];
now=ch[now][1];
}
}
}
int pre()
{
int now=ch[root][0];
while (ch[now][1]) now=ch[now][1];
return now;
}
int nxt()
{
int now=ch[root][1];
while (ch[now][0]) now=ch[now][0];
return now;
}
void del(int x)
{
int wh=find(x);
if (cnt[root]>1)
{
--cnt[root];
update(root);
return;
}
if (!ch[root][0]&&!ch[root][1])
{
clear(root);
root=0;
return;
}
if (!ch[root][0])
{
int oldroot=root;
root=ch[oldroot][1];
f[root]=0;
clear(oldroot);
return;
}
if (!ch[root][1])
{
int oldroot=root;
root=ch[oldroot][0];
f[root]=0;
clear(oldroot);
return;
}
int oldroot=root;
int leftbig=pre();splay(leftbig);
ch[root][1]=ch[oldroot][1];
f[ch[root][1]]=root;
clear(oldroot);
update(root);
return;
}
int main()
{
scanf("%d",&n);
for (int i=1;i<=n;++i)
{
scanf("%d%d",&opt,&x);
switch(opt)
{
case 1:
{
insert(x);
break;
}
case 2:
{
del(x);
break;
}
case 3:
{
int ans=find(x);
printf("%d\n",ans);
break;
}
case 4:
{
int ans=findx(x);
printf("%d\n",ans);
break;
}
case 5:
{
insert(x);
printf("%d\n",key[pre()]);
del(x);
break;
}
case 6:
{
insert(x);
printf("%d\n",key[nxt()]);
del(x);
break;
}
}
}
return 0;
}
- 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
- 209
- 210
- 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
- 209
- 210
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100005
#define inf 1000000000
int n,m,l,r,root;
int a[N],f[N],ch[N][2],size[N],key[N],delta[N];
int get(int x)
{
return ch[f[x]][1]==x;
}
void update(int x)
{
size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
}
void pushdown(int x)
{
if (x&&delta[x])
{
delta[ch[x][0]]^=1;
delta[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
delta[x]=0;
}
}
int build(int l,int r,int fa)
{
if (l>r) return 0;
int mid=(l+r)>>1;
f[mid]=fa;key[mid]=a[mid];size[mid]=1;
int lch=build(l,mid-1,mid);
int rch=build(mid+1,r,mid);
ch[mid][0]=lch,ch[mid][1]=rch;
update(mid);
return mid;
}
void rotate(int x)
{
pushdown(f[x]);
pushdown(x);
int old=f[x],oldf=f[old],wh=get(x);
ch[old][wh]=ch[x][wh^1];
if (ch[old][wh]) f[ch[old][wh]]=old;
ch[x][wh^1]=old;
f[old]=x;
if (oldf) ch[oldf][ch[oldf][1]==old]=x;
f[x]=oldf;
update(old);
update(x);
}
void splay(int x,int tar)
{
for (int fa;(fa=f[x])!=tar;rotate(x))
if (f[fa]!=tar)
rotate( (get(x)==get(fa))?fa:x );
if (!tar) root=x;
}
int find(int x)
{
int now=root;
while (1)
{
pushdown(now);
if (x<=size[ch[now][0]]) now=ch[now][0];
else
{
x-=size[ch[now][0]];
if (x==1) return now;
x-=1;
now=ch[now][1];
}
}
}
void write(int x)
{
pushdown(x);
if (ch[x][0]) write(ch[x][0]);
if (key[x]!=-inf&&key[x]!=inf) printf("%d ",key[x]);
if (ch[x][1]) write(ch[x][1]);
}
int main()
{
scanf("%d%d",&n,&m);
a[1]=-inf;a[n+2]=inf;
for (int i=1;i<=n;++i) a[i+1]=i;
root=build(1,n+2,0);
for (int i=1;i<=m;++i)
{
scanf("%d%d",&l,&r);
if (l>=r) continue;
int aa=find(l);
int bb=find(r+2);
splay(aa,0);
splay(bb,aa);
delta[ch[ch[root][1]][0]]^=1;
}
write(root);
return 0;
}
- 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
- 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
树链剖分+dfs序
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int max_n=1e5+5;
const int max_e=max_n*2;
const int max_tree=max_n*5;
int n,q,x,N,ans;
char s[20];
int tot,point[max_n],next[max_e],v[max_e];
int size[max_n],h[max_n],father[max_n],son[max_n];
int top[max_n],in[max_n],out[max_n];
int sum[max_tree],delta[max_tree];
inline void addedge(int x,int y){
++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y;
++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x;
}
inline void dfs_1(int x,int fa,int dep){
size[x]=1; h[x]=dep; father[x]=fa;
int maxson=0;
for (int i=point[x];i;i=next[i])
if (v[i]!=fa){
dfs_1(v[i],x,dep+1);
size[x]+=size[v[i]];
if (maxson<size[v[i]]){
maxson=size[v[i]];
son[x]=v[i];
}
}
}
inline void dfs_2(int x,int fa){
if (son[fa]!=x) top[x]=x;
else top[x]=top[fa];
in[x]=++N;
if (son[x]) dfs_2(son[x],x);
for (int i=point[x];i;i=next[i])
if (v[i]!=fa&&v[i]!=son[x])
dfs_2(v[i],x);
out[x]=N;
}
inline void update(int now){
sum[now]=sum[now<<1]+sum[now<<1|1];
}
inline void pushdown(int now,int l,int r,int mid){
if (delta[now]>=0){
sum[now<<1]=delta[now]*(mid-l+1);
delta[now<<1]=delta[now];
sum[now<<1|1]=delta[now]*(r-mid);
delta[now<<1|1]=delta[now];
delta[now]=-1;
}
}
inline void interval_change(int now,int l,int r,int lrange,int rrange,int v){
int mid=(l+r)>>1;
if (lrange<=l&&r<=rrange){
sum[now]=(r-l+1)*v;
delta[now]=v;
return;
}
pushdown(now,l,r,mid);
if (lrange<=mid)
interval_change(now<<1,l,mid,lrange,rrange,v);
if (mid+1<=rrange)
interval_change(now<<1|1,mid+1,r,lrange,rrange,v);
update(now);
}
inline int query(int now,int l,int r,int lrange,int rrange){
int mid=(l+r)>>1,ans=0;
if (lrange<=l&&r<=rrange) return sum[now];
pushdown(now,l,r,mid);
if (lrange<=mid)
ans+=query(now<<1,l,mid,lrange,rrange);
if (mid+1<=rrange)
ans+=query(now<<1|1,mid+1,r,lrange,rrange);
return ans;
}
inline void QUERY(int u,int t){
int f1=top[u],f2=top[t],ans=0;
while (f1!=f2){
if (h[f1]<h[f2]){
swap(f1,f2);
swap(u,t);
}
ans+=query(1,1,N,in[f1],in[u]);
u=father[f1];
f1=top[u];
}
if (in[u]>in[t]) swap(u,t);
ans+=query(1,1,N,in[u],in[t]);
int cnt=h[x];
printf("%d\n",cnt-ans);
}
inline void CHANGE(int u,int t){
int f1=top[u],f2=top[t],ans=0;
while (f1!=f2){
if (h[f1]<h[f2]){
swap(f1,f2);
swap(u,t);
}
interval_change(1,1,N,in[f1],in[u],1);
u=father[f1];
f1=top[u];
}
if (in[u]>in[t]) swap(u,t);
interval_change(1,1,N,in[u],in[t],1);
}
inline void INSTALL(int u,int t){
QUERY(u,t);
CHANGE(u,t);
}
inline void UNINSTALL(int x){
ans=query(1,1,N,in[x],out[x]);
printf("%d\n",ans);
interval_change(1,1,N,in[x],out[x],0);
}
int main(){
scanf("%d",&n);
for (int i=1;i<n;++i){
scanf("%d",&x);
++x;
addedge(x,i+1);
}
dfs_1(1,0,1);
dfs_2(1,0);
memset(sum,0,sizeof(sum));
memset(delta,128,sizeof(delta));
scanf("%d",&q);
for (int i=1;i<=q;++i){
scanf("%s",s);
scanf("%d",&x);
++x;
if (s[0]=='i')
INSTALL(1,x);
else
UNINSTALL(x);
}
}
- 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
- 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
手写栈
inline void dfs_1()
{
int temp=0;
strack[++temp]=1; size[1]=1; h[1]=1;
for (int i=1;i<=n;++i) cur[i]=point[i];
while (temp)
{
int x=strack[temp];
if (v[cur[x]]==father[x]) cur[x]=nxt[cur[x]];
if (!cur[x])
{
temp--;
if (father[x])
{
size[father[x]]+=size[x];
if (size[x]>size[son[father[x]]]) son[father[x]]=x;
}
continue;
}
int vt=v[cur[x]];
strack[++temp]=vt;
size[vt]=1; h[vt]=h[x]+1; father[vt]=x;
cur[x]=nxt[cur[x]];
}
}
inline void dfs_2()
{
int temp=0;
strack[++temp]=1; num[1]=++N; top[1]=1;
for (int i=1;i<=n;++i) cur[i]=point[i];
while (temp)
{
int x=strack[temp];
if (!use[x])
{
use[x]=1;
int vt=son[x];
if (vt)
{
strack[++temp]=vt;
top[vt]=top[x];
num[vt]=++N;
}
continue;
}
while (cur[x]&&(v[cur[x]]==father[x]||v[cur[x]]==son[x])) cur[x]=nxt[cur[x]];
if (!cur[x])
{
temp--;
continue;
}
else
{
int vt=v[cur[x]];
strack[++temp]=vt;
top[vt]=vt;
num[vt]=++N;
cur[x]=nxt[cur[x]];
}
}
}
- 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
- 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
Link-Cut-Tree
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define N 10005
char opt[10];
int n,m;
int f[N],ch[N][2],rev[N],stack[N];
bool isroot(int x)
{
return ch[f[x]][0]!=x&&ch[f[x]][1]!=x;
}
int get(int x)
{
return ch[f[x]][1]==x;
}
void pushdown(int x)
{
if (x&&rev[x])
{
if (ch[x][0]) rev[ch[x][0]]^=1;
if (ch[x][1]) rev[ch[x][1]]^=1;
swap(ch[x][0],ch[x][1]);
rev[x]=0;
}
}
void rotate(int x)
{
int old=f[x],oldf=f[old],wh=get(x);
if (!isroot(old)) ch[oldf][ch[oldf][1]==old]=x;
f[x]=oldf;
ch[old][wh]=ch[x][wh^1];
if (ch[old][wh]) f[ch[old][wh]]=old;
ch[x][wh^1]=old;
f[old]=x;
}
void splay(int x)
{
int top=0;stack[++top]=x;
for (int i=x;!isroot(i);i=f[i]) stack[++top]=f[i];
for (int i=top;i>=1;--i) pushdown(stack[i]);
for (int fa;!isroot(x);rotate(x))
if (!isroot(fa=f[x]))
rotate((get(x)==get(fa))?fa:x);
}
void access(int x)
{
int t=0;
for (;x;t=x,x=f[x])
{
splay(x);
ch[x][1]=t;
}
}
void reverse(int x)
{
access(x);
splay(x);
rev[x]^=1;
}
void link(int x,int y)
{
reverse(x);
f[x]=y;
}
void cut(int x,int y)
{
reverse(x);
access(y);
splay(y);
f[x]=ch[y][0]=0;
}
int find(int x)
{
access(x);
splay(x);
while (ch[x][0]) x=ch[x][0];
return x;
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=m;++i)
{
scanf("%s",opt);
int x,y;scanf("%d%d",&x,&y);
if (opt[0]=='C')
link(x,y);
else if (opt[0]=='D')
cut(x,y);
else
{
if (find(x)==find(y)) puts("Yes");
else puts("No");
}
}
}
- 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
- 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
静态主席树
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int max_n=1e5+5;
int n,m,x,y,z,sz,ans;
int val[max_n],p[max_n],num[max_n],root[max_n];
int sum[max_n*20],ls[max_n*20],rs[max_n*20];
inline int cmp(int a,int b){return val[a]<val[b];}
inline void build(int &now,int l,int r,int x){
int mid=(l+r)>>1;
sum[++sz]=sum[now]; ls[sz]=ls[now]; rs[sz]=rs[now];
now=sz;
++sum[now];
if (l==r) return;
if (x<=mid) build(ls[now],l,mid,x);
else build(rs[now],mid+1,r,x);
}
inline int query(int i,int j,int l,int r,int k){
int mid=(l+r)>>1;
if (l==r) return l;
int t=sum[ls[j]]-sum[ls[i]];
if (t>=k) return query(ls[i],ls[j],l,mid,k);
else return query(rs[i],rs[j],mid+1,r,k-t);
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",&val[i]),p[i]=i;
sort(p+1,p+n+1,cmp);
for (int i=1;i<=n;++i)
num[p[i]]=i;
sz=0; root[0]=0;
for (int i=1;i<=n;++i){
root[i]=root[i-1];
build(root[i],1,n,num[i]);
}
for (int i=1;i<=m;++i){
scanf("%d%d%d",&x,&y,&z);
ans=query(root[x-1],root[y],1,n,z);
printf("%d\n",val[p[ans]]);
}
}
- 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
- 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
动态主席树
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int max_n=1e4+5;
const int max_m=1e4+5;
const int max_log=30;
const int max_tree=3e6+5;
char opt[10];
int n,m,a,b,t,sz,N,cnt;
int val[max_n],A[max_m],B[max_m],K[max_m],flag[max_m];
int num[max_n*2],hash[max_n*2],root[max_n*2],L[max_log],R[max_log];
int sum[max_tree],ls[max_tree],rs[max_tree];
inline int find(int x){
int l=1,r=N,mid;
while (l<=r){
mid=(l+r)>>1;
if (hash[mid]<x) l=mid+1;
else r=mid-1;
}
return l;
}
inline void build(int last,int l,int r,int &now,int x,int v){
int mid=(l+r)>>1;
now=++sz;
sum[now]=sum[last]+v; ls[now]=ls[last]; rs[now]=rs[last];
if (l==r) return;
if (x<=mid) build(ls[last],l,mid,ls[now],x,v);
else build(rs[last],mid+1,r,rs[now],x,v);
}
inline int query(int l,int r,int k){
int mid=(l+r)>>1;
if (l==r) return l;
int suml=0,sumr=0;
for (int i=1;i<=a;++i) suml+=sum[ls[L[i]]];
for (int i=1;i<=b;++i) sumr+=sum[ls[R[i]]];
if (sumr-suml>=k){
for (int i=1;i<=a;++i) L[i]=ls[L[i]];
for (int i=1;i<=b;++i) R[i]=ls[R[i]];
return query(l,mid,k);
}
else{
for (int i=1;i<=a;++i) L[i]=rs[L[i]];
for (int i=1;i<=b;++i) R[i]=rs[R[i]];
return query(mid+1,r,k-(sumr-suml));
}
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i) scanf("%d",&val[i]),num[++cnt]=val[i];
for (int i=1;i<=m;++i){
scanf("%s",opt);
if (opt[0]=='C'){
scanf("%d%d",&A[i],&B[i]);
num[++cnt]=B[i];
}
else{
flag[i]=1;
scanf("%d%d%d",&A[i],&B[i],&K[i]);
}
}
sort(num+1,num+cnt+1);
hash[++N]=num[1];
for (int i=2;i<=cnt;++i)
if (num[i]!=num[i-1]) hash[++N]=num[i];
for (int i=1;i<=n;++i){
t=find(val[i]);
for (int j=i;j<=n;j+=j&(-j))
build(root[j],1,N,root[j],t,1);
}
for (int i=1;i<=m;++i)
if (flag[i]){
a=0,b=0,A[i]--;
for (int j=A[i];j>=1;j-=j&(-j)) L[++a]=root[j];
for (int j=B[i];j>=1;j-=j&(-j)) R[++b]=root[j];
printf("%d\n",hash[query(1,N,K[i])]);
}
else{
t=find(val[A[i]]);
for (int j=A[i];j<=n;j+=j&(-j)) build(root[j],1,N,root[j],t,-1);
val[A[i]]=B[i];
t=find(val[A[i]]);
for (int j=A[i];j<=n;j+=j&(-j)) build(root[j],1,N,root[j],t,1);
}
}
- 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
- 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
算法
高精度
加
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 505
char s1[N],s2[N];
int a[N],b[N];
void jia(int *a,int *b)
{
a[0]=max(a[0],b[0]);
for (int i=1;i<=a[0];++i) a[i]+=b[i];
for (int i=1;i<=a[0];++i)
{
a[i+1]+=a[i]/10;
a[i]%=10;
}
if (a[a[0]+1]) a[0]++;
}
int main()
{
scanf("%s",s1);scanf("%s",s2);
a[0]=strlen(s1);b[0]=strlen(s2);
for (int i=0;i<a[0];++i) a[i+1]=s1[a[0]-i-1]-'0';
for (int i=0;i<b[0];++i) b[i+1]=s2[b[0]-i-1]-'0';
jia(a,b);
for (int i=a[0];i>=1;--i) printf("%d",a[i]);
putchar('\n');
}
- 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
- 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
减
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 505
char s1[N],s2[N];
int a[N],b[N];
bool flag;
bool compare(int *a,int *b)
{
if (a[0]<b[0]) return 1;
else if (a[0]>b[0]) return 0;
for (int i=a[0];i>=1;--i)
{
if (a[i]<b[i]) return 1;
else if (b[i]<a[i]) return 0;
}
return 0;
}
void jian(int *a,int *b)
{
for (int i=1;i<=a[0];++i)
{
if (a[i]<b[i]) --a[i+1],a[i]+=10;
a[i]-=b[i];
}
while (a[0]>1&&!a[a[0]]) --a[0];
}
int main()
{
scanf("%s",s1);scanf("%s",s2);
a[0]=strlen(s1);b[0]=strlen(s2);
for (int i=0;i<a[0];++i) a[i+1]=s1[a[0]-i-1]-'0';
for (int i=0;i<b[0];++i) b[i+1]=s2[b[0]-i-1]-'0';
flag=compare(a,b);
if (flag)
for (int i=0;i<=max(a[0],b[0]);++i) swap(a[i],b[i]);
jian(a,b);
if (flag) putchar('-');
for (int i=a[0];i>=1;--i) printf("%d",a[i]);
putchar('\n');
}
- 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
- 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
乘
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 1005
char s1[N],s2[N];
int a[N],b[N],ans[N];
void cheng(int *a,int *b)
{
ans[0]=a[0]+b[0]-1;
for (int i=1;i<=a[0];++i)
for (int j=1;j<=b[0];++j)
ans[i+j-1]+=a[i]*b[j];
for (int i=1;i<=ans[0];++i)
{
ans[i+1]+=ans[i]/10;
ans[i]%=10;
}
while (ans[ans[0]+1])
{
ans[0]++;
ans[ans[0]+1]=ans[ans[0]]/10;
ans[ans[0]]%=10;
}
}
int main()
{
scanf("%s",s1);scanf("%s",s2);
a[0]=strlen(s1);b[0]=strlen(s2);
for (int i=0;i<a[0];++i) a[i+1]=s1[a[0]-i-1]-'0';
for (int i=0;i<b[0];++i) b[i+1]=s2[b[0]-i-1]-'0';
cheng(a,b);
for (int i=ans[0];i>=1;--i) printf("%d",ans[i]);
putchar('\n');
}
- 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
- 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
除
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 1005
char s1[N],s2[N];
int a[N],b[N],c[N],tmp[N];
void copy(int *a,int *b,int loc)
{
for (int i=1;i<=a[0];++i) b[i+loc-1]=a[i];
b[0]=a[0]+loc-1;
}
int compare(int *a,int *b)
{
if (a[0]>b[0]) return 1;
if (a[0]<b[0]) return -1;
for (int i=a[0];i>=1;--i)
{
if (a[i]>b[i]) return 1;
else if (a[i]<b[i]) return -1;
}
return 0;
}
void jian(int *a,int *b)
{
int flag=compare(a,b);
if (flag==0)
{
a[0]=1,a[1]=0;
return;
}
if (flag==1)
{
for (int i=1;i<=a[0];++i)
{
if (a[i]<b[i]) --a[i+1],a[i]+=10;
a[i]-=b[i];
}
while (a[0]>1&&!a[a[0]]) --a[0];
}
}
void chu(int *a,int *b,int *c)
{
c[0]=a[0]-b[0]+1;
for (int i=c[0];i>=1;--i)
{
memset(tmp,0,sizeof(tmp));
copy(b,tmp,i);
while (compare(a,tmp)>=0) c[i]++,jian(a,tmp);
}
while (c[0]>1&&!c[c[0]]) --c[0];
}
int main()
{
scanf("%s",s1);scanf("%s",s2);
a[0]=strlen(s1);b[0]=strlen(s2);
for (int i=0;i<a[0];++i) a[i+1]=s1[a[0]-i-1]-'0';
for (int i=0;i<b[0];++i) b[i+1]=s2[b[0]-i-1]-'0';
chu(a,b,c);
for (int i=c[0];i>=1;--i) putchar(c[i]+'0');putchar('\n');
}
- 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
- 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
归并排序求逆序对
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define LL long long
#define N 100005
int n,k;LL rev;
int a[N],s[N],b[N];
void mergesort(int l,int r)
{
int mid=(l+r)>>1;
if (l==r) return;
mergesort(l,mid);
mergesort(mid+1,r);
int i=l,j=mid+1,now=l;
while (i<=mid&&j<=r)
{
if (s[i]<s[j]) b[now++]=s[i++];
else b[now++]=s[j++],rev+=(LL)mid-i+1;
}
while (i<=mid) b[now++]=s[i++];
while (j<=r) b[now++]=s[j++];
for (;l<=r;++l) s[l]=b[l];
}
int main()
{
scanf("%d%d",&n,&k);
for (int i=1;i<=n;++i) scanf("%d",&a[i]);
for (int i=1;i<=n;++i)
{
s[i]=s[i-1]+a[i]-k;
if (s[i]<=0) ++rev;
}
mergesort(1,n);
printf("%lld\n",(LL)(n+1)*n/2-rev);
}
- 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
- 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
网络流
Dinic
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
#define N 10005
#define inf 1000000000
int n,m,maxflow,x,y,z;
int tot,point[N],nxt[N],v[N],remain[N];
int cur[N],deep[N],last[N],num[N];
queue <int> q;
void addedge(int x,int y,int cap)
{
++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap;
++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0;
}
bool bfs(int s,int t)
{
for (int i=1;i<=t;++i) deep[i]=t;
deep[s]=0;
for (int i=1;i<=t;++i) cur[i]=point[i];
q.push(s);
while (!q.empty())
{
int now=q.front();q.pop();
for (int i=point[now];i!=-1;i=nxt[i])
if (deep[v[i]]==t&&remain[i])
{
deep[v[i]]=deep[now]+1;
q.push(v[i]);
}
}
return deep[t]<t;
}
int dfs(int now,int t,int limit)
{
if (now==t||!limit) return limit;
int flow=0,f;
for (int i=cur[now];i!=-1;i=nxt[i])
{
cur[now]=i;
if (deep[v[i]]==deep[now]+1&&(f=dfs(v[i],t,min(remain[i],limit))))
{
flow+=f;
limit-=f;
remain[i]-=f;
remain[i^1]+=f;
if (!limit) break;
}
}
return flow;
}
void dinic(int s,int t)
{
while (bfs(s,t))
maxflow+=dfs(s,t,inf);
}
int main()
{
tot=-1;
memset(point,-1,sizeof(point));
memset(nxt,-1,sizeof(nxt));
scanf("%d%d",&m,&n);
for (int i=1;i<=m;++i)