Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <vector>
- #include <queue>
- #include <algorithm>
- #include <string>
- #include <stack>
- #include <set>
- #include <map>
- #define pii pair <int,int>
- using namespace std;
- using ll = long long;
- using ld = long double;
- void cv(vector <int> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- void cvl(vector <ll> &v){
- for (auto x: v) cout<<x<<' ';
- cout<<"\n";
- }
- int l=0,r=10;
- int inter(int tl, int tr){
- if (tl >= l && tr <= r) return 2;
- else if (tr >= l && tl < l) return 1;
- else if (tl <= r && tr > r) return 1;
- else return 0;
- }
- void cvv(vector <vector <int> > &v){
- for (auto x: v) cv(x);
- cout<<"\n";
- }
- int n, mxn; //mxn - округлена до верхней стпени двойки
- //в конце ф-иии build: n = mxn
- vector <int> a;
- ll inf = 2e9;
- struct tree{
- vector <ll> t;
- vector <ll> to_add;
- void bld(){
- mxn = log2(n);
- //cout<<"n mxn = "<<n<<' '<<mxn<<"\n";
- if (pow(2, mxn) < n){
- mxn++;
- }
- mxn = pow(2, mxn);
- t.assign(2 * mxn - 1, -inf);
- to_add.assign(2 * mxn - 1, 0);
- a.resize(mxn, -inf);
- for (int i = 0; i < n; ++i){
- cin>>a[i];
- }
- int id = 2*mxn-2;
- for (int i = mxn-1; i >= 0; --i){
- t[id] = a[i];
- id--;
- }
- for (int i = 2 * mxn - 2; i >= 2; i -= 2){
- t[(i-1)/2] = max(t[i] , t[i-1]);
- }
- n = mxn;
- }
- void sh(){
- cout<<"tree\n";
- cvl(t);
- cout<<"\n\n";
- }
- void push(int v, int tl, int tr){
- if (to_add[v] == 0) return;
- int to = 2*v+1;
- int tm = (tl + tr) / 2;
- to_add[to] += to_add[v];
- t[to] += to_add[v];
- to++;
- to_add[to] += to_add[v];
- t[to] += to_add[v];
- to_add[v] = 0;
- //t[v] = max(t[to], t[to-1]);
- }
- ll get(int v, int tl, int tr){
- //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
- if (inter(tl, tr) == 0) {
- //cout<<"not intersect\n";
- return -inf;
- }
- else if (inter(tl, tr) == 2){
- //cout<<"contains\n";
- return t[v];
- }
- else if (inter(tl, tr) == 1){
- //cout<<"partly intersect\n";
- push(v, tl, tr);
- int tm = (tl + tr) / 2;
- return max(get(v*2+1,tl, tm) , get(2*v+2, tm+1, tr));
- }
- }
- void add(int v, int tl, int tr, int val){
- //cout<<"tl tr = "<<tl<<' '<<tr<<"\n";
- if (inter(tl, tr) == 0) {
- //cout<<"not intersect\n";
- return;
- }
- else if (inter(tl, tr) == 2){
- //cout<<"contains\n";
- to_add[v] += val;
- t[v] += val;
- }
- else{
- //cout<<"partly intersect\n";
- int tm = (tl + tr) / 2;
- push(v, tl, tr);
- add(2*v+1, tl, tm, val);
- add(2*v+2, tm+1, tr, val);
- t[v] = max(t[v*2+1], t[v*2+2]);
- }
- }
- void req(){
- char x; cin>>x;
- cin>>l>>r;
- l--; r--;
- //cout<<"request = "<<x<<"\n";
- //cout<<"l r = "<<l<<" "<<r<<"\n";
- if (x == 'm'){
- ll res = get(0,0,n-1);
- //cout<<"max("<<l<<","<<r<<") = "<<res<<"\n";
- cout<<res<<' ';
- }else{
- int val; cin>>val;
- //cout<<"val = "<<val<<"\n";
- add(0, 0, n-1, val);
- }
- //sh();
- }
- };
- int main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin>>n;
- tree wrk;
- wrk.bld();
- //wrk.sh();
- //int a,b; cin>>a>>b; cout<<inter(a, b);
- int q; cin>>q; for (int i=0;i<q;++i) {
- wrk.req();
- }
- }
- /*
- 5
- 2 4 3 1 5
- 5
- m 1 3
- a 2 4 100
- m 1 3
- a 5 5 10
- m 1 5
- */
Add Comment
Please, Sign In to add comment