Submission #483632

# Submission time Handle Problem Language Result Execution time Memory
483632 2021-10-31T11:56:17 Z MilosMilutinovic Growing Trees (BOI11_grow) C++14
10 / 100
452 ms 5024 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)<x) 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 Incorrect 59 ms 3916 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 136 ms 1508 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 27 ms 1612 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 174 ms 2736 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 212 ms 3692 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 183 ms 3704 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 452 ms 4556 KB Output is correct
2 Incorrect 243 ms 3848 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 229 ms 4164 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 313 ms 5024 KB Output is correct