思路:看这个题的输入输出格式很容易能够想到线段树,一开始想了一个用三个线段树的方法,写了500多行,但是wa了,不太好调,看题解发现想复杂了,其实挺简单,我们考虑用线段树维护这个东西,那么对于一段区间l,r来说,sum显然等于tr[u<<1].sum+tr[u<<1|1].sum,但是这并不全,因为中间连接的部分也可能会形成递增子序列,那么连接的部分就是要知道左区间右端递增的长度tr[u<<1].lenr,以及右区间左边递增的长度tr[u<<1|1].lenl,如果这两个能够按照递增的顺序拼起来,那么就会再产生tr[u<<1].lenr*tr[u<<1|1].lenl个连续递增序列,我们发现这个用线段树是很好维护的,而修改的话是一个单点修改
- // Problem: E. Non-Decreasing Dilemma
- // Contest: Codeforces - Codeforces Round 742 (Div. 2)
- // URL: https://codeforces.com/contest/1567/problem/E
- // Memory Limit: 256 MB
- // Time Limit: 2000 ms
-
- #include
- #include
- #include
- #define fi first
- #define se second
- #define i128 __int128
- using namespace std;
- typedef long long ll;
- typedef double db;
- typedef pair<int,int> PII;
- const double eps=1e-7;
- const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
- const long long int llINF=0x3f3f3f3f3f3f3f3f;
- inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
- while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
- inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
- inline void write(ll x,char ch) {write(x);putchar(ch);}
- void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
- bool cmp0(int a,int b) {return a>b;}
- template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
- template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
- void hack() {printf("\n----------------------------------\n");}
-
- int T,hackT;
- int n,m,k;
- int w[N];
- struct Node{
- int l,r;
- ll sum;
- int lenl,lenr;
- int tl,tr;
- };
- Node tr[N*4];
-
- void pushup(Node &u,Node &le,Node &ri) {
- u.sum=le.sum+ri.sum;
- u.tl=le.tl;
- u.tr=ri.tr;
-
- if(ri.tl>=le.tr) {
- u.sum+=(ll)ri.lenl*le.lenr;
- }
-
- if(ri.lenr==ri.r-ri.l+1) {
- if(ri.tl>=le.tr) {
- u.lenr=ri.lenr+le.lenr;
- }else u.lenr=ri.lenr;
- }else u.lenr=ri.lenr;
-
- if(le.lenl==le.r-le.l+1) {
- if(le.tr<=ri.tl) {
- u.lenl=le.lenl+ri.lenl;
- }else u.lenl=le.lenl;
- }else u.lenl=le.lenl;
- }
-
- void pushup(int u) {
- pushup(tr[u],tr[u<<1],tr[u<<1|1]);
- }
-
- void build(int u,int l,int r) {
- if(l==r) tr[u]={l,r,1,1,1,w[l],w[r]};
- else {
- tr[u]={l,r};
- int mid=l+r>>1;
- build(u<<1,l,mid),build(u<<1|1,mid+1,r);
-
- pushup(u);
- }
- }
-
- void modify(int u,int x,int c) {
- if(tr[u].l==x&&tr[u].r==x) {
- tr[u]={x,x,1,1,1,c,c};
- }else {
- int mid=tr[u].l+tr[u].r>>1;
- if(x<=mid) modify(u<<1,x,c);
- else modify(u<<1|1,x,c);
-
- pushup(u);
- }
- }
-
- void init(Node &vis) {
- vis={0,0,0,0,0,0,0};
- }
-
- Node query(int u,int l,int r) {
- if(tr[u].l>=l&&tr[u].r<=r) return tr[u];
- else {
- int mid=tr[u].l+tr[u].r>>1;
- Node a,b,c;
- init(a),init(b),init(c);
-
- int tb=0,tc=0;
- if(l<=mid) b=query(u<<1,l,r),tb++;
- if(r>mid) c=query(u<<1|1,l,r),tc++;
-
- if(tb&&tc) {
- pushup(a,b,c);
- return a;
- }else if(tb) return b;
- else return c;
- }
- }
-
- void solve() {
- n=read();
- int q=read();
-
- for(int i=1;i<=n;i++) w[i]=read();
-
- build(1,1,n);
-
- while(q--) {
- int op=read();
-
- if(op==1) {
- int x=read(),c=read();
- modify(1,x,c);
- }else if(op==2) {
- int l=read(),r=read();
- printf("%lld\n",query(1,l,r).sum);
- }
- }
- }
-
- int main() {
- // init();
- // stin();
- // ios::sync_with_stdio(false);
-
- // scanf("%d",&T);
- T=1;
- while(T--) hackT++,solve();
-
- return 0;
- }