二阶差分

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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;
const int mod=1e9+7;
ll n,dif[(int)1e5+9],ans;
char s[(int)1e5+9];
int main()
{
scanf("%lld%s",&n,s+1);
for(int i=1;i<n;++i)if(s[i]=='1')dif[i+1]+=1;
for(int i=1;i<=n;++i){
dif[i]+=dif[i-1];
if(dif[i]>mod)dif[i]-=mod;
}
for(int i=1;i<=n;++i){
dif[i]+=dif[i-1];
if(dif[i]>mod)dif[i]-=mod;
}
for(int i=1;i<=n;++i){
if(s[i]=='1'){
ans+=dif[i];
if(ans>mod)ans-=mod;
}
}
printf("%lld\n", ans);
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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll n,m,k,a[(int)5e4+9],ans[(int)5e4+9],l=1,r,sum,s[(int)5e4+9],sz,id[(int)5e4+9],l1[(int)5e4+9],r1[(int)5e4+9];
struct line{
ll l,r,num;
}q[(int)5e4+9];
bool cmp(line x,line y){
return id[x.r]==id[y.r]?x.l<y.l:x.r<y.r;
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
sz=n/sqrt(m*2/3);
for(int i=1;i<=n;++i)cin>>a[i],id[i]=(i-1)/sz+1;
for(int i=1;i<=m;++i)cin>>q[i].l>>q[i].r,q[i].num=i,l1[i]=q[i].l,r1[i]=q[i].r;
sort(q+1,q+1+m,cmp);
for(int i=1;i<=m;++i){
while(l<q[i].l)sum-=s[a[l]]*(s[a[l]]-1)/2,s[a[l]]-=1,sum+=s[a[l]]*(s[a[l]]-1)/2,l+=1;
while(l>q[i].l)l-=1,sum-=s[a[l]]*(s[a[l]]-1)/2,s[a[l]]+=1,sum+=s[a[l]]*(s[a[l]]-1)/2;
while(r<q[i].r)r+=1,sum-=s[a[r]]*(s[a[r]]-1)/2,s[a[r]]+=1,sum+=s[a[r]]*(s[a[r]]-1)/2;
while(r>q[i].r)sum-=s[a[r]]*(s[a[r]]-1)/2,s[a[r]]-=1,sum+=s[a[r]]*(s[a[r]]-1)/2,r-=1;
ans[q[i].num]=sum;
}
for(int i=1,x;i<=m;++i){
x=std::gcd(ans[i],(r1[i]-l1[i]+1)*(r1[i]-l1[i])/2);
if(l1[i]==r1[i])cout<<"0/1\n";
else cout<<ans[i]/x<<"/"<<(r1[i]-l1[i]+1)*(r1[i]-l1[i])/2/x<<"\n";
}
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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

ll tt,n,m;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
f(sb,1,tt){
cin>>n>>m;
ll lc=min(lowbit(n),lowbit(m)),cnt=1;
for(;(lc&1)==0;lc>>=1,cnt+=1);
cout<<(cnt+1)/2<<" "<<(n>m?"0\n":"1\n");
}
return 0;
}

wa了快一个月,原来是维护区间最值的树状数组的更新函数写挂了,:)

这错误写法能过洛谷完全因为它是不带修改的…

正确的:

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
struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x)
{
while(x<=n){
h[x]=aa[x];
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return 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
struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x,int k)
{
while(x<=n){
h[x]=k;//错在这里!!!!
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return 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
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
#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define sqr(x) (x) * (x)
#define pr1(x) printf("Case %d: ",x)
#define pn1(x) printf("Case %d:\n",x)
#define pr2(x) printf("Case #%d: ",x)
#define pn2(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define sz(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
using ll=long long;

LL tt,k,n,aa[(int)1e5+9],rk[(int)1e5+9];

struct BIT
{
LL h[(int)1e5 + 9], n;
void init(int x)
{
n = x;
f(i,1,n)update(i);
}
void update(int x)
{
while(x<=n){
h[x]=aa[x];
int low=lowbit(x);
for(int i=1;i<low;i<<=1)h[x]=min(h[x],h[x-i]);
x+=lowbit(x);
}
}
LL gmin(int l, int r)
{
LL ans = 1e18;
while (r >= l)
{
ans = min(ans, aa[r]);
r--;
while (r - lowbit(r) >= l)
{
ans = min(h[r], ans);
r -= lowbit(r);
}
}
return ans;
}
};


void solve(){
cin>>n>>k;
f(i,1,n)cin>>aa[i],rk[i]=i;
ll ans=0;
BIT b;
if(k==0){
b.init(n);
for(int i=1;i<n;++i){
ans=max(ans,min(min(aa[i],aa[i+1]),b.gmin(1,n)*2ll));
}
}
else {
sort(rk+1,rk+1+n,[](long long x,long long y){
return aa[x]<aa[y];
});
for(int i=1;i<k;++i)aa[rk[i]]=1e9;
b.init(n);
aa[n+1]=-10086;
int ma=0,ma2=0;
for(int i=1;i<n;++i){
int tp=min(aa[i],aa[i+1]);
if(tp>=ma){
ma2=ma,ma=tp;
}else if(tp>ma2)ma2=tp;
}
for(int i=1;i<=n;++i){
LL tmp=aa[i];
aa[i]=1e9;
b.update(i);
int usma=max(min(aa[i],aa[i+1]),min(aa[i-1],aa[i]))==ma&&aa[i]==ma?ma2:ma;
usma=max({aa[i-1],aa[i+1],usma+0ll});
ans=max(ans,min(usma+0ll,b.gmin(1,n)*2));
//cerr<<i<<"::"<<usma<<"__"<<b.gmin(1,n)<<"+++"<<ans<<"\n";
aa[i]=tmp;
b.update(i);
}
}
cout<<ans<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
f(sb,1,tt){
solve();
}
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

#include <bits/stdc++.h>
/*
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
*/
using namespace std;

const double eps = 1e-10;
const double pi = 3.1415926535897932384626433832795;
const double eln = 2.718281828459045235360287471352;

#define f(i, a, b) for (int i = a; i <= b; i++)
#define scan(x) scanf("%d", &x)
#define mp make_pair
#define pb push_back
#define lowbit(x) (x&(-x))

#define fi first
#define se second
#define SZ(x) int((x).size())
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define summ(a) (accumulate(all(a), 0ll))

typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;

using ll=long long;

int tt,n,dp[(int)1e5+9],a[(int)1e5+9],x[(int)1e5+9];

void solve(){
cin>>n;
for(int i=1;i<=n;++i){
cin>>a[i];
x[i]=a[i]^x[i-1];
}
map<int,int> ma;
ma[0]=0;
for(int i=1;i<=n;++i){
if(a[i])dp[i]=dp[i-1]+1;
else dp[i]=dp[i-1];
if(ma.count(x[i])){
dp[i]=min(dp[i],ma[x[i]]+i-1);
}
ma[x[i]]=min(ma[x[i]],dp[i]-i);
}
cout<<dp[n]<<"\n";
}

int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>tt;
f(sb,1,tt)solve();
return 0;
}
0%