Submission #931961

# Submission time Handle Problem Language Result Execution time Memory
931961 2024-02-22T16:23:54 Z pan Growing Trees (BOI11_grow) C++17
100 / 100
241 ms 14076 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<ll, pi> pii;
char t;
ll n, q, x, y;
ll const INF = 1e13;
struct node{
 
    int s, e, m; 
    ll val, minn; 
    ll lazy; 
    node *l, *r; 
    node (int S, int E)
    { 
       s = S, e = E, m = (s+e)/2;
       val = 0; 
       lazy = 0; 
       l=r=nullptr;
 
    }
    void create()
    {
		if(s != e && l==nullptr)
       { 
           l = new node(s, m); 
           r = new node(m+1, e); 
       }
    }
 
    void propogate()
    {
       if (lazy==0) return; 
       create();
       val+=lazy; 
       minn+=lazy;
       if (s != e){ 
           l->lazy+=lazy;
           r->lazy+=lazy;
       }
       lazy=0; 
    }
    void update(int S, int E, ll V)
    { 
	   propogate();
       if(s==S && e==E) lazy += V; 
       else{ 
           create();
           if(E <= m) l->update(S, E, V); 
           else if (m < S) r->update(S, E, V); 
           else l->update(S, m, V),r->update(m+1, E, V);
           l->propogate(),r->propogate();
           val = max(l->val, r->val); 
           minn = min(l->minn, r->minn);
       }
       
    }
    
    ll query(int S, int E)
	{
 
       propogate(); 
       if(s == S && e == E) return val; 
       create();
       if(E <= m) return l->query(S, E); 
       else if(S >= m+1) return r->query(S, E);
       else return max(l->query(S, m), r->query(m+1, E)); 
   }
 
 
   ll bsmin(ll V) // find lowest ind with val>=V
   {
       propogate();
	   if (s==e) return (val>=V)?s:INF;
	   create();
	   l->propogate(); r->propogate();
	   if (l->val>=V) return l->bsmin(V);
	   else return r->bsmin(V);
   }
   ll bsmax(ll V)// find highest ind with val<=V
   {
       propogate();
	   if (s==e) return (val<=V)?s:-INF;
	   create();
	   l->propogate(); r->propogate();
	   if (r->minn<=V) return r->bsmax(V);
	   else return l->bsmax(V);
   }
   
 
} *root;
 
 
int main()
{
	
	input2(n, q);
	ll arr[n];
	for (ll i=0; i<n; ++i) input(arr[i]);
	sort(arr, arr+n);
	root = new node(0, n-1);
	for (ll i=0; i<n; ++i) root->update(i, i, arr[i]);
	while (q--)
	{
		cin >> t >> x >> y;
		if (t=='F')//update
		{
			ll start = root->bsmin(y);
			ll end = start+x-1;
			if (start==INF) continue;
			if (end>=n-1) {root->update(start, n-1, 1); continue;}
			ll maxele = root->query(end,end);
			ll l = root->bsmin(maxele);
			ll r = root->bsmax(maxele);
			if (start<=l-1) root->update(start, l-1, 1);
			ll left = x-max(0LL, l-start);
			if (left>0) root->update(r-left+1, r, 1);
		}
		else
		{
			ll ans = max(0LL, root->bsmax(y)-root->bsmin(x)+1);
			print(ans, '\n');
		}
	}
	return 0;
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
      |                      ~~~~~^~~~~~~~~~~~~~~~~~~~
grow.cpp:122:2: note: in expansion of macro 'input2'
  122 |  input2(n, q);
      |  ^~~~~~
grow.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | #define input(x) scanf("%lld", &x);
      |                  ~~~~~^~~~~~~~~~~~
grow.cpp:124:25: note: in expansion of macro 'input'
  124 |  for (ll i=0; i<n; ++i) input(arr[i]);
      |                         ^~~~~
# Verdict Execution time Memory Grader output
1 Correct 145 ms 12124 KB Output is correct
2 Correct 196 ms 11860 KB Output is correct
3 Correct 82 ms 11344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 604 KB Output is correct
2 Correct 6 ms 604 KB Output is correct
3 Correct 7 ms 604 KB Output is correct
4 Correct 5 ms 604 KB Output is correct
5 Correct 147 ms 1156 KB Output is correct
6 Correct 175 ms 1364 KB Output is correct
7 Correct 8 ms 860 KB Output is correct
8 Correct 157 ms 1208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 159 ms 1616 KB Output is correct
2 Correct 189 ms 1624 KB Output is correct
3 Correct 3 ms 1372 KB Output is correct
4 Correct 145 ms 2152 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 1832 KB Output is correct
2 Correct 185 ms 2468 KB Output is correct
3 Correct 11 ms 1368 KB Output is correct
4 Correct 166 ms 2628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 130 ms 9172 KB Output is correct
2 Correct 191 ms 10060 KB Output is correct
3 Correct 41 ms 2652 KB Output is correct
4 Correct 63 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 10328 KB Output is correct
2 Correct 214 ms 10844 KB Output is correct
3 Correct 74 ms 9560 KB Output is correct
4 Correct 42 ms 3140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 135 ms 11092 KB Output is correct
2 Correct 158 ms 12184 KB Output is correct
3 Correct 77 ms 10832 KB Output is correct
4 Correct 41 ms 2904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 193 ms 11900 KB Output is correct
2 Correct 179 ms 10640 KB Output is correct
3 Correct 50 ms 13652 KB Output is correct
4 Correct 135 ms 12408 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 175 ms 11772 KB Output is correct
2 Correct 184 ms 12116 KB Output is correct
3 Correct 188 ms 13484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 241 ms 14076 KB Output is correct