线段树95pts,WA on #20
查看原帖
线段树95pts,WA on #20
999274
CNS_5t0_0r2楼主2024/10/21 12:53
#include<bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lchild (root << 1)
#define rchild ((root << 1) + 1)
using namespace std;
inline char gc(){
    static char buf[10000005],*t1 = buf,*t2 = buf;
    return t1 == t2 && (t2 = (t1 = buf) + fread(buf,1,1000000,stdin),t1 == t2) ? EOF : *t1++;
}
int read1(){
	char c = gc();
	int x = 0;
	while(c < '0' || c > '9')
		c = gc();
	while(c >= '0' && c <= '9'){
		x = (x << 3) + (x << 1) + c - 48;
		c = gc();
	}
	return x;
}
long long read2(){
	char c = gc();
	long long x = 0;
	while(c < '0' || c > '9')
		c = gc();
	while(c >= '0' && c <= '9'){
		x = (x << 3) + (x << 1) + c - 48;
		c = gc();
	}
	return x;
}
const int N = 2e5 + 9;
int n,q;
long long W;
int a[N];

struct node{
	long long val;
	long long tag;//区间加标记 
} t[N << 2];

int len(int l,int r){
	return r - l + 1;
}

bool in_range(int l,int r,int L,int R){//[l,r]是否在修改/查询区间[L,R]中 
	return L <= l && r <= R;
}
bool out_range(int l,int r,int L,int R){//[l,r]是否与修改/查询区间[L,R]无交集 
	return l > R || r < L;
}

void push_up(int root){
	t[root].val = t[lchild].val + t[rchild].val;
}

void make_tag(int root,int len,int v){
	t[root].tag += v;
	t[root].val += 1ll * v * len;
}
void push_down(int root,int l,int r){
	make_tag(lchild,len(l,mid),t[root].tag);
	make_tag(rchild,len(mid + 1,r),t[root].tag);
	t[root].tag = 0;
}

void build(int root,int l,int r){
	if(l == r){
		t[root].val = a[l];
		return;
	}
	build(lchild,l,mid);
	build(rchild,mid + 1,r);
	push_up(root);
}

//将区间 [L,R] 内的所有数加 v 
void update(int root,int l,int r,int L,int R,int v){
	if(out_range(l,r,L,R))
		return;
	if(in_range(l,r,L,R)){
		make_tag(root,len(l,r),v);
		return;
	}
	push_down(root,l,r);
	update(lchild,l,mid,L,R,v);
	update(rchild,mid + 1,r,L,R,v);
	push_up(root);
}

int query(int root,int l,int r,long long v,long long w){
	if(l == r)
		return (int)(v * t[root].val < w);
	push_down(root,l,r);
	if(v * t[lchild].val < w)
		return len(l,mid) + query(rchild,mid + 1,r,v,w - v * t[lchild].val);
	return query(lchild,l,mid,v,w);
}

signed main(){
	ios::sync_with_stdio(false);
	cout.tie(0);
	n = read1();q = read1();W = read2();
	for(int i = 1;i <= n;i++)
		a[i] = read1();
	build(1,1,n);
	while(q--){
		int l = read1(),r = read1(),d = read1();
		update(1,1,n,l,r,d);
		long long tmp = W,i = t[1].val;
		long long ans = 0;
		while(tmp > i){
			tmp -= i;
			i <<= 1;
			ans += n;
		}
		ans += query(1,1,n,i / t[1].val,tmp);
		cout << ans << '\n';
	}
	return 0;
}
2024/10/21 12:53
加载中...