Submission #483635

# Submission time Handle Problem Language Result Execution time Memory
483635 2021-10-31T12:01:08 Z MilosMilutinovic Growing Trees (BOI11_grow) C++14
100 / 100
558 ms 4608 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

const int N=101000;
int n,q,a[N];
struct node{
	ll sm;
}nd[4*N];
void modify(int p,int l,int r,int tl,int x) {
	if (tl<l||tl>r) return;
	if (l==r) nd[p].sm+=x;
	else {
		int md=(l+r)>>1;
		if (tl<=md) modify(p+p,l,md,tl,x);
		else modify(p+p+1,md+1,r,tl,x);
		nd[p].sm=nd[p+p].sm+nd[p+p+1].sm;
	}
}
int get(int p,int l,int r,int tl) {
	if (l>tl) return 0;
	if (r<=tl) return nd[p].sm;
	int md=(l+r)>>1;
	return get(p+p,l,md,tl)+get(p+p+1,md+1,r,tl);
}
int main() {
	scanf("%d%d",&n,&q);
	rep(i,1,n+1) scanf("%d",a+i);
	sort(a+1,a+n+1);
	rep(i,1,n+1) modify(1,1,n,i,a[i]),modify(1,1,n,i+1,-a[i]);
	while (q--) {
		char t;
		scanf("\n%c",&t);
		int x,y;
		scanf("%d%d",&x,&y);
		if (t=='F') {
			if (get(1,1,n,n)<y) continue;
			int pos;
			{
				int l=1,r=n;
				while (l<=r) {
					int md=(l+r)>>1;
					if (get(1,1,n,md)>=y) pos=md,r=md-1;
					else l=md+1;
				}
			}
			x=min(x,n-pos+1);
			if (get(1,1,n,pos)==get(1,1,n,pos+x-1)) {
				int l=pos,r=n,ll=n;
				while (l<=r) {
					int md=(l+r)>>1;
					if (get(1,1,n,md)==get(1,1,n,pos)) ll=md,l=md+1;
					else r=md-1;
				}
				modify(1,1,n,max(pos,ll-x+1),1);
				modify(1,1,n,ll+1,-1);
			} else {
				int tl;
				{
					int l=pos,r=n;
					while (l<=r) {
						int md=(l+r)>>1;
						if (get(1,1,n,md)<get(1,1,n,pos+x-1)) tl=md,l=md+1;
						else r=md-1;
					}
				}
				modify(1,1,n,pos,1);
				modify(1,1,n,tl+1,-1);
				++tl;
				int tr=-1;
				{
					int l=tl,r=n;
					while (l<=r) {
						int md=(l+r)>>1;
						if (get(1,1,n,md)==get(1,1,n,tl)) tr=md,l=md+1;
						else r=md-1;
					}
				}
				if (tr!=-1) {
					x-=(tl-pos);
					modify(1,1,n,tr-x+1,1);
					modify(1,1,n,tr+1,-1);
				}
			}
		} else {
			if (get(1,1,n,n)<x||get(1,1,n,1)>y) {
				printf("0\n");
				continue;
			}
			int fg,fl;
			{
				int l=1,r=n;
				while (l<=r) {
					int md=(l+r)>>1;
					if (get(1,1,n,md)>=x) fg=md,r=md-1;
					else l=md+1;
				}
			}
			{
				int l=1,r=n;
				while (l<=r) {
					int md=(l+r)>>1;
					if (get(1,1,n,md)<=y) fl=md,l=md+1;
					else r=md-1;
				}
			}
			printf("%d\n",fl-fg+1);
		}
	}
}

Compilation message

grow.cpp: In function 'int main()':
grow.cpp:44:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |  scanf("%d%d",&n,&q);
      |  ~~~~~^~~~~~~~~~~~~~
grow.cpp:45:20: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   45 |  rep(i,1,n+1) scanf("%d",a+i);
      |               ~~~~~^~~~~~~~~~
grow.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   50 |   scanf("\n%c",&t);
      |   ~~~~~^~~~~~~~~~~
grow.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |   scanf("%d%d",&x,&y);
      |   ~~~~~^~~~~~~~~~~~~~
grow.cpp:55:8: warning: 'pos' may be used uninitialized in this function [-Wmaybe-uninitialized]
   55 |    int pos;
      |        ^~~
grow.cpp:124:20: warning: 'fl' may be used uninitialized in this function [-Wmaybe-uninitialized]
  124 |    printf("%d\n",fl-fg+1);
      |                  ~~^~~
grow.cpp:124:20: warning: 'fg' may be used uninitialized in this function [-Wmaybe-uninitialized]
grow.cpp:85:11: warning: 'tl' may be used uninitialized in this function [-Wmaybe-uninitialized]
   85 |     modify(1,1,n,tl+1,-1);
      |     ~~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 218 ms 2884 KB Output is correct
2 Correct 424 ms 4332 KB Output is correct
3 Correct 232 ms 4292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 332 KB Output is correct
2 Correct 6 ms 332 KB Output is correct
3 Correct 5 ms 332 KB Output is correct
4 Correct 3 ms 332 KB Output is correct
5 Correct 136 ms 1484 KB Output is correct
6 Correct 147 ms 1620 KB Output is correct
7 Correct 15 ms 540 KB Output is correct
8 Correct 48 ms 1216 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 162 ms 744 KB Output is correct
2 Correct 162 ms 1860 KB Output is correct
3 Correct 3 ms 460 KB Output is correct
4 Correct 68 ms 1360 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 126 ms 760 KB Output is correct
2 Correct 189 ms 1720 KB Output is correct
3 Correct 25 ms 460 KB Output is correct
4 Correct 163 ms 1860 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 217 ms 1728 KB Output is correct
2 Correct 373 ms 3968 KB Output is correct
3 Correct 47 ms 1228 KB Output is correct
4 Correct 193 ms 4004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 337 ms 2860 KB Output is correct
2 Correct 364 ms 4020 KB Output is correct
3 Correct 236 ms 4240 KB Output is correct
4 Correct 49 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 201 ms 2844 KB Output is correct
2 Correct 220 ms 3980 KB Output is correct
3 Correct 248 ms 4420 KB Output is correct
4 Correct 46 ms 1228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 2840 KB Output is correct
2 Correct 372 ms 2904 KB Output is correct
3 Correct 76 ms 3396 KB Output is correct
4 Correct 135 ms 3780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 299 ms 2900 KB Output is correct
2 Correct 397 ms 4336 KB Output is correct
3 Correct 558 ms 4608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 311 ms 3136 KB Output is correct