以下题意皆不做解释
T1
me
先由大于等于的查询操作想到权值线段树,结果空间不行,又想分块,不知道怎么维护,于是写了个暴力……A了。
#include<bits/stdc++.h>
using namespace std;
int a[1000010];
struct node
{
char z;
int l, r, k;
}q[3010];
int main(void)
{
cin.tie(0), cout.tie(0);
int n, q;
cin >> n >> q;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
while(q--)
{
char c;
int l, r, k;
cin >> c >> l >> r >> k;
if(c == 'M')
{
// change(1, 1, n, l, r, k);
for(int i = l;i <= r;i++)
{
a[i] += k;
}
}
else
{
int ans = 0;
for(int i = l;i <= r;i++)
{
ans += (a[i] >= k);
}
cout << ans << '\n';
// cout << (r - l + 1) - ask(1, 1, n, l, r, k) << '\n';
}
}
return 0;
}
/*
转化为[l,r]最大小于k的数
权值线段树?
哦对二分第k大,复杂度是O(nlog^2n)
有点难写,再想想分块
分块也能做?
分块维护的是啥?
先写一版暴力 \kk
*/笔者太菜了\ll,考试结束也一直没想出来正解
ans
虽然可以由大于等于的查询操作想到权值线段树,可是空间复杂度直接boom。
那咋办?线段树空间大就换空间小的呗,树状数组不仅无法处理且也空间大,那就只能用分快了。
操作一已经早就被分块蹂躏烂了,操作二呢?
其实这种查询还有先排序再二分的处理方式,只不过复杂度高,但是查询最多次,可以通过。
具体就是搞两个块状数组,一个是正常的,一个是每个块都是有序的,修改就整块正常的和排序了的都直接加,散块就给正常的先暴力加,再复制到排序的上后再排一次序。
查询操作为每个整块二分,散块暴力枚举。
复杂度为。
#include<bits/stdc++.h>
//#define int unsigned long long
using namespace std;
namespace Fast{
namespace Fread{
const int SIZE=1<<20;char buf[SIZE],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SIZE,stdin);if(S==TT)return '\n';}return *S++;}}
namespace Fwrite{
const int SIZE=1<<20;char buf[SIZE],*S=buf,*TT=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct Reader{
template <typename T>
Reader &operator>>(T &x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return *this;}
Reader &operator>>(double &x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(long double &x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(__float128 &x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(char &c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return *this;}
Reader &operator>>(char *str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return *this;}
Reader &operator>>(string &str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return *this;}
Reader(){}
}cin;
struct Writer{
const int Setprecision=6;
typedef int mxdouble;
template <typename T>
Writer &operator<<(T x){if(x==0){putchar('0');return *this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(char c){putchar(c);return *this;}
Writer &operator<<(char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer &operator<<(const char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer &operator<<(string str){int st=0,ed=str.size();while(st<ed) putchar(str[st++]);return *this;}
Writer() {}
} cout;
}
#ifdef ONLINE_JUDGE
#define cin FastIO::cin
#endif
#define cout FastIO::cout
}
using namespace Fast;
int a[1000010], b[1000010], ls[1000010], t, L[1000010], R[1000010], pos[1000010], add[1000010];
inline void change(int l, int r, int c)
{
int p = pos[l], q = pos[r];
if(p == q)
{
for(int i = l;i <= r;i++)
{
a[i] += c;
}
for(int i = L[p];i <= R[p];i++)
{
ls[i] = a[i];
}
sort(ls + L[p], ls + R[p] + 1);
for(int i = L[p];i <= R[p];i++)
{
b[i] = ls[i];
}
}
else
{
for(int i = p + 1;i <= q - 1;i++)
{
add[i] += c;
}
for(int i = l;i <= R[p];i++)
{
a[i] += c;
}
for(int i = L[p];i <= R[p];i++)
{
ls[i] = a[i];
}
sort(ls + L[p], ls + R[p] + 1);
for(int i = L[p];i <= R[p];i++)
{
b[i] = ls[i];
}
for(int i = L[q];i <= r;i++)
{
a[i] += c;
}
for(int i = L[q];i <= R[q];i++)
{
ls[i] = a[i];
}
sort(ls + L[q], ls + R[q] + 1);
for(int i = L[q];i <= R[q];i++)
{
b[i] = ls[i];
}
}
return;
}
inline int search(int l, int r, int c)
{
int p = pos[l], q = pos[r];
if(p == q)
{
int cnt = 0;
for(int i = l;i <= r;i++)
{
cnt += a[i] + add[p] >= c;
}
return cnt;
}
int ans = 0;
for(int i = p + 1;i <= q - 1;i++)
{
ans += (b + R[i] + 1) - lower_bound(b + L[i], b + R[i] + 1, c - add[i]);
}
for(int i = l;i <= R[p];i++)
{
ans += a[i] + add[p] >= c;
}
for(int i = L[q];i <= r;i++)
{
ans += a[i] + add[q] >= c;
}
return ans;
}
signed main(void)
{
int n, m;
cin >> n >> m;
for(int i = 1;i <= n;i++)
{
cin >> a[i], b[i] = a[i];
}
t = sqrt(n);
for(int i = 1;i <= t;i++)
{
L[i] = (i - 1) * t + 1, R[i] = i * t;
}
if(R[t] < n)
{
t++, L[t] = R[t - 1] + 1, R[t] = n;
}
for(int i = 1;i <= t;i++)
{
for(int j = L[i];j <= R[i];j++)
{
pos[j] = i;
}
}
for(int i = 1;i <= t;i++)
{
sort(b + L[i], b + R[i] + 1);
}
while(m--)
{
char c;
int l, r, k;
cin >> c >> l >> r >> k;
if(c == 'M')
{
change(l, r, k);
}
else
{
cout << search(l, r, k) << '\n';
}
}
return 0;
}T2
me & ans
先想出暴力。
#include<bits/stdc++.h>
using namespace std;
int n;
int a[30010];
int maxn, minn;
long long ans;
int main(void)
{
cin.tie(0);
cin >> n;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
for(int i = 2;i < n;i++)
{
minn = maxn = 0;
for(int j = i - 1;j >= 1;j--)
{
if(a[i] > a[j])
{
minn++;
}
}
for(int j = i + 1;j <= n;j++)
{
if(a[i] < a[j])
{
maxn++;
}
}
ans += minn * maxn;
}
cout << ans;
return 0;
} 这代码已经能过了
一看,这代码可以用权值线段树优化呀,于是就有了正解。
#include<bits/stdc++.h>
#define lc p << 1
#define rc p << 1 | 1
#define pl c[p].l
#define pr c[p].r
using namespace std;
int n;
int a[30010];
unsigned long long ans;
struct tre
{
int l, r, sum;
}c[5000010];
inline void build(int p, int l, int r)
{
c[p] = (tre){l, r, 0};
if(l == r)
{
return;
}
int mid = l + r >> 1;
build(lc, l, mid), build(rc, mid + 1, r);
return;
}
inline void change(int p, int x, int k)
{
c[p].sum += k;
if(pl == pr)
{
return;
}
int mid = pl + pr >> 1;
if(mid >= x)
{
change(lc, x, k);
}
else
{
change(rc, x, k);
}
return;
}
inline int ask(int p, int L, int R)
{
if(L > R)
{
return 0;
}
if(L <= pl && pr <= R)
{
return c[p].sum;
}
// if(pl == pr)
// {
// puts("边界有问题");
// return 0;
// }
int mid = pl + pr >> 1, ans = 0;
if(mid >= L)
{
ans += ask(lc, L, R);
}
if(mid < R)
{
ans += ask(rc, L, R);
}
return ans;
}
int x[30010], y[30010];
int main(void)
{
cin.tie(0);
cin >> n;
build(1, 1, 100000);
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
change(1, a[1], 1);
for(int i = 2;i < n;i++)
{
x[i] = ask(1, 1, a[i] - 1);
change(1, a[i], 1);
}
build(1, 1, 100000);
change(1, a[n], 1);
for(int i = n - 1;i > 1;i--)
{
ans += x[i] * ask(1, a[i] + 1, 100000);
change(1, a[i], 1);
}
cout << ans;
return 0;
}
/*
想出n^2暴力
用set可以优化创过去
但我还是用线段树优化
其实是我不会用set
*/T3
me
没写完,只能写一个暴力了。
#include<bits/stdc++.h>
using namespace std;
int a[2000010], cnt[2000010];
int main(void)
{
cin.tie(0), cout.tie(0);
int n, c, m;
cin >> n >> c >> m;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
while(m--)
{
int l, r;
cin >> l >> r;
int minn = 1e9, maxn = 0;
for(int i = l;i <= r;i++)
{
cnt[a[i]]++, minn = min(minn, a[i]), maxn = max(maxn, a[i]);
}
int ans = 0;
for(int i = minn;i <= maxn;i++)
{
if(cnt[i] > 1)
{
ans++;
}
cnt[i] = 0;
}
cout << ans << '\n';
}
return 0;
}ans
本题过于套路了虽然我考场上没做出来,与HH的项链类似,不做过多阐述
#include<bits/stdc++.h>
namespace Fast{
namespace Fread{
const int SIZE=1<<20;char buf[SIZE],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SIZE,stdin);if(S==TT)return '\n';}return *S++;}}
namespace Fwrite{
const int SIZE=1<<20;char buf[SIZE],*S=buf,*TT=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct Reader{
template <typename T>
Reader &operator>>(T &x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return *this;}
Reader &operator>>(char &c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return *this;}
Reader &operator>>(char *str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return *this;}
Reader(){}
}cin;
struct Writer{
const int Setprecision=6;
typedef int mxdouble;
template <typename T>
Writer &operator<<(T x){if(x==0){putchar('0');return *this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(char c){putchar(c);return *this;}
Writer &operator<<(char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer &operator<<(const char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer() {}
} cout;
}
#ifdef ONLINE_JUDGE
#define cin FastIO::cin
#endif
#define cout FastIO::cout
}
using namespace Fast;
#define pl(p) c[p].l
#define pr(p) c[p].r
using namespace std;
int rt[2000005], last[2000005], prelast[2000005], cnt;
struct tre
{
int l, r, sum;
}c[80000000];
inline void change(int &p, int q, int l, int r, int x, int k)
{
p = ++cnt, c[p] = c[q], c[p].sum += k;
if(l == r)
{
return;
}
int mid = l + r >> 1;
if(x <= mid)
{
change(pl(p), pl(q), l, mid, x, k);
}
else
{
change(pr(p), pr(q), mid + 1, r, x, k);
}
return;
}
inline int ask(int p, int l, int r, int x, int y)
{
if(x <= l && r <= y)
{
return c[p].sum;
}
int ans = 0, mid = l + r >> 1;
if(x <= mid)
{
ans += ask(pl(p), l, mid, x, y);
}
if(mid < y)
{
ans += ask(pr(p), mid + 1, r, x, y);
}
return ans;
}
int main(void)
{
int n, c, m;
cin >> n >> c >> m;
int tmp;
for(int i = 1;i <= n;i++)
{
int x;
cin >> x;
if(prelast[x] != 0)
{
change(tmp, rt[i - 1], 1, n, prelast[x], -1);
change(rt[i], tmp, 1, n, last[x], 1);
}
else if(last[x] != 0)
{
change(rt[i], rt[i - 1], 1, n, last[x], 1);
}
else
{
rt[i] = rt[i - 1];
}
prelast[x] = last[x];
last[x] = i;
}
for(int i = 1;i <= m;i++)
{
int l, r;
cin >> l >> r;
cout << ask(rt[r], 1, n, l, r) << '\n';
}
return 0;
}但是需要卡常。
T4
me
饿啊~没想到差分这个小trick,直接不会。
#include<bits/stdc++.h>
using namespace std;
int n, m;
long long a[100010];
int maxn, minn;
int main(void)
{
cin.tie(0), cout.tie(0);
cin >> n >> m;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
int l, r, k, d;
while(m--)
{
int opt;
cin >> opt;
if(opt == 1)
{
cin >> l >> r >> k >> d;
for(int i = l;i <= r;i++)
{
a[i] += k, k += d;
}
}
else
{
int p;
cin >> p;
cout << a[p] << '\n';
}
}
return 0;
}ans
也就用线段树维护一个差分,查找就区间加,修改也是区间查,转换一下就行了。
#include<bits/stdc++.h>
#define int long long
#define lc (p << 1)
#define rc (p << 1 | 1)
using namespace std;
namespace Fast{
namespace Fread{
const int SIZE=1<<20;char buf[SIZE],*S,*TT;inline char getchar(){if(S==TT){TT=(S=buf)+fread(buf,1,SIZE,stdin);if(S==TT)return '\n';}return *S++;}}
namespace Fwrite{
const int SIZE=1<<20;char buf[SIZE],*S=buf,*TT=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==TT)flush();}struct NTR{~NTR(){flush();}} ztr;}
using namespace Fread;
using namespace Fwrite;
#define getchar Fread::getchar
#define putchar Fwrite::putchar
namespace FastIO{
struct Reader{
template <typename T>
Reader &operator>>(T &x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return *this;}
Reader &operator>>(double &x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(long double &x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(__float128 &x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return *this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
Reader &operator>>(char &c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return *this;}
Reader &operator>>(char *str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return *this;}
Reader &operator>>(string &str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return *this;}
Reader(){}
}cin;
struct Writer{
const int Setprecision=6;
typedef int mxdouble;
template <typename T>
Writer &operator<<(T x){if(x==0){putchar('0');return *this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return *this;}
Writer &operator<<(char c){putchar(c);return *this;}
Writer &operator<<(char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer &operator<<(const char *str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
Writer &operator<<(string str){int st=0,ed=str.size();while(st<ed) putchar(str[st++]);return *this;}
Writer() {}
} cout;
}
#ifdef ONLINE_JUDGE
#define cin FastIO::cin
#endif
#define cout FastIO::cout
}
using namespace Fast;
int a[1000010];
int n, m;
struct node
{
int l, r, sum, tag;
}c[4000010];
inline void build(int p, int l, int r)
{
c[p].l = l, c[p].r = r;
if(l == r)
{
c[p].sum = a[l];
return;
}
int mid = l + r >> 1;
build(lc, l, mid), build(rc, mid + 1, r), c[p].sum = c[lc].sum + c[rc].sum;
return;
}
inline void change(int p, int l, int r, int k)
{
if(c[p].l >= l && c[p].r <= r)
{
c[p].sum += (c[p].r - c[p].l + 1) * k, c[p].tag += k;
return;
}
int mid = c[p].l + c[p].r >> 1;
if(c[p].tag)
{
c[lc].sum += (c[lc].r - c[lc].l + 1) * c[p].tag, c[lc].tag += c[p].tag, c[rc].sum += (c[rc].r - c[rc].l + 1) * c[p].tag, c[rc].tag += c[p].tag, c[p].tag = 0;
}
if(mid >= l)
{
change(lc, l, r, k);
}
if(mid < r)
{
change(rc, l, r, k);
}
c[p].sum = c[lc].sum + c[rc].sum;
return;
}
inline int ask(int p, int l, int r)
{
if(c[p].l >= l && c[p].r <= r)
{
return c[p].sum;
}
int mid = c[p].l + c[p].r >> 1, ans = 0;
if(c[p].tag)
{
c[lc].sum += (c[lc].r - c[lc].l + 1) * c[p].tag, c[lc].tag += c[p].tag, c[rc].sum += (c[rc].r - c[rc].l + 1) * c[p].tag, c[rc].tag += c[p].tag, c[p].tag = 0;
}
if(mid >= l)
{
ans += ask(lc, l, r);
}
if(mid < r)
{
ans += ask(rc, l, r);
}
return ans;
}
signed main(void)
{
cin >> n >> m;
for(int i = 1;i <= n;i++)
{
cin >> a[i];
}
for(int i = n;i >= 2;i--)
{
a[i] -= a[i - 1];
}
build(1, 1, n);
while(m--)
{
int opt;
cin >> opt;
if(opt == 1)
{
int l, r, K, D;
cin >> l >> r >> K >> D;
change(1, l, l, K);
if(l + 1 <= r)
{
change(1, l + 1, r, D);
}
if(r + 1 <= n)
{
change(1, r + 1, r + 1, -(K + (r - l) * D));
}
}
else
{
int x;
cin >> x;
cout << ask(1, 1, x) << '\n';
}
}
return 0;
}T5
me
没时间了只能拼暴力了,又由于少看一个条件,痛失35pts。
#include<bits/stdc++.h>
using namespace std;
int a[50010], cnt[10000], b[50010];
int main(void)
{
cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
string s;
cin >> s;
s = ' ' + s;
for(int i = 1;i <= n;i++)
{
a[i] = s[i];
}
while(m--)
{
int opt;
cin >> opt;
if(opt == 1)
{
int ans = 0;
int l, r;
char c;
cin >> l >> r >> c;
int k = c;
for(int i = l;i <= r;i++)
{
if(a[i] == k)
{
ans++;
}
}
cout << ans << '\n';
}
else if(opt == 2)
{
int l, r;
char c;
cin >> l >> r >> c;
int k = c;
for(int i = l;i <= r;i++)
{
a[i] = k;
}
}
else
{
int l, r;
cin >> l >> r;
sort(a + l, a + r + 1);
}
}
return 0;
}ans
本题有多种做法,这里讲述分块做法。
操作一、二过于简单不说,操作三快排超时,考虑桶排。
#include<bits/stdc++.h>
using namespace std;
inline int cf(char x)
{
return ('A' <= x && x <= 'Z') ? (x - 'A') : (x - 'a');
}
int a[50010], L[50010], R[50010], pos[50010], sum[250][28], t, lazy[250];
inline void push_down(int x)
{
if(~lazy[x])
{
int l = L[x], r = R[x];
int len = r - l + 1;
for(int i = l;i <= r;i++)
{
a[i] = lazy[x];
}
for(int i = 0;i < 26;i++)
{
sum[x][i] = 0;
}
sum[x][lazy[x]] = len, lazy[x] = -1;
}
return;
}
inline int ask(int x, int y, int k)
{
int l = pos[x], r = pos[y];
if(l == r)
{
push_down(l);
int ans = 0;
for(int i = x;i <= y;i++)
{
ans += a[i] == k;
}
return ans;
}
int ans = 0;
for(int i = l + 1;i < r;i++)
{
ans += lazy[i] != -1 ? (lazy[i] == k ? R[i] - L[i] + 1 : 0) : sum[i][k];
}
push_down(l), push_down(r);
for(int i = x;i <= R[l];i++)
{
ans += a[i] == k;
}
for(int i = L[r];i <= y;i++)
{
ans += a[i] == k;
}
return ans;
}
inline void change1(int x, int y, int k)
{
int l = pos[x], r = pos[y];
if(l == r)
{
push_down(l);
for(int i = x;i <= y;i++)
{
sum[l][a[i]]--, sum[l][k]++, a[i] = k;
}
}
else
{
for(int i = l + 1;i < r;i++)
{
lazy[i] = k, memset(sum[i], 0, sizeof(sum[i])), sum[i][k] = R[i] - L[i] + 1;
}
push_down(l), push_down(r);
for(int i = x;i <= R[l];i++)
{
sum[l][a[i]]--, sum[l][k]++, a[i] = k;
}
for(int i = L[r];i <= y;i++)
{
sum[r][a[i]]--, sum[r][k]++, a[i] = k;
}
}
return;
}
int cnt[26];
inline void get_cnt(int x, int y)
{
memset(cnt,0,sizeof(cnt));
int l = pos[x], r = pos[y];
if(l == r)
{
push_down(l);
for(int i = x;i <= y;i++)
{
cnt[a[i]]++;
}
return;
}
for(int i = l + 1;i < r;i++)
{
push_down(i);
}
push_down(l), push_down(r);
for(int i = x;i <= R[l];i++)
{
cnt[a[i]]++;
}
for(int i = L[r];i <= y;i++)
{
cnt[a[i]]++;
}
for(int i = l + 1;i < r;i++)
{
for(int j = 0;j < 26;j++)
{
cnt[j] += sum[i][j];
}
}
return;
}
inline void change2(int x, int y)
{
get_cnt(x, y);
int now = x;
for(int i = 0;i < 26;i++)
{
if(!cnt[i])
{
continue;
}
change1(now, now + cnt[i] - 1, i), now += cnt[i];
}
return;
}
int main(void)
{
int n, m;
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
char c;
scanf(" %c",&c);
a[i] = cf(c);
}
memset(lazy, -1, sizeof(lazy));
t = sqrt(n);
for(int i = 1;i <= t;i++)
{
L[i] = (i - 1) * t + 1, R[i] = i * t;
}
if(R[t] < n)
{
t++, L[t] = R[t - 1] + 1, R[t] = n;
}
for(int i = 1;i <= t;i++)
{
for(int j = L[i];j <= R[i];j++)
{
pos[j] = i, sum[i][a[j]]++;
}
}
while(m--)
{
int opt;
scanf("%d", &opt);
if(opt == 1)
{
int l, r;
char c;
scanf("%d%d %c", &l, &r, &c);
int k = cf(c);
printf("%d\n", ask(l, r, k));
}
else if(opt == 2)
{
int l, r;
char c;
scanf("%d%d %c", &l, &r, &c);
int k = cf(c);
change1(l, r, k);
}
else
{
int l, r;
scanf("%d%d", &l, &r);
change2(l, r);
}
}
return 0;
}注意:本题细节多,且容易写错导致代码退化
