跳到正文

题解

论如何在 hyzx 活下去

keep_on约 3 分钟
原文

这是 2026年10月25日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年7月27日
收录
2026年8月31日
更新
2026年11月11日
篇幅
约 3 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+5;
ll n,q,mod,a[N];
struct Seg {ll l,r,sum,add,mul;}t[N<<2];
ll read() {/*...*/}
void push_up(int x) {t[x].sum=(t[x*2].sum+t[x*2+1].sum)%mod;}
void push_down(int x,int y) {
    int len=t[y].r-t[y].l+1;
    t[y].sum=(t[y].sum*t[x].mul+t[x].add*len)%mod;
    t[y].mul=t[y].mul*t[x].mul%mod;
    t[y].add=(t[y].add*t[x].mul+t[x].add)%mod;
}
void build(int x,int l,int r) {
    t[x]={l,r,0,0,1};
    if(l==r) {t[x].sum=a[l]; return;}
    int mid=(l+r)/2;
    build(x*2,l,mid); build(x*2+1,mid+1,r); push_up(x);
}
ll query(int x,int l,int r) {
    if(l<=t[x].l && t[x].r<=r) return t[x].sum;
    push_down(x,x*2); push_down(x,x*2+1); t[x].add=0; t[x].mul=1;
    int mid=(t[x].l+t[x].r)/2; ll res=0;
    if(l<=mid) res+=query(x*2,l,r);
    if(r>mid) res+=query(x*2+1,l,r);
    return res;
}
void update(int x,int l,int r,int k,int op) {
    if(l<=t[x].l && t[x].r<=r) {
        int len=t[x].r-t[x].l+1;
        if(op==1) {
            t[x].add=t[x].add*k%mod; t[x].sum=t[x].sum*k%mod; t[x].mul=t[x].mul*k%mod;
        } else {t[x].sum=(t[x].sum+k*len)%mod; t[x].add=(t[x].add+k)%mod;}
        return;
    }
    push_down(x,x*2); push_down(x,x*2+1); t[x].add=0; t[x].mul=1;
    int mid=(t[x].l+t[x].r)/2;
    if(l<=mid) update(x*2,l,r,k,op);
    if(r>mid) update(x*2+1,l,r,k,op);
    push_up(x);
}
int main() {/*...*/}