Submission #744052

# Submission time Handle Problem Language Result Execution time Memory
744052 2023-05-18T07:43:03 Z zaneyu Weirdtree (RMI21_weirdtree) C++17
100 / 100
568 ms 48536 KB
#include "weirdtree.h"
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using pii=pair<ll,ll>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
struct node{
	int m1,m2;
	ll sum;
	int cnt=0;
}seg[4*maxn];
ll lazy[4*maxn];
int arr[maxn];
int n;
int MX;
node merge(node a,node b){
	node c;
	c.m1=max(a.m1,b.m1);
	c.sum=a.sum+b.sum;
	if(a.m1==b.m1){
		c.cnt=a.cnt+b.cnt;
		c.m2=max(a.m2,b.m2);
	}
	else if(c.m1==a.m1){
		c.cnt=a.cnt;
		c.m2=max(a.m2,b.m1);
	}
	else{
		c.cnt=b.cnt;
		c.m2=max(a.m1,b.m2);
	}
	return c;
}
void pushdown(int idx,int l,int r){
	if(!lazy[idx]) return;
	seg[idx].sum+=seg[idx].cnt*lazy[idx];
	seg[idx].m1+=lazy[idx];
	if(l==r){
		lazy[idx]=0;
		return;
	}
	for(int c:{idx*2,idx*2+1}){
		if(lazy[c]+seg[c].m1==seg[idx].m1-lazy[idx]) lazy[c]+=lazy[idx];
	}
	lazy[idx]=0;
}
void build(int idx,int l,int r){
	lazy[idx]=0;
	if(l==r){
		seg[idx].sum=seg[idx].m1=arr[l],seg[idx].m2=-1;
		seg[idx].cnt=1;
		return;
	}
	int mid=(l+r)/2;
	build(idx*2,l,mid),build(idx*2+1,mid+1,r);
	seg[idx]=merge(seg[idx*2],seg[idx*2+1]);
}
void upd(int idx,int l,int r,int p,int x){
	pushdown(idx,l,r);
	if(r<p or l>p) return;
	if(l==r){
		seg[idx].sum=seg[idx].m1=x;
		return;
	}
	int mid=(l+r)/2;
	upd(idx*2,l,mid,p,x),upd(idx*2+1,mid+1,r,p,x);
	seg[idx]=merge(seg[idx*2],seg[idx*2+1]);
}
void upd2(int idx,int l,int r,int ql,int qr,int x){
	pushdown(idx,l,r);
	if(r<ql or l>qr or seg[idx].m1<MX) return;
	if(ql<=l and r<=qr and seg[idx].m1-x>seg[idx].m2){
		lazy[idx]-=x;
		pushdown(idx,l,r);
		return;
	}
	int mid=(l+r)/2;
	upd2(idx*2,l,mid,ql,qr,x),upd2(idx*2+1,mid+1,r,ql,qr,x);
	seg[idx]=merge(seg[idx*2],seg[idx*2+1]);
}
node query(int idx,int l,int r,int ql,int qr){
	pushdown(idx,l,r);
	if(ql<=l and r<=qr) return seg[idx];
	int mid=(l+r)/2;
	if(qr<=mid) return query(idx*2,l,mid,ql,qr);
	if(ql>mid) return query(idx*2+1,mid+1,r,ql,qr);
	return merge(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}
int need;
int find(int idx,int l,int r,int ql,int qr){
	pushdown(idx,l,r);
	if(r<ql or l>qr or seg[idx].m1<MX) return -1;
	//cout<<idx<<l<<' '<<r<<'\n';
	if(ql<=l and r<=qr){
		if(seg[idx].cnt<need){
			need-=seg[idx].cnt;
			return -1;
		}
	}
	if(l==r) return l;
	int mid=(l+r)/2;
	int z=find(idx*2,l,mid,ql,qr);
	if(z!=-1) return z;
	return find(idx*2+1,mid+1,r,ql,qr);
}
void initialise(int N, int Q, int h[]) {
	n=N;
	REP(i,N) arr[i]=h[i+1];
	build(1,0,n-1);
}
void cut(int l, int r, int k) {
	--l,--r;
	while(k){
		node cur=query(1,0,n-1,l,r);
		if(cur.m1==0) break;
		MX=cur.m1;
		ll df=cur.m1-cur.m2;
		MXTO(cur.m2,0);
		//cout<<cur.m1<<' '<<cur.m2<<' '<<cur.cnt<<'\n';
		if(df*cur.cnt<=k){
			k-=df*cur.cnt;
			upd2(1,0,n-1,l,r,df);
			continue;
		}
		
		upd2(1,0,n-1,l,r,k/cur.cnt);
		MX-=(k/cur.cnt);
		need=(k%(cur.cnt));
		if(need){
			int p=find(1,0,n-1,l,r);
			//cout<<p<<' ';
			upd2(1,0,n-1,l,p,1);
		}
		break;
	}
}
void magic(int i, int x) {
	--i;
	upd(1,0,n-1,i,x);
}
long long int inspect(int l, int r) {
	--l,--r;
	return query(1,0,n-1,l,r).sum;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28468 KB Output is correct
3 Correct 83 ms 31628 KB Output is correct
4 Correct 98 ms 31648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28500 KB Output is correct
2 Correct 15 ms 28568 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 28500 KB Output is correct
2 Correct 15 ms 28568 KB Output is correct
3 Correct 533 ms 41548 KB Output is correct
4 Correct 408 ms 41552 KB Output is correct
5 Correct 568 ms 41416 KB Output is correct
6 Correct 398 ms 41312 KB Output is correct
7 Correct 18 ms 31240 KB Output is correct
8 Correct 65 ms 33028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 31240 KB Output is correct
2 Correct 65 ms 33028 KB Output is correct
3 Correct 216 ms 41024 KB Output is correct
4 Correct 228 ms 41120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28468 KB Output is correct
3 Correct 83 ms 31628 KB Output is correct
4 Correct 98 ms 31648 KB Output is correct
5 Correct 16 ms 28500 KB Output is correct
6 Correct 15 ms 28568 KB Output is correct
7 Correct 18 ms 31240 KB Output is correct
8 Correct 65 ms 33028 KB Output is correct
9 Correct 83 ms 33612 KB Output is correct
10 Correct 99 ms 33484 KB Output is correct
11 Correct 80 ms 33544 KB Output is correct
12 Correct 105 ms 33608 KB Output is correct
13 Correct 90 ms 33620 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 28500 KB Output is correct
2 Correct 13 ms 28468 KB Output is correct
3 Correct 83 ms 31628 KB Output is correct
4 Correct 98 ms 31648 KB Output is correct
5 Correct 16 ms 28500 KB Output is correct
6 Correct 15 ms 28568 KB Output is correct
7 Correct 533 ms 41548 KB Output is correct
8 Correct 408 ms 41552 KB Output is correct
9 Correct 568 ms 41416 KB Output is correct
10 Correct 398 ms 41312 KB Output is correct
11 Correct 216 ms 41024 KB Output is correct
12 Correct 228 ms 41120 KB Output is correct
13 Correct 83 ms 33612 KB Output is correct
14 Correct 99 ms 33484 KB Output is correct
15 Correct 80 ms 33544 KB Output is correct
16 Correct 105 ms 33608 KB Output is correct
17 Correct 90 ms 33620 KB Output is correct
18 Correct 18 ms 31240 KB Output is correct
19 Correct 65 ms 33028 KB Output is correct
20 Correct 331 ms 48096 KB Output is correct
21 Correct 438 ms 48536 KB Output is correct
22 Correct 335 ms 48336 KB Output is correct
23 Correct 404 ms 48340 KB Output is correct
24 Correct 332 ms 48188 KB Output is correct