版本比较
byd数据结构考试
正文
增加 155 行删除 4 行
56 行未改动5758596061616263646566676869707071727374175 行未改动全部展开250251252253254254255256257258336 行未改动全部展开595596597598599599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755252 行未改动全部展开56 行未改动分块维护的是啥?先写一版暴力 \kk*/```笔者太菜了\ll,考试结束也已一直没想出来正解笔者太菜了\ll,考试结束也一直没想出来正解## ans
虽然可以由大于等于的查询操作想到权值线段树,可是空间复杂度直接**boom**。
那咋办?线段树空间大就换空间小的呗,树状数组不仅无法处理且也空间大,那就只能用分快了。
操作一已经早就被分块蹂躏烂了,操作二呢?
其实这种查询还有一种先排序再二分的处理方式,只不过复杂度高,但是查询最多$3000$次,可以通过。其实这种查询还有先排序再二分的处理方式,只不过复杂度高,但是查询最多$3000$次,可以通过。
具体就是搞两个块状数组,一个是正常的,一个是每个块都是有序的,修改就整块正常的和排序了的都直接加,散块就给正常的先暴力加,再复制到排序的上后再排一次序。
查询操作为每个整块二分,散块暴力枚举。175 行未改动全部展开}```
# [T2](https://www.luogu.com.cn/problem/P1637)## me## me & ans先想出$n^2$暴力。```cpp#include<bits/stdc++.h>using namespace std;336 行未改动全部展开 return 0;}```## ans也就用线段树维护一个差分,查找就区间加,修改也是区间加。也就用线段树维护一个差分,查找就区间加,修改也是区间查,转换一下就行了。```cpp#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](https://www.luogu.com.cn/problem/P2787)## me没时间了只能拼暴力了,又由于少看一个条件,痛失35pts。```cpp252 行未改动全部展开