Submission #997279

# Submission time Handle Problem Language Result Execution time Memory
997279 2024-06-12T01:21:20 Z JuanJL Monkey and Apple-trees (IZhO12_apple) C++17
100 / 100
208 ms 222388 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 

#define fst first 
#define snd second
#define pb push_back
#define forn(i,a,b) for(int i = a; i < b; i++)
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
#define mset(a,v) memset((a),(v),sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define dbg(v) cout<<"Line("<<__LINE__<<"): "<<#v<<" = "<<v<<'\n';
#define pi pair<int,int>
#define pll pair<ll,ll>
typedef long long ll;
using namespace std;
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
typedef tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_multiset;

struct Node{
	ll val, l,r;
	Node(){ val=0;  l = r = -1; }
};

struct STree { // example: range sum with range addition
	int indice = 2;
	vector<Node> st;
	vector<int> lazy;int n;
	int M;
	STree(int n, int m): M(m), st(4*n+5,Node()), lazy(4*n+5,0), n(n) {}

	void push(int k, int s, int e){
		if(!lazy[k])return; // if neutral, nothing to do
		st[k].val=(e-s)*lazy[k]; // update st according to lazy
		//cout<<"("<<st[k].val<<") "<<s<<" <-> "<<e<<" -- "<<k<<'\n';
		if(s+1<e){ // propagate to children
			if(st[k].l==-1){ st[k].l=indice; indice+=1; }
			if(st[k].r==-1){ st[k].r=indice; indice+=1; }
			lazy[st[k].l]=lazy[k];
			lazy[st[k].r]=lazy[k];
		}
		lazy[k]=0; // clear node lazy
	}
	void upd(int k, int s, int e, int a, int b, int v){
		push(k,s,e);
		if(s>=b||e<=a)return;
		if(s>=a&&e<=b){
			//cout<<s<<" <-> "<<e<<'\n';
			lazy[k]=v; // accumulate lazy
			push(k,s,e);return;
		}
		int m=(s+e)/2;
		if(st[k].l==-1){ st[k].l=indice; indice+=1; }
		if(st[k].r==-1){ st[k].r=indice; indice+=1;}
		upd(st[k].l,s,m,a,b,v);upd(st[k].r,m,e,a,b,v);
		st[k].val=st[st[k].l].val+st[st[k].r].val; // operation
	}
	int query(int k, int s, int e, int a, int b){
		if(s>=b||e<=a)return 0; // operation neutral
		push(k,s,e);
		//cout<<k<<" -> "<<s<<" <-> "<<e<<'\n';
		if(s>=a&&e<=b)return st[k].val;
		int m=(s+e)/2;
		if(st[k].l==-1){ st[k].l=indice; indice+=1; }
		if(st[k].r==-1){ st[k].r=indice; indice+=1; }
		return query(st[k].l,s,m,a,b)+query(st[k].r,m,e,a,b); // operation
	}
	
	void upd(int a, int b, int v){upd(1,0,M,a,b,v);}
	int query(int a, int b){return query(1,0,M,a,b);}
};

int main(){FIN;
	ll m; cin>>m;
	ll t,c,pC; c=0;
	ll l,r;
	STree stree(2000000+5,1000000000+5);
	
	while(m--){
		cin>>t>>l>>r;
		
		/*if(c+r+1>=1000000+2){
			while(true){}
		}*/
		
		if(t==2){
			stree.upd(c+l,c+r+1,1);
		}else{
			//cout<<"QUERY\n";
			pC=stree.query(l+c,r+c+1);
			cout<<pC<<'\n';
			c=pC;
		}
	}
	return 0;
}

Compilation message

apple.cpp: In constructor 'STree::STree(int, int)':
apple.cpp:31:6: warning: 'STree::M' will be initialized after [-Wreorder]
   31 |  int M;
      |      ^
apple.cpp:29:15: warning:   'std::vector<Node> STree::st' [-Wreorder]
   29 |  vector<Node> st;
      |               ^~
apple.cpp:32:2: warning:   when initialized here [-Wreorder]
   32 |  STree(int n, int m): M(m), st(4*n+5,Node()), lazy(4*n+5,0), n(n) {}
      |  ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 29 ms 219484 KB Output is correct
2 Correct 27 ms 219476 KB Output is correct
3 Correct 28 ms 219484 KB Output is correct
4 Correct 34 ms 219648 KB Output is correct
5 Correct 36 ms 219484 KB Output is correct
6 Correct 36 ms 219664 KB Output is correct
7 Correct 33 ms 219484 KB Output is correct
8 Correct 99 ms 219660 KB Output is correct
9 Correct 165 ms 219984 KB Output is correct
10 Correct 166 ms 219912 KB Output is correct
11 Correct 163 ms 219920 KB Output is correct
12 Correct 164 ms 219988 KB Output is correct
13 Correct 149 ms 219984 KB Output is correct
14 Correct 137 ms 219980 KB Output is correct
15 Correct 189 ms 220912 KB Output is correct
16 Correct 206 ms 222088 KB Output is correct
17 Correct 144 ms 222036 KB Output is correct
18 Correct 142 ms 222032 KB Output is correct
19 Correct 208 ms 222388 KB Output is correct
20 Correct 195 ms 222292 KB Output is correct