萌新刚学珂朵莉树求助

P5350 序列

```cpp #include <cmath> #include <cstdio> using LL = long long; #ifdef __linux__ using LLL = __int128; #else using LLL = long long; #endif class IO { #define MY_DEBUG 0 #define isdigit(x) (x >= '0' && x <= '9') static const int MAXSIZE = 1 << 20; char buf[MAXSIZE], *p1, *p2; char pbuf[MAXSIZE], *pp; int precision; public: #if MY_DEBUG #else IO() : p1(buf), p2(buf), pp(pbuf), precision(6) { } ~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); } #endif inline bool blank(char ch) const { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } void flush() { fwrite(pbuf, 1, pp - pbuf, stdout), pp = pbuf; } void filein(const char* str) const { freopen(str, "rb", stdin); } void fileout(const char* str) const { freopen(str, "wb", stdout); } inline int getch() { #if MY_DEBUG return getchar(); #endif if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin); return p1 == p2 ? -1 : *p1++; } template <typename T, typename... Args> void read(T& x, Args&... args) { read(x); read(args...); } void read() {} template <typename T> void read(T& x) { double tmp = 1; bool sign = 0; x = 0; int ch = getch(); for (; !isdigit(ch) && ~ch; ch = getch()) if (ch == '-') sign = 1; for (; isdigit(ch); ch = getch()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = getch(); isdigit(ch); ch = getch()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; } void read(char& ch) { for (ch = getch(); blank(ch) && ~ch; ch = getch()) ; } void read(char* s) { int ch = getch(); while (blank(ch)) ch = getch(); while (!blank(ch) && ~ch) *s++ = ch, ch = getch(); *s = 0; } void readline(char* s) { int ch = getch(); while (blank(ch) && ch != '\n') ch = getch(); while (ch != '\n' && ~ch) *s++ = ch, ch = getch(); *s = 0; } void putch(const char c) { #if MY_DEBUG putchar(c); #else if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf; *pp++ = c; #endif } void setprecision(int n) { precision = n; } template <typename T, typename... Args> void write(const T& x, const Args&... args) { write(x); write(args...); } void write() {} template <typename T> void write(T x) { if (x < 0) x = -x, putch('-'); static T sta[40]; int top = 0; do sta[top++] = x % 10, x /= 10; while (x); while (top) putch(sta[--top] + '0'); } void write(char c) { putch(c); } void write(double x) { const double eps = pow(10, -precision - 2); if (x == 0) { putch('0'), putch('.'); for (int i = 1; i <= precision; ++i) putch('0'); return; } if (x < 0) putch('-'), x = -x; LLL n = pow(10, precision); double res = (LLL)(x * n + 0.5) / (n * 1.0); LLL y = LLL(res * n + eps) % n; if (precision) { write(LLL(res + eps), '.'); int sta[20], p = 0; for (; p < precision; y /= 10) sta[++p] = y % 10; for (int i = p; i >= 1; i--) putch(sta[i] ^ 48); } else write(LLL(res + eps)); } void write(const char* s) { while (*s) putch(*s++); } } io; #define writeln(...) io.write(__VA_ARGS__), io.putch('\n') #define dbg(x) io.write(#x " = "), writeln(x) #undef isdigit // define fast io #include <algorithm> #include <cassert> #include <set> #include <vector> #define int long long #define check l > x ? (std::swap(l, x), std::swap(r, y)) : ( void )0 const int MOD = 1000000007; struct Node { mutable int l, r, v; Node(int l, int r = -1, int v = 0) : l(l), r(r), v(v) {} bool operator<(const Node& rhs) const { return l < rhs.l; } }; std::set<Node> tree; auto split(int p) { auto it = tree.lower_bound(Node(p)); if (it != tree.end() && it->l == p) return it; it--; int r = it->r; it->r = p - 1; return tree.emplace(p, r, it->v).first; } void assign(int l, int r, int v) { auto it1 = split(l), it2 = split(r + 1); tree.erase(it1, it2); tree.emplace(l, r, v); } void modify(int l, int r, int v) { auto it1 = split(l), it2 = split(r + 1); for (auto it = it1; it != it2; it++) it->v = (it->v + v) % MOD; } void copyto(int l, int r, int x, int y) { std::set<Node>::iterator it1, it2, it3, it4; if (x > l) it1 = split(l), it2 = split(r + 1), it3 = split(x), it4 = split(y + 1); else it3 = split(x), it4 = split(y + 1), it1 = split(l), it2 = split(r + 1); std::vector<Node> v; for (auto it = it1; it != it2; it++) v.emplace_back(*it); tree.erase(it3, it4); for (auto it = v.begin(); it != v.end(); it++) tree.emplace(it->l + x - l, it->r + y - r, it->v); } void swap(int l, int r, int x, int y) { std::set<Node>::iterator it1, it2, it3, it4; if (x > l) it1 = split(l), it2 = split(r + 1), it3 = split(x), it4 = split(y + 1); else it3 = split(x), it4 = split(y + 1), it1 = split(l), it2 = split(r + 1); decltype(tree) t; t.clear(); for (auto it = it1; it != it2; it++) t.emplace(it->l, it->r, it->v); copyto(x, y, l, r); tree.erase(it3, it4); for (auto it = t.begin(); it != t.end(); it++) tree.emplace(it->l + x - l, it->r + y - r, it->v); } void reverse(int l, int r) { auto it1 = split(l), it2 = split(r + 1); std::vector<Node> v; v.clear(); for (auto it = it1; it != it2; it++) v.push_back(*it); tree.erase(it1, it2); for (auto it = v.rbegin(); it != v.rend(); it++) tree.emplace(l + r - it->r, l + r - it->l, it->v); } int query(int l, int r) { auto it1 = split(l), it2 = split(r + 1); int sum = 0; for (auto it = it1; it != it2; it++) sum = (sum + 1LL * (it->r - it->l + 1) * it->v % MOD) % MOD; return sum; } signed main() { int n, m; io.read(n, m); for (int i = 1; i <= n; i++) { int x; io.read(x); tree.emplace(i, i, x); } tree.emplace(n + 1, n + 1, 0); for (int i = 1; i <= m; i++) { int opt, l, r, x, y, v; io.read(opt, l, r); if (opt == 1) writeln(query(l, r)); if (opt == 2) io.read(v), assign(l, r, v); if (opt == 3) io.read(v), modify(l, r, v); if (opt == 4) io.read(x, y), copyto(l, r, x, y); if (opt == 5) io.read(x, y), swap(l, r, x, y); if (opt == 6) reverse(l, r); } tree.erase(tree.lower_bound(Node(n + 1))); for (auto it = tree.begin(); it != tree.end(); it++) for (int i = it->l; i <= it->r; i++) io.write(it->v, " "); writeln(); return 0; } ```
by andyli @ 2020-03-06 10:10:37


[qnmx](https://www.luogu.com.cn/discuss/show/198439)
by 空与白之凌 @ 2020-03-06 10:11:21


@[andyli](/user/84282)
by 空与白之凌 @ 2020-03-06 10:11:25


~~用平衡树多好。~~
by 万万没想到 @ 2020-03-06 10:20:55


@[andyli](/user/84282) 请问您 swap 里面的 ` decltype(tree) t;` 是什么意思那?谢谢,我写 ODT 不这样写qwq~
by ieeqwq @ 2020-03-06 11:31:22


@[我谔谔](/user/127284) 就是`std::set<Node>`
by andyli @ 2020-03-06 11:32:14


@[我谔谔](/user/127284) C++11中`decltype`是自动类型推断
by andyli @ 2020-03-06 11:32:39


@[我谔谔](/user/127284) qwq这与odt没关系吧
by andyli @ 2020-03-06 11:33:09


@[andyli](/user/84282) qwq我只是有点迷看不懂 /cy
by ieeqwq @ 2020-03-06 11:37:04


@[andyli](/user/84282) 我看不出错了,告辞
by ieeqwq @ 2020-03-06 11:40:32


| 下一页