Submission #848958

# Submission time Handle Problem Language Result Execution time Memory
848958 2023-09-13T18:33:08 Z Ahmed_Solyman Monkey and Apple-trees (IZhO12_apple) C++14
100 / 100
240 ms 186736 KB
/*
In the name of Allah
made by: Ahmed_Solyman
*/
#include <bits/stdc++.h>
#include <ext/rope>

using namespace std;
using namespace __gnu_cxx;
#pragma GCC optimize("O3")
//-------------------------------------------------------------//
typedef long long ll;
typedef unsigned long long ull;
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define allr(v) v.rbegin(),v.rend()
#define sum_to(n) (n*(n+1))/2
#define pb push_back
#define pf push_front
#define fil(arr,x) memset(arr,x,sizeof(arr))
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define F first
#define S second
#define MP make_pair
#define endl '\n'
const ll mod=1e9+7;
int dx[8]={0,1,0,-1,1,1,-1,-1};
int dy[8]={1,0,-1,0,1,-1,-1,1};
//-------------------------------------------------------------//
ll lcm(ll a,ll b)
{
    return (max(a,b)/__gcd(a,b))*min(a,b);
}
void person_bool(bool x)
{
    cout<<(x?"YES":"NO")<<endl;
}
struct Node {
	int sum, lazy, tl, tr, l, r;
	Node() : sum(0), lazy(0), l(-1), r(-1) {}
};

const int MAXN = 123456;
Node segtree[64 * MAXN];
int cnt = 2;

void push_lazy(int node) {
	if (segtree[node].lazy) {
		segtree[node].sum = segtree[node].tr - segtree[node].tl + 1;
		int mid = (segtree[node].tl + segtree[node].tr) / 2;
		if (segtree[node].l == -1) {
			segtree[node].l = cnt++;
			segtree[segtree[node].l].tl = segtree[node].tl;
			segtree[segtree[node].l].tr = mid;
		}
		if (segtree[node].r == -1) {
			segtree[node].r = cnt++;
			segtree[segtree[node].r].tl = mid + 1;
			segtree[segtree[node].r].tr = segtree[node].tr;
		}
		segtree[segtree[node].l].lazy = segtree[segtree[node].r].lazy = 1;
		segtree[node].lazy = 0;
	}
}

void update(int node, int l, int r) {
	push_lazy(node);
	if (l == segtree[node].tl && r == segtree[node].tr) {
		segtree[node].lazy = 1;
		push_lazy(node);
	} else {
		int mid = (segtree[node].tl + segtree[node].tr) / 2;
		if (segtree[node].l == -1) {
			segtree[node].l = cnt++;
			segtree[segtree[node].l].tl = segtree[node].tl;
			segtree[segtree[node].l].tr = mid;
		}
		if (segtree[node].r == -1) {
			segtree[node].r = cnt++;
			segtree[segtree[node].r].tl = mid + 1;
			segtree[segtree[node].r].tr = segtree[node].tr;
		}

		if (l > mid) update(segtree[node].r, l, r);
		else if (r <= mid) update(segtree[node].l, l, r);
		else {
			update(segtree[node].l, l, mid);
			update(segtree[node].r, mid + 1, r);
		}

		push_lazy(segtree[node].l);
		push_lazy(segtree[node].r);
		segtree[node].sum =
		    segtree[segtree[node].l].sum + segtree[segtree[node].r].sum;
	}
}

int query(int node, int l, int r) {
	push_lazy(node);
	if (l == segtree[node].tl && r == segtree[node].tr)
		return segtree[node].sum;
	else {
		int mid = (segtree[node].tl + segtree[node].tr) / 2;
		if (segtree[node].l == -1) {
			segtree[node].l = cnt++;
			segtree[segtree[node].l].tl = segtree[node].tl;
			segtree[segtree[node].l].tr = mid;
		}
		if (segtree[node].r == -1) {
			segtree[node].r = cnt++;
			segtree[segtree[node].r].tl = mid + 1;
			segtree[segtree[node].r].tr = segtree[node].tr;
		}

		if (l > mid) return query(segtree[node].r, l, r);
		else if (r <= mid) return query(segtree[node].l, l, r);
		else
			return query(segtree[node].l, l, mid) +
			       query(segtree[node].r, mid + 1, r);
	}
}
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    fast
    segtree[1].tl=1;
    segtree[1].tr=1e9;
    int q;cin>>q;
    int c=0;
    while(q--){
        int t,l,r;cin>>t>>l>>r;
        if(t==1){
            c=query(1,l+c,r+c);
            cout<<c<<endl;
        }
        else{
            update(1,l+c,r+c);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 31 ms 185936 KB Output is correct
2 Correct 30 ms 185916 KB Output is correct
3 Correct 30 ms 185936 KB Output is correct
4 Correct 38 ms 185936 KB Output is correct
5 Correct 40 ms 185940 KB Output is correct
6 Correct 40 ms 185940 KB Output is correct
7 Correct 40 ms 185940 KB Output is correct
8 Correct 102 ms 186036 KB Output is correct
9 Correct 181 ms 186116 KB Output is correct
10 Correct 196 ms 186324 KB Output is correct
11 Correct 195 ms 186196 KB Output is correct
12 Correct 190 ms 186364 KB Output is correct
13 Correct 172 ms 186652 KB Output is correct
14 Correct 174 ms 186452 KB Output is correct
15 Correct 230 ms 186452 KB Output is correct
16 Correct 240 ms 186736 KB Output is correct
17 Correct 171 ms 186320 KB Output is correct
18 Correct 171 ms 186448 KB Output is correct
19 Correct 240 ms 186452 KB Output is correct
20 Correct 229 ms 186380 KB Output is correct