线段树,为什么样例没过却A了
查看原帖
线段树,为什么样例没过却A了
418419
ko_no_lzx_da楼主2022/3/15 13:16
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
#define ll long long 
using namespace std;
int L,R;
struct node{
	ll maxx,sum;
	//int l,r;
}tree[1000000];
ll a[1000000];
void ch(int l,int r,int n){
	if(l==r&&l>=L&&r<=R){
		tree[n].sum=sqrt(tree[n].sum);
		tree[n].maxx=sqrt(tree[n].maxx);
		return; 
	}
	int m=(r+l)/2;
	if(L<=m&&tree[n*2].maxx>1)ch(l,m,n*2);
	if(m<R&&tree[n*2+1].maxx>1)ch(m+1,r,n*2+1);
	tree[n].maxx=max(tree[n*2].maxx,tree[n*2+1].maxx);
	tree[n].sum=tree[n*2+1].sum+tree[n*2].sum;
}
void build(int l,int r,int n){
	if(l==r){
		tree[n].maxx=tree[n].sum=a[l];
		return;
	}
	int m=(l+r)/2;
	build(l,m,n*2);
	build(m+1,r,n*2+1);
	tree[n].maxx=max(tree[n*2].maxx,tree[n*2+1].maxx);
	tree[n].sum=tree[n*2+1].sum+tree[n*2].sum;
}
ll visit(int l,int r,int n){
	ll ans=0;
	if(L<=l&&r<=R){
		return tree[n].sum;
	}
	int m=(l+r)/2;
	if(L<=m)ans+=visit(l,m,n*2);
	if(m<R)ans+=visit(m+1,r,n*2+1);
	return ans;
}
int n,m;
int main(){
	cin >>n;
	for(int i=1;i<=n;i++)cin >>a[i];
	build(1,n,1);
	cin >>m;
	for(int i=1;i<=m;i++){
		int k;
		cin >>k>>L>>R;
		if(L>R)swap(L,R);
		if(k){
			cout <<visit(1,n,1)<<endl;
		}else{
			ch(1,n,1);
		}
	}
	return 0;
}


2022/3/15 13:16
加载中...