// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
std::ifstream terminal("/dev/tty");
#define PP cerr<<"\033[1;30mpause...\e[0m",terminal.ignore();
#else
#define debug(x...)
#define Debug(x...)
#define PP
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x),__VA_ARGS__})
#define cmax(x,...) x=max({(x),__VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define uint unsigned long long
#define MAXN 200010
template <class T> struct treap {
struct node {
node *ch[2];
int rd, size;
T val;
};
node *root = nullptr;
T DEFAULT_VALUE;
vector<node> pool;
treap(int N) {
pool.resize(N);
}
int total = 0;
node *newnode(T val) {
node *x = &pool[total++];
x->val = val;
x->rd = rand();
x->size = 1;
return x;
}
int getsize(node *x) {return x ? x->size : 0; }
void pushup(node *x) {
if (!x) return;
x->size = getsize(x->ch[0]) + getsize(x->ch[1]) + 1;
T lc = x->ch[0] ? x->ch[0]->val : DEFAULT_VALUE;
T rc = x->ch[1] ? x->ch[1]->val : DEFAULT_VALUE;
x->val.pushup(lc, rc);
}
void pushdown(node *x) {
if (!x) return;
if (x->val.rev) swap(x->ch[0], x->ch[1]);
if (x->ch[0]) x->ch[0]->val.pushdown(x->val);
if (x->ch[1]) x->ch[1]->val.pushdown(x->val);
x->val.clear();
}
pair<node*, node*> split(node *x, int size) {
if (!x) return {nullptr, nullptr};
pushdown(x);
if (size <= getsize(x->ch[0])) {
auto [lc, rc] = split(x->ch[0], size);
x->ch[0] = rc;
pushup(x);
return {lc, x};
}
auto [lc, rc] = split(x->ch[1], size - getsize(x->ch[0]) - 1);
x->ch[1] = lc;
pushup(x);
return {x, rc};
}
node *merge(node *x, node *y) {
if (!x && !y) return nullptr;
if (!x) return y;
if (!y) return x;
pushdown(x), pushdown(y);
if (x->rd < y->rd) {
x->ch[1] = merge(x->ch[1], y);
pushup(x);
return x;
}
y->ch[0] = merge(x, y->ch[0]);
pushup(y);
return y;
}
void insert(T val, int rank) {
auto [l, r] = split(root, rank);
node *mid = merge(l, newnode(val));
root = merge(mid, r);
}
vector<T> inorder() {
vector<T> res;
function<void(node*)> dfs = [&](node *x) {
if (!x) return;
pushdown(x);
dfs(x->ch[0]);
res.push_back(x->val);
dfs(x->ch[1]);
};
dfs(root);
return res;
}
tuple<node*, node*, node*> interval(int l, int r) {
auto [L, y] = split(root, r+1);
auto [x, mid] = split(L, l);
return {x, mid, y};
}
void graphviz_dump(string filename = "graph.dot") {
FILE *f = fopen(filename.c_str(), "w");
fprintf(f, "digraph {\n");
rep(i, 0, total) {
node *x = &pool[i];
fprintf(f, "%lld.c_str());
rep(j, 1, sz(desired)) fprintf(f, " | %s", desired[j].c_str());
fprintf(f, "\" shape=\"record\"];\n");
}
rep(i, 0, total) {
node *x = &pool[i];
if (x->ch[0]) fprintf(f, " %lld -> %ld;\n", i, x->ch[0] - &pool[0]);
if (x->ch[1]) fprintf(f, " %lld -> %ld;\n", i, x->ch[1] - &pool[0]);
}
fprintf(f, "}\n");
}
void modify(int l, int r) {
auto [x, mid, y] = interval(l, r);
mid->val.rev ^= 1;
root = merge(merge(x, mid), y);
}
int query(int l, int r) {
auto [x, mid, y] = interval(l, r);
int res = mid->val.sum;
root = merge(merge(x, mid), y);
return res;
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
struct value {
int c = 0, sum = 0, rev = 0;
void pushup(value &lc, value &rc) {
sum = lc.sum + rc.sum + c;
}
void pushdown(value &parent) {
rev ^= parent.rev;
}
void clear() {
rev = 0;
}
};
int n, q; cin >> n >> q;
treap<value> t(n);
rep(i, 0, n) {
int x; cin >> x;
t.insert({x, 0, 0}, i);
}
while (q--) {
int op, l, r; cin >> op >> l >> r; --l, --r;
if (op == 1) t.modify(l, r);
else printf("%lld\n", t.query(l, r));
}
return 0;
}
No Comments