Transformation(线段树区间加减,区间推平,区间乘法,区间平方和,立方和查询) · znzryb
## 思路讲解
呃,没什么好说的,主要就是一些细节问题
pushdown顺序放在这里了
```cpp
void pushdown(int o){
int l=o<<1,r=l|1; // 左儿子,右儿子
if(tag2[o]!=initv){
settag2(l, tag2[o]);
settag2(r, tag2[o]);
tag2[o]=initv;
}
if(tag3[o]!=1){
settag3(l, tag3[o]);
settag3(r, tag3[o]);
tag3[o]=1;
}
if(tag[o]!=0){
settag(l, tag[o]);
settag(r, tag[o]);
tag[o]=0;
}
}
```
## AC代码
[https://vjudge.net/solution/63174043](https://vjudge.net/solution/63174043)
::: 源代码
```cpp
// by Gospel_rock
#include <bits/stdc++.h>
#include <cstdio>
#include <vector>
#define all(vec) vec.begin(),vec.end()
#define PB push_back
#define EB emplace_back
#define EM emplace
#define FI first
#define SE second
#define pct __builtin_popcountll
#define ctz __builtin_ctzll
#define SZ(a) ((long long) a.size())
// #define FOR(i, a, b) for (long long i = (a); i <= (b); ++i)
// #define ROF(i, a, b) for (long long i = (a); i >= (b); --i)
#define debug(var) cerr << #var <<":"<<var<<"\n";
#define cend cerr<<"\n-----------\n"
#define fsp(x) fixed<<setprecision(x)
#define minn(vec) *min_element(vec.begin(),vec.end())
#define maxx(vec) *max_element(vec.begin(),vec.end())
using namespace std;
#ifdef LOCAL
template<typename T> void _print(T x) { cerr << x; }
// 可以打印多层数组,但是格式不是很好
template<typename T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) _print(i), cerr << " "; cerr << "]"; }
template<typename T> void _print(set<T> s) { cerr << "{ "; for (T i : s) _print(i), cerr << " "; cerr << "}"; }
template<typename T, typename U> void _print(pair<T, U> p) { cerr << "{"; _print(p.first); cerr << ", "; _print(p.second); cerr << "}"; }
template<typename T, typename U> void _print(map<T, U> m) { cerr << "{ "; for (auto &p : m) _print(p), cerr << " "; cerr << "}"; }
// 打印两层数组,格式好一些
template<typename T> void _print(vector<vector<T>> v){cerr<<"{\n";for (auto i : v) _print(i), cerr << "\n";cerr<<" }";}
#endif
using ll = long long;using ull = unsigned long long;
using DB=double;using LD=long double;
using i128 = __int128;
using pdd = pair<DB,DB>;using plb = pair<ll,bool>;
using pll = pair<ll,ll>;using vll=vector<ll>;using vb=vector<bool>;
using tll = tuple<ll,ll,ll>;using vii=vector<int>;
using vpll = vector<pll>;using vtll = vector<tll>;
using vdb = vector<DB>;using vc=vector<char>;
using arr3 = array<ll,3> ;using arr2 = array<ll,2>;
using CD=complex<double>;
static constexpr ll MAXN=(ll)1e6+10,INF=(ll)1e17+3; // 1e17+3是素数 1e18+9是素数
static constexpr ll mod=10007; // 998244353;
static constexpr double eps=1e-8;const double pi=acos(-1.0);
ll N,M,Q,X,K,T,lT;
inline void __();
// 来自wida github仓库 https://github.com/hh2048/XCPC/tree/main 加了一些新功能
//46 ms https://acm.hdu.edu.cn/contest/view-code?cid=1179&rid=14912 读入字符getc(a) 的AC记录
namespace QuickRead { // 快读
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int getc() {
return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
template <typename T> void Cin(T &a) {
T ans = 0;
bool f = 0;
char c = getc();
for (; c < '0' || c > '9'; c = getc()) {
if (c == '-') f = 1;
}
for (; c >= '0' && c <= '9'; c = getc()) {
ans = ans * 10 + c - '0';
}
a = f ? -ans : ans;
}
void getc(char &a){
char c=getc();
while (isspace(c)) {
c=getc();
}
a=c;
}
template <typename T, typename... Args> void Cin(T &a, Args &...args) {
Cin(a), Cin(args...);
}
template <typename T> void write(T x) { // 注意,这里输出不带换行
if (x < 0) putchar('-'), x = -x;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
} // namespace QuickRead
using namespace QuickRead;
bool isconti=true;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// cin>>lT;
while(isconti)
__();
return 0;
}
/*
*/
// 参考zkw的PPT 以及 https://www.luogu.com.cn/record/207546670
// 参考题解 https://www.luogu.com.cn/article/w3gfeebh
// AC https://vjudge.net/solution/63174043
// 线段树区间加减,区间修改,区间平方和,立方和查询
constexpr ll preset=0,initv=-INF;
// 依赖常量mod 线段树区间加减,区间修改,区间和查询
struct SEG{
int n,sz=1,dep;
static ll op(const ll &a,const ll &b){
return (a+b)%mod;
}
vector<ll> tr;vector<ll> tag;vector<ll> tag2;
vector<ll> sum;
vector<ll> tr3;
vector<ll> tag3;
SEG(int n){ // 传入的时候可以传大一点
this->n=n+2;
dep=__lg(this->n)+1;
sz=1<<dep;
tr.resize((sz<<1)+5,preset);
tag.resize((sz<<1)+5,preset);
tag2.resize((sz<<1)+5,initv);
tag3.resize((sz<<1)+5,1);
sum.resize((sz<<1)+5,preset);
tr3.resize((sz<<1)+5,preset);
}
int len(int o){return 1<<(dep-__lg(o));};
void pushup(int o){
tr[o]=op(tr[o<<1],tr[o<<1|1]);
sum[o]=op(sum[o<<1],sum[o<<1|1]);
tr3[o]=op(tr3[o<<1],tr3[o<<1|1]);
}
void settag(int o,ll x){ // add
tr3[o]=op(tr3[o],x*x*x%mod*len(o)+3*tr[o]*x+3*sum[o]*x*x);
tr[o]=op(tr[o],x*x*len(o)+sum[o]*x*2);
sum[o]=op(sum[o],x*len(o));
tag[o]=op(tag[o],x);
}
void settag2(int o,ll x){ // modify
tr[o]=op(len(o)*x*x,0);
sum[o]=op(x*len(o),0);
tr3[o]=op(len(o)*x*x*x,0);
tag[o]=0;
tag3[o]=1;
tag2[o]=x;
}
void settag3(int o,ll x){ // mul
tr[o]=op(tr[o]*x*x,0);
sum[o]=op(x*sum[o],0);
tr3[o]=op(tr3[o]*x*x%mod*x,0);
tag[o]=op(tag[o]*x,0);
tag3[o]=op(tag3[o]*x,0);
}
void pushdown(int o){
int l=o<<1,r=l|1; // 左儿子,右儿子
if(tag2[o]!=initv){
settag2(l, tag2[o]);
settag2(r, tag2[o]);
tag2[o]=initv;
}
if(tag3[o]!=1){
settag3(l, tag3[o]);
settag3(r, tag3[o]);
tag3[o]=1;
}
if(tag[o]!=0){
settag(l, tag[o]);
settag(r, tag[o]);
tag[o]=0;
}
}
void assign(int i,ll x){
tr[i+sz]=x*x%mod;
sum[i+sz]=x;
tr3[i+sz]=x*x%mod*x%mod;
}
void build(){
for(int o=sz-1;o>=1;--o){
pushup(o);
}
}
void push(int l,int r){
// 在j层,l,r变为了同一点
int i=dep,j=__lg(l^r);
while (i>j) { // 就是为了避免一下重复下推
pushdown(l>>i--);
}
while(i){
pushdown(l>>i),pushdown(r>>i--);
}
}
ll query(int l,int r,int x){
ll res=preset;
// 变为开区间,并加上sz
l=l-1+sz,r=r+1+sz;
push(l,r);
if(x==1){
for(;l^r^1;l>>=1,r>>=1){
// l^r^1 表示 只要 l 与 r 还不是同一个父亲的两兄弟,就继续上跳。
if(~l&1){
// l是偶数,说明l是父节点的左儿子,开区间,将其右儿子加入答案
res=op(res,sum[l^1]);
}
if(r&1){
res=op(sum[r^1],res);
}
}
}else if(x==2){
for(;l^r^1;l>>=1,r>>=1){
// l^r^1 表示 只要 l 与 r 还不是同一个父亲的两兄弟,就继续上跳。
if(~l&1){
// l是偶数,说明l是父节点的左儿子,开区间,将其右儿子加入答案
res=op(res,tr[l^1]);
}
if(r&1){
res=op(tr[r^1],res);
}
}
}else{
for(;l^r^1;l>>=1,r>>=1){
// l^r^1 表示 只要 l 与 r 还不是同一个父亲的两兄弟,就继续上跳。
if(~l&1){
// l是偶数,说明l是父节点的左儿子,开区间,将其右儿子加入答案
res=op(res,tr3[l^1]);
}
if(r&1){
res=op(tr3[r^1],res);
}
}
}
return res;
}
void add(int l,int r,ll x){
l=l-1+sz,r=r+1+sz;
push(l, r);
for(;l^r^1;pushup(l>>=1),pushup(r>>=1)){
if(~l&1){
settag(l^1, x);
}
if(r&1){
settag(r^1, x);
}
}
for (l >>= 1; l; l >>= 1) pushup(l);
}
void modify(int l,int r,ll x){
l=l-1+sz,r=r+1+sz;
push(l, r);
for(;l^r^1;pushup(l>>=1),pushup(r>>=1)){
if(~l&1){
settag2(l^1, x);
}
if(r&1){
settag2(r^1, x);
}
}
for (l >>= 1; l; l >>= 1) pushup(l);
}
void mul(int l,int r,ll x){
l=l-1+sz,r=r+1+sz;
push(l, r);
for(;l^r^1;pushup(l>>=1),pushup(r>>=1)){
if(~l&1){
settag3(l^1, x);
}
if(r&1){
settag3(r^1, x);
}
}
for (l >>= 1; l; l >>= 1) pushup(l);
}
};
inline void __(){
Cin(N,M);
if(N==0 && M==0) {isconti=false;return;}
SEG tr(N);
for(int i=1;i<=M;++i){
ll op,l,r,x;
Cin(op,l,r,x);
if(op==1){ // add
tr.add(l, r, x);
}else if(op==2){ // mul
tr.mul(l, r, x);
}else if(op==3){ // modify
tr.modify(l, r, x);
}else{
write(tr.query(l, r, x));
putchar('\n');
}
}
}
/*
AC
https://vjudge.net/solution/63174043
*/
```
:::
## 心路历程(WA,TLE,MLE……)