数据结构刷题笔记

Part 1 线段树

注:线段树都使用了动态开点/kel

P2023 [AHOI2009] 维护序列

区间加,区间乘,区间求和。

  1 #include <iostream>
  2 #include <cstdio>
  3 using namespace std;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 #define space putchar(' ')
  8 #define enter putchar('\n')
  9 #define debug(x) cerr << #x << " = " << x << endl
 10 
 11 namespace Fast
 12 {
 13     template<typename T> inline void read(T &s)
 14     {
 15         s = 0; bool f = false; char c = getchar();
 16         while (c < '0' || c > '9') { if (c == '-') f = true; c = getchar(); }
 17         while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
 18         if (f) s = ~s + 1;
 19     }
 20     template<typename T> inline T Abs(T x) { return x > 0 ? x : -x; }
 21     template<typename T> inline T Max(T x, T y) { return x > y ? x : y; }
 22     template<typename T> inline T Min(T x, T y) { return x < y ? x : y; }
 23     template<typename T> inline void addmod(T &x, T p) { if (x >= p) x -= p; }
 24     template<typename T> inline void submod(T &x, T p) { if (x < 0) x += p; }
 25     template<typename T, typename ...Args> inline void read(T& x, Args&... others)
 26     {
 27         read(x), read(others...);
 28     }
 29     template<typename T, typename ...Args> inline T Max(T x, T y, Args... others)
 30     {
 31         return Max(Max(x, y), others...);
 32     }
 33     template<typename T, typename ...Args> inline T Min(T x, T y, Args... others)
 34     {
 35         return Min(Min(x, y), others...);
 36     }
 37 }
 38 using namespace Fast;
 39 
 40 const int N = 1e5 + 10;
 41 int n, root, cnt; ll p, a[N];
 42 struct node
 43 {
 44     int ls, rs, l, r; ll addtag, multag, sum;
 45     node() { clear(); }
 46     inline void clear() { ls = rs = l = r = addtag = sum = 0, multag = 1; }
 47 } t[N << 2];
 48 
 49 inline void pushup(int x) { addmod(t[x].sum = t[t[x].ls].sum + t[t[x].rs].sum, p); }
 50 inline void pushdown(int x)
 51 {
 52     t[t[x].ls].sum = (t[t[x].ls].sum * t[x].multag + t[x].addtag * (t[t[x].ls].r - t[t[x].ls].l + 1)) % p;
 53     t[t[x].ls].addtag = (t[t[x].ls].addtag * t[x].multag + t[x].addtag) % p;
 54     t[t[x].ls].multag = t[t[x].ls].multag * t[x].multag % p;
 55     t[t[x].rs].sum = (t[t[x].rs].sum * t[x].multag + t[x].addtag * (t[t[x].rs].r - t[t[x].rs].l + 1)) % p;
 56     t[t[x].rs].addtag = (t[t[x].rs].addtag * t[x].multag + t[x].addtag) % p;
 57     t[t[x].rs].multag = t[t[x].rs].multag * t[x].multag % p;
 58     t[x].addtag = 0, t[x].multag = 1;
 59 }
 60 void build(int &x, int l, int r)
 61 {
 62     if (!x) x = ++cnt; t[x].l = l, t[x].r = r;
 63     if (l == r) { t[x].sum = a[l]; return; }
 64     int mid = l + r >> 1;
 65     build(t[x].ls, l, mid);
 66     build(t[x].rs, mid + 1, r);
 67     pushup(x);
 68 }
 69 void addmodify(int x, int l, int r, ll v)
 70 {
 71     if (l <= t[x].l && t[x].r <= r)
 72     {
 73         t[x].sum = (t[x].sum + v * (t[x].r - t[x].l + 1)) % p; 
 74         addmod(t[x].addtag += v, p);
 75         return;
 76     }
 77     pushdown(x); int mid = t[x].l + t[x].r >> 1;
 78     if (l <= mid) addmodify(t[x].ls, l, r, v);
 79     if (mid < r) addmodify(t[x].rs, l, r, v);
 80     pushup(x);
 81 }
 82 void mulmodify(int x, int l, int r, ll v)
 83 {
 84     if (l <= t[x].l && t[x].r <= r)
 85     {
 86         t[x].sum = t[x].sum * v % p;
 87         t[x].addtag = t[x].addtag * v % p;
 88         t[x].multag = t[x].multag * v % p;
 89         return;
 90     }
 91     pushdown(x); int mid = t[x].l + t[x].r >> 1;
 92     if (l <= mid) mulmodify(t[x].ls, l, r, v);
 93     if (mid < r) mulmodify(t[x].rs, l, r, v);
 94     pushup(x);
 95 }
 96 ll query(int x, int l, int r)
 97 {
 98     if (l <= t[x].l && t[x].r <= r) return t[x].sum;
 99     pushdown(x); int mid = t[x].l + t[x].r >> 1; ll ans = 0;
100     if (l <= mid) ans += query(t[x].ls, l, r);
101     if (mid < r) ans += query(t[x].rs, l, r);
102     addmod(ans, p); return ans;
103 }
104 
105 int main()
106 {
107     int m, opt, l, r, v;
108     read(n, p); root = ++cnt;
109     for (int i = 1; i <= n; i++) read(a[i]);
110     build(root, 1, n); read(m);
111     while (m--)
112     {
113         read(opt, l, r); if (opt <= 2) read(v);
114         if (opt == 1) mulmodify(root, l, r, v);
115         else if (opt == 2) addmodify(root, l, r, v);
116         else cout << query(root, l, r), enter;
117     }
118     return 0;
119 }
AC Code

P4513 小白逛公园

单点修改,求区间最大连续子段和。

  1 #include <iostream>
  2 #include <cstdio>
  3 using namespace std;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 #define space putchar(' ')
  8 #define enter putchar('\n')
  9 #define debug(x) cerr << #x << " = " << x << endl
 10 
 11 namespace Fast
 12 {
 13     template<typename T> inline void read(T &s)
 14     {
 15         s = 0; bool f = false; char c = getchar();
 16         while (c < '0' || c > '9') { if (c == '-') f = true; c = getchar(); }
 17         while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
 18         if (f) s = ~s + 1;
 19     }
 20     template<typename T> inline T Abs(T x) { return x > 0 ? x : -x; }
 21     template<typename T> inline T Max(T x, T y) { return x > y ? x : y; }
 22     template<typename T> inline T Min(T x, T y) { return x < y ? x : y; }
 23     template<typename T> inline void addmod(T &x, T p) { if (x >= p) x -= p; }
 24     template<typename T> inline void submod(T &x, T p) { if (x < 0) x += p; }
 25     template<typename T, typename ...Args> inline void read(T& x, Args&... others)
 26     {
 27         read(x), read(others...);
 28     }
 29     template<typename T, typename ...Args> inline T Max(T x, T y, Args... others)
 30     {
 31         return Max(Max(x, y), others...);
 32     }
 33     template<typename T, typename ...Args> inline T Min(T x, T y, Args... others)
 34     {
 35         return Min(Min(x, y), others...);
 36     }
 37 }
 38 using namespace Fast;
 39 
 40 const int N = 5e5 + 10;
 41 int n, m, root, cnt, tot; ll a[N];
 42 struct node
 43 {
 44     int ls, rs, l, r; ll sum, maxsum, maxl, maxr;
 45     node() { clear(); }
 46     inline void clear() { ls = rs = l = r = sum = maxsum = maxl = maxr = 0; }
 47 } t[N << 2];
 48 
 49 inline void pushup(int x)
 50 {
 51     t[x].sum = t[t[x].ls].sum + t[t[x].rs].sum;
 52     t[x].maxsum = Max(t[t[x].ls].maxsum, t[t[x].rs].maxsum, t[t[x].ls].maxr + t[t[x].rs].maxl);
 53     t[x].maxl = Max(t[t[x].ls].maxl, t[t[x].ls].sum + t[t[x].rs].maxl);
 54     t[x].maxr = Max(t[t[x].rs].maxr, t[t[x].rs].sum + t[t[x].ls].maxr);
 55 }
 56 void build(int &x, int l, int r)
 57 {
 58     if (!x) x = ++cnt; t[x].l = l, t[x].r = r;
 59     if (l == r)
 60     {
 61         t[x].sum = t[x].maxsum = t[x].maxl = t[x].maxr = a[l];
 62         return;
 63     }
 64     int mid = l + r >> 1;
 65     build(t[x].ls, l, mid);
 66     build(t[x].rs, mid + 1, r);
 67     pushup(x);
 68 }
 69 void modify(int x, int pos, int val)
 70 {
 71     if (t[x].l == t[x].r && t[x].l == pos)
 72     {
 73         t[x].sum = t[x].maxsum = t[x].maxl = t[x].maxr = val;
 74         return;
 75     }
 76     int mid = t[x].l + t[x].r >> 1;
 77     if (pos <= mid) modify(t[x].ls, pos, val);
 78     if (mid < pos) modify(t[x].rs, pos, val);
 79     pushup(x);
 80 }
 81 node query(int x, int l, int r)
 82 {
 83     if (l <= t[x].l && t[x].r <= r) return t[x];
 84     int mid = t[x].l + t[x].r >> 1;
 85     if (r <= mid) return query(t[x].ls, l, r);
 86     if (l > mid) return query(t[x].rs, l, r);
 87     node ansl = query(t[x].ls, l, r), ansr = query(t[x].rs, l, r), ans;
 88     ans.sum = ansl.sum + ansr.sum;
 89     ans.maxsum = Max(ansl.maxsum, ansr.maxsum, ansl.maxr + ansr.maxl);
 90     ans.maxl = Max(ansl.maxl, ansl.sum + ansr.maxl);
 91     ans.maxr = Max(ansr.maxr, ansr.sum + ansl.maxr);
 92     return ans;
 93 }
 94 
 95 int main()
 96 {
 97     read(n, m); root = ++cnt;
 98     for (int i = 1; i <= n; i++) read(a[i]);
 99     build(root, 1, n);
100     while (m--)
101     {
102         int opt, l, r; read(opt, l, r);
103         if (opt == 1)
104         {
105             if (l > r) swap(l, r);
106             cout << query(root, l, r).maxsum, enter;
107         }
108         else modify(root, l, r);
109     }
110     return 0;
111 }
AC Code

P1471 方差

区间加,求区间和以及区间平方和。

  1 #include <iostream>
  2 #include <cstdio>
  3 using namespace std;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 #define space putchar(' ')
  8 #define enter putchar('\n')
  9 #define debug(x) cerr << #x << " = " << x << endl
 10 
 11 namespace Fast
 12 {
 13     template<typename T> inline void read(T &s)
 14     {
 15         s = 0; bool f = false; char c = getchar();
 16         while (c < '0' || c > '9') { if (c == '-') f = true; c = getchar(); }
 17         while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
 18         if (f) s = ~s + 1;
 19     }
 20     template<typename T> inline T Abs(T x) { return x > 0 ? x : -x; }
 21     template<typename T> inline T Max(T x, T y) { return x > y ? x : y; }
 22     template<typename T> inline T Min(T x, T y) { return x < y ? x : y; }
 23     template<typename T> inline void addmod(T &x, T p) { if (x >= p) x -= p; }
 24     template<typename T> inline void submod(T &x, T p) { if (x < 0) x += p; }
 25     template<typename T, typename ...Args> inline void read(T& x, Args&... others)
 26     {
 27         read(x), read(others...);
 28     }
 29     template<typename T, typename ...Args> inline T Max(T x, T y, Args... others)
 30     {
 31         return Max(Max(x, y), others...);
 32     }
 33     template<typename T, typename ...Args> inline T Min(T x, T y, Args... others)
 34     {
 35         return Min(Min(x, y), others...);
 36     }
 37 }
 38 using namespace Fast;
 39 
 40 const int N = 1e5 + 10;
 41 int n, m, root, cnt; double a[N];
 42 struct node
 43 {
 44     int ls, rs, l, r; double sum, sum2, tag;
 45     node() { clear(); }
 46     inline void clear() { ls = rs = l = r = sum = sum2 = tag = 0; }
 47 } t[N << 2];
 48 
 49 inline void pushup(int x)
 50 {
 51     t[x].sum = t[t[x].ls].sum + t[t[x].rs].sum;
 52     t[x].sum2 = t[t[x].ls].sum2 + t[t[x].rs].sum2;
 53 }
 54 inline void pushdown(int x)
 55 {
 56     double len = (t[t[x].ls].r - t[t[x].ls].l + 1.0);
 57     t[t[x].ls].sum2 += 2.0 * t[t[x].ls].sum * t[x].tag + t[x].tag * t[x].tag * len;
 58     t[t[x].ls].sum += t[x].tag * len; t[t[x].ls].tag += t[x].tag;
 59     len = (t[t[x].rs].r - t[t[x].rs].l + 1.0);
 60     t[t[x].rs].sum2 += 2.0 * t[t[x].rs].sum * t[x].tag + t[x].tag * t[x].tag * len;
 61     t[t[x].rs].sum += t[x].tag * len; t[t[x].rs].tag += t[x].tag;
 62     t[x].tag = 0;
 63 }
 64 void build(int &x, int l, int r)
 65 {
 66     if (!x) x = ++cnt; t[x].l = l, t[x].r = r;
 67     if (l == r) { t[x].sum = a[l], t[x].sum2 = a[l] * a[l]; return; }
 68     int mid = l + r >> 1;
 69     build(t[x].ls, l, mid);
 70     build(t[x].rs, mid + 1, r);
 71     pushup(x);
 72 }
 73 void modify(int x, int l, int r, double v)
 74 {
 75     if (l <= t[x].l && t[x].r <= r)
 76     {
 77         double len = (t[x].r - t[x].l + 1.0);
 78         t[x].sum2 += 2.0 * t[x].sum * v + v * v * len;
 79         t[x].sum += v * len; t[x].tag += v; return;
 80     }
 81     pushdown(x); int mid = t[x].l + t[x].r >> 1;
 82     if (l <= mid) modify(t[x].ls, l, r, v);
 83     if (mid < r) modify(t[x].rs, l, r, v);
 84     pushup(x);
 85 }
 86 double query(int x, int l, int r)
 87 {
 88     if (l <= t[x].l && t[x].r <= r) return t[x].sum;
 89     pushdown(x); int mid = t[x].l + t[x].r >> 1; double ans = 0.0;
 90     if (l <= mid) ans += query(t[x].ls, l, r);
 91     if (mid < r) ans += query(t[x].rs, l, r);
 92     return ans; 
 93 }
 94 double query2(int x, int l, int r)
 95 {
 96     if (l <= t[x].l && t[x].r <= r) return t[x].sum2;
 97     pushdown(x); int mid = t[x].l + t[x].r >> 1; double ans = 0.0;
 98     if (l <= mid) ans += query2(t[x].ls, l, r);
 99     if (mid < r) ans += query2(t[x].rs, l, r);
100     return ans; 
101 }
102 
103 int main()
104 {
105     read(n, m); root = ++cnt;
106     for (int i = 1; i <= n; i++) scanf("%lf", &a[i]);
107     build(root, 1, n);
108     while (m--)
109     {
110         int opt, l, r; read(opt, l, r); double v, tmp;
111         if (opt == 1) scanf("%lf", &v), modify(root, l, r, v);
112         else if (opt == 2) printf("%0.4lf\n", query(root, l, r) / (r - l + 1.0));
113         else tmp = query(root, l, r) / (r - l + 1.0), tmp *= tmp,
114             printf("%0.4lf\n", query2(root, l, r) / (r - l + 1.0) - tmp);
115     }
116     return 0;
117 }
AC Code

P6327 区间加区间sin和

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cmath> 
  4 using namespace std;
  5 typedef long long ll;
  6 typedef unsigned long long ull;
  7 typedef long double ld;
  8 #define space putchar(' ')
  9 #define enter putchar('\n')
 10 #define debug(x) cerr << #x << " = " << x << endl
 11 
 12 namespace Fast
 13 {
 14     template<typename T> inline void read(T &s)
 15     {
 16         s = 0; bool f = false; char c = getchar();
 17         while (c < '0' || c > '9') { if (c == '-') f = true; c = getchar(); }
 18         while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
 19         if (f) s = ~s + 1;
 20     }
 21     template<typename T> inline T Abs(T x) { return x > 0 ? x : -x; }
 22     template<typename T> inline T Max(T x, T y) { return x > y ? x : y; }
 23     template<typename T> inline T Min(T x, T y) { return x < y ? x : y; }
 24     template<typename T> inline void addmod(T &x, T p) { if (x >= p) x -= p; }
 25     template<typename T> inline void submod(T &x, T p) { if (x < 0) x += p; }
 26     template<typename T, typename ...Args> inline void read(T& x, Args&... others)
 27     {
 28         read(x), read(others...);
 29     }
 30     template<typename T, typename ...Args> inline T Max(T x, T y, Args... others)
 31     {
 32         return Max(Max(x, y), others...);
 33     }
 34     template<typename T, typename ...Args> inline T Min(T x, T y, Args... others)
 35     {
 36         return Min(Min(x, y), others...);
 37     }
 38 }
 39 using namespace Fast;
 40 
 41 const int N = 2e5 + 10;
 42 int n, m, root, cnt, a[N];
 43 struct node
 44 {
 45     int ls, rs, l, r; ll tag; double Sin, Cos;
 46     node() { clear(); }
 47     inline void clear() { ls = rs = l = r = tag = Sin = Cos = 0; }
 48 } t[N << 2];
 49 
 50 inline void pushup(int x)
 51 {
 52     t[x].Sin = t[t[x].ls].Sin + t[t[x].rs].Sin;
 53     t[x].Cos = t[t[x].ls].Cos + t[t[x].rs].Cos;
 54 }
 55 inline void pushdown(int x)
 56 {
 57     if (!t[x].tag) return;
 58     double Sintag = sin(t[x].tag), Costag = cos(t[x].tag);
 59     double Sin = t[t[x].ls].Sin, Cos = t[t[x].ls].Cos;
 60     t[t[x].ls].Sin = Sin * Costag + Cos * Sintag;
 61     t[t[x].ls].Cos = Cos * Costag - Sin * Sintag;
 62     Sin = t[t[x].rs].Sin, Cos = t[t[x].rs].Cos;
 63     t[t[x].rs].Sin = Sin * Costag + Cos * Sintag;
 64     t[t[x].rs].Cos = Cos * Costag - Sin * Sintag;
 65     t[t[x].ls].tag += t[x].tag, t[t[x].rs].tag += t[x].tag, t[x].tag = 0;
 66 }
 67 void build(int &x, int l, int r)
 68 {
 69     if (!x) x = ++cnt; t[x].l = l, t[x].r = r;
 70     if (l == r) { t[x].Sin = sin(a[l]), t[x].Cos = cos(a[l]); return; }
 71     int mid = t[x].l + t[x].r >> 1;
 72     build(t[x].ls, l, mid);
 73     build(t[x].rs, mid + 1, r);
 74     pushup(x);
 75 }
 76 void modify(int x, int l, int r, int v, double Sinv, double Cosv)
 77 {
 78     if (l <= t[x].l && t[x].r <= r)
 79     {
 80         double Sin = t[x].Sin, Cos = t[x].Cos;
 81         t[x].Sin = Sin * Cosv + Cos * Sinv;
 82         t[x].Cos = Cos * Cosv - Sin * Sinv;
 83         t[x].tag += v; return;
 84     }
 85     pushdown(x); int mid = t[x].l + t[x].r >> 1;
 86     if (l <= mid) modify(t[x].ls, l, r, v, Sinv, Cosv);
 87     if (mid < r) modify(t[x].rs, l, r, v, Sinv, Cosv);
 88     pushup(x);
 89 }
 90 double query(int x, int l, int r)
 91 {
 92     if (l <= t[x].l && t[x].r <= r) return t[x].Sin;
 93     pushdown(x); int mid = t[x].l + t[x].r >> 1; double ans = 0.0;
 94     if (l <= mid) ans += query(t[x].ls, l, r);
 95     if (mid < r) ans += query(t[x].rs, l, r);
 96     return ans;
 97 }
 98 
 99 int main()
100 {
101     read(n); for (int i = 1; i <= n; i++) read(a[i]);
102     root = ++cnt; build(root, 1, n); read(m);
103     while (m--)
104     {
105         int opt, l, r, v; read(opt, l, r);
106         if (opt == 1) read(v), modify(root, l, r, v, sin(v), cos(v));
107         else printf("%0.1lf\n", query(root, l, r));
108     }
109     return 0;
110 }
AC Code

P3792 由乃与大母神原型和偶像崇拜

  1 #include <iostream>
  2 #include <cstdio>
  3 using namespace std;
  4 typedef long long ll;
  5 typedef unsigned long long ull;
  6 typedef long double ld;
  7 #define space putchar(' ')
  8 #define enter putchar('\n')
  9 #define debug(x) cerr << #x << " = " << x << endl
 10 
 11 namespace Fast
 12 {
 13     template<typename T> inline void read(T &s)
 14     {
 15         s = 0; bool f = false; char c = getchar();
 16         while (c < '0' || c > '9') { if (c == '-') f = true; c = getchar(); }
 17         while (c >= '0' && c <= '9') s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
 18         if (f) s = ~s + 1;
 19     }
 20     template<typename T> inline T Abs(T x) { return x > 0 ? x : -x; }
 21     template<typename T> inline T Max(T x, T y) { return x > y ? x : y; }
 22     template<typename T> inline T Min(T x, T y) { return x < y ? x : y; }
 23     template<typename T> inline T addmod(T &x, T p) { if (x >= p) x -= p; return x; }
 24     template<typename T> inline T submod(T &x, T p) { if (x < 0) x += p; return x; }
 25     template<typename T, typename ...Args> inline void read(T& x, Args&... others)
 26     {
 27         read(x), read(others...);
 28     }
 29     template<typename T, typename ...Args> inline T Max(T x, T y, Args... others)
 30     {
 31         return Max(Max(x, y), others...);
 32     }
 33     template<typename T, typename ...Args> inline T Min(T x, T y, Args... others)
 34     {
 35         return Min(Min(x, y), others...);
 36     }
 37 }
 38 using namespace Fast;
 39 
 40 const int N = 5e5 + 10;
 41 const ll p = 998244353;
 42 const ll inv2 = 499122177;
 43 const ll inv6 = 166374059;
 44 int n, m, root, cnt, a[N];
 45 struct node
 46 {
 47     int ls, rs, l, r, maxnum, minnum; ll sum, sum2;
 48     node() { clear(); }
 49     inline void clear() { ls = rs = l = r = maxnum = minnum = sum = sum2 = 0; }
 50 } t[N << 2];
 51 
 52 inline void pushup(int x)
 53 {
 54     t[x].maxnum = Max(t[t[x].ls].maxnum, t[t[x].rs].maxnum);
 55     t[x].minnum = Min(t[t[x].ls].minnum, t[t[x].rs].minnum);
 56     t[x].sum = t[t[x].ls].sum + t[t[x].rs].sum; addmod(t[x].sum, p);
 57     t[x].sum2 = t[t[x].ls].sum2 + t[t[x].rs].sum2; addmod(t[x].sum2, p);
 58 }
 59 void build(int &x, int l, int r)
 60 {
 61     if (!x) x = ++cnt; t[x].l = l, t[x].r = r;
 62     if (l == r)
 63     {
 64         t[x].maxnum = t[x].minnum = t[x].sum = a[l];
 65         t[x].sum2 = 1ll * a[l] * a[l] % p; return;
 66     }
 67     int mid = t[x].l + t[x].r >> 1;
 68     build(t[x].ls, l, mid);
 69     build(t[x].rs, mid + 1, r);
 70     pushup(x);
 71 }
 72 void modify(int x, int pos)
 73 {
 74     if (t[x].l == t[x].r && pos == t[x].l)
 75     {
 76         t[x].maxnum = t[x].minnum = t[x].sum = a[pos];
 77         t[x].sum2 = 1ll * a[pos] * a[pos] % p; return;
 78     }
 79     int mid = t[x].l + t[x].r >> 1;
 80     if (pos <= mid) modify(t[x].ls, pos);
 81     if (mid < pos) modify(t[x].rs, pos);
 82     pushup(x);
 83 }
 84 node query(int x, int l, int r)
 85 {
 86     if (l <= t[x].l && t[x].r <= r) return t[x];
 87     int mid = t[x].l + t[x].r >> 1;
 88     if (r <= mid) return query(t[x].ls, l, r);
 89     if (l > mid) return query(t[x].rs, l, r);
 90     node ansl = query(t[x].ls, l, r), ansr = query(t[x].rs, l, r), ans;
 91     ans.maxnum = Max(ansl.maxnum, ansr.maxnum);
 92     ans.minnum = Min(ansl.minnum, ansr.minnum);
 93     ans.sum = ansl.sum + ansr.sum; addmod(ans.sum, p);
 94     ans.sum2 = ansl.sum2 + ansr.sum2; addmod(ans.sum2, p);
 95     return ans;
 96 }
 97 
 98 inline ll Sum(ll n) { return n * (n + 1) % p * inv2 % p; }
 99 inline ll Sum2(ll n) { return n * (n + 1) % p * (2 * n + 1) % p * inv6 % p; }
100 int main()
101 {
102     read(n, m); root = ++cnt;
103     for (int i = 1; i <= n; i++) read(a[i]); build(root, 1, n);
104     while (m--)
105     {
106         int opt, l, r; read(opt, l, r);
107         if (opt == 1) a[l] = r, modify(root, l);
108         else
109         {
110             node ans = query(root, l, r); ll tmp;
111             puts(ans.maxnum - ans.minnum == r - l &&
112                 ans.sum == submod(tmp = Sum(ans.maxnum) - Sum(ans.minnum - 1), p) &&
113                 ans.sum2 == submod(tmp = Sum2(ans.maxnum) - Sum2(ans.minnum - 1), p)
114                 ? "damushen" : "yuanxing");
115         }
116     }
117     return 0;
118 }
AC Code

 

 

 

posted @ 2021-12-05 19:37  jhqqwq  阅读(68)  评论(0编辑  收藏  举报