// 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);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#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 MAXN 200010
struct Splay {
struct node {
int rk, size, val, sum, cnt;
node *ch[2], *parent;
};
node *newnode(int rank, int val) {
node *x = &datalist[total++];
x->rk = rank, x->val = val;
x->ch[0] = x->ch[1] = x->parent = nullptr;
x->cnt = 1;
return x;
}
node *root = nullptr;
int total = 0;
vector<node> datalist;
vector<int> address;
Splay(int n) {
datalist.resize(n);
}
int get(node *x) {return x->parent->ch[1] == x; }
int getsize(node *x) {return x ? x->size : 0; }
int getsum(node *x) {return x ? x->sum : 0; }
void pushup(node *x) {
if (!x) return;
x->size = getsize(x->ch[0]) + getsize(x->ch[1]) + x->cnt;
x->sum = getsum(x->ch[0]) + getsum(x->ch[1]) + x->val*x->cnt;
}
void pushdown(node *x) {
}
void rotate(node *x) {
node *y = x->parent;
node *z = y->parent;
int t = get(x);
y->ch[t] = x->ch[t ^ 1];
if (x->ch[t ^ 1]) x->ch[t ^ 1]->parent = y;
x->ch[t ^ 1] = y;
y->parent = x;
if (z) z->ch[y == z->ch[1]] = x;
x->parent = z;
pushup(y);
pushup(x);
}
void splay(node *x, node *target) {
for (node *f; f = x->parent, f != target; rotate(x)) {
if (f->parent != target) rotate(get(x) == get(f) ? f : x);
}
if (!target) root = x;
}
void insert(int rank, int val, int id) {
if (!root) {
root = newnode(rank, val);
return;
}
node *x = root;
for (; ; x = x->ch[rank > x->rk]) {
if (x->rk == rank) {
address[id] = x - &datalist[0];
++x->cnt;
pushup(x);
pushup(x->parent);
splay(x, nullptr);
return;
}
if (!x->ch[rank > x->rk]) {
address[id] = total;
x->ch[rank > x->rk] = newnode(rank, val);
x->ch[rank > x->rk]->parent = x;
//
pushup(x->ch[rank > x->rk]);
pushup(x);
//
splay(x->ch[rank > x->rk], nullptr);
return;
}
}
}
node *kth(int k) { // 0-index
node *x = root; pushdown(x);
for (pushdown(x); ; pushdown(x)) {
if (k == getsize(x->ch[0])) return x;
if (k < getsize(x->ch[0])) x = x->ch[0];
else k -= getsize(x->ch[0]) + 1, x = x->ch[1];
}
return nullptr;
}
// void remove(int l, int r) {
// splay(kth(l - 1), nullptr);
// splay(kth(r + 1), root);
// root->ch[1]->ch[0] = nullptr;
// splay(root->ch[1], nullptr);
// }
node *predecessor(node *x) {
if (x->ch[0]) {
x = x->ch[0];
while (x->ch[1]) x = x->ch[1];
return x;
}
while (x->parent && x->parent->ch[0] == x) x = x->parent;
return x->parent;
}
node *successor(node *x) {
if (x->ch[1]) {
x = x->ch[1];
while (x->ch[0]) x = x->ch[0];
return x;
}
while (x->parent && x->parent->ch[1] == x) x = x->parent;
return x->parent;
}
void remove(node *x) {
if (x->cnt > 1) {
--x->cnt;
pushup(x);
splay(x, nullptr);
return;
}
splay(predecessor(x), nullptr);
splay(successor(x), root);
root->ch[1]->ch[0] = nullptr;
pushup(root->ch[1]);
splay(root->ch[1], nullptr);
}
void graphviz_dump(string filename = "graph.dot") {
FILE *f = fopen(filename.c_str(), "w");
fprintf(f, "digraph {\n");
rep(i, 0, total) {
node *x = &datalist[i];
fprintf(f, " %lld;\n");
}
rep(i, 0, total) {
node *x = &datalist[i];
if (x->ch[0]) fprintf(f, " %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
if (x->ch[1]) fprintf(f, " %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
}
fprintf(f, "}\n");
}
void op1(int k, int val) {
node *x = &datalist[address[k]];
remove(x);
insert(val, val, k);
}
int op2(int a, int b) {
node *l = nullptr, *r = nullptr;
node *x = root;
for (pushdown(x); x; pushdown(x)) {
if (x->rk >= a) x = x->ch[0];
else l = x, x = x->ch[1];
}
x = root;
for (pushdown(x); x; pushdown(x)) {
if (x->rk <= b) x = x->ch[1];
else r = x, x = x->ch[0];
}
splay(l, nullptr);
splay(r, root);
return getsize(root->ch[1]->ch[0]);
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n, q; cin >> n >> q;
Splay t(n+q+2); t.address.resize(n+2);
rep(i, 0, n) {
int x; cin >> x;
t.insert(x, x, i);
}
t.insert(-INF, -INF, n), t.insert(INF, INF, n+1);
while (q--) {
string op; int a, b;
cin >> op >> a >> b;
if (op[0] == '!') t.op1(a-1, b);
else printf("%lld\n", t.op2(a, b));
}
return 0;
}
No Comments