HDU 4027Can you answer these queries?

题意:先说下写这题的感受吧,在已知这个题开根号的次数不会超过7次的情况下,还是写了2个多小时,完全整个人都是懵的,出了很多小错误,讲道理,这题就只有要注意开根号的次数,加上类似于剪枝的东西,是能过的,但就是很菜,最后交的手都软了,还得多练练线段树;
#include<algorithm>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<cstring>
#include<cstdio>
#include<cmath>
#define N 100005
#define INF 0x3f3f3f3f
using namespace std;
typedef struct node {
	int x;
	int y;
	long long date;
	long long p;
} node;
node a[N*4];
long long b[N];
long long w[N];
void built(int root,int first,int end) {
	if(first==end) {
		a[root].date=b[first];
		a[root].x=first;
		a[root].y=end;
		a[root].p=0;
		return ;
	}
	int mid=(first+end)/2;
	built(root*2,first,mid);
	built(root*2+1,mid+1,end);
	a[root].x=a[root*2].x;
	a[root].y=a[root*2+1].y;
	a[root].date=a[root*2].date+a[root*2+1].date;
	a[root].p=0;
}
void U(int root,int first,int end,int l,int r) {
	if(l<=first&&end<=r) {
		a[root].p+=1;
		return ;
	}
	int mid=(first+end)/2;
	if(a[root].p!=0) {
		if(a[root].p>7) {
			a[root].date=(end-first+1);
		}
		a[root*2].p+=a[root].p;
		a[root*2+1].p+=a[root].p;
		a[root].p=0;
	}
	if(l<=mid)  U(root*2,first,mid,l,r);
	if(r>mid)  U(root*2+1,mid+1,end,l,r);
	a[root].date=a[root*2].date+a[root*2+1].date;
}
long long sum;
void Q(int root,int first,int end,int l,int r) {
	if(l<=first&&end<=r) {
		if(a[root].date==(end-first+1)) {
			sum+=a[root].date;
			return ;
		}
		if(first==end) {
			if(a[root].p>7) {
				a[root].date=1;
				a[root].p=0;
				sum+=a[root].date;
				return ;
			}
			if(a[root].p<=7&&a[root].p>0) {
				long long t=a[root].date;
				for(int i=1; i<=a[root].p; i++) {
					t=(int)sqrt(t);
					if(t==1) {
						break;
					}
				}
				a[root].date=t;
				a[root].p=0;
				sum+=a[root].date;
				return ;
			} else {
				sum+=a[root].date;
				return ;
			}
		}
	}
	int mid=(first+end)/2;
	if(a[root].p!=0) {
		if(a[root].p>7) {
			a[root].date=(end-first+1);
		}
		a[root*2].p+=a[root].p;
		a[root*2+1].p+=a[root].p;
		a[root].p=0;
	}
	if(l<=mid)  Q(root*2,first,mid,l,r);
	if(r>mid)  Q(root*2+1,mid+1,end,l,r);
	a[root].date=a[root*2].date+a[root*2+1].date;
}
int main() {
	int m;
	int j=0;
	while(scanf("%d",&m)!=EOF) {
		j++;
		memset(w,0,sizeof(w));
		printf("Case #%d:\n",j);
		for(int i=1; i<=m; i++) {
			scanf("%I64d",&b[i]);
		}
		built(1,1,m);
		int n;
		scanf("%d",&n);
		long long d,c,e;
		for(int i=1; i<=n; i++) {
			scanf("%I64d %I64d %I64d",&d,&c,&e);
			if(c>e)  swap(c,e);
			if(d==0) {
				U(1,1,m,c,e);
			} else if(d==1) {
				sum=0;
				Q(1,1,m,c,e);
				printf("%I64d\n",sum);
			}
		}
		printf("\n");
	}
	return 0;
}

posted @ 2017-10-03 20:25  wang9897  阅读(88)  评论(0编辑  收藏  举报