HDU4348:To the moon

主席树。区间更新区间查询。调的要死。因为update 忘了op->col=t->col。然后一直WA。。。而且开始自己的写法是错的。。。后来就换了一种写法。。。QAQ

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
ll readll(){
	ll x=0;char c=getchar();bool f=true;
	while(!isdigit(c)) {
		if(c==‘-‘) f=false;c=getchar();
	}
	while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
	return f?x:-x;
}
int read(){
	int x=0;char c=getchar();bool f=true;
	while(!isdigit(c)) {
		if(c==‘-‘) f=false;c=getchar();
	}
	while(isdigit(c)) x=x*10+c-‘0‘,c=getchar();
	return f?x:-x;
}
const int nmax=100005;
const int inf=0x7f7f7f7f;
struct node{
	node *l,*r;ll sum,col;
};
node nodes[nmax*24],*root[nmax],*pt;
node* build(int l,int r){
	node* op=pt++;op->col=0;
	if(l==r){
		op->sum=readll();return op;
	}
	int mid=(l+r)>>1;
	op->l=build(l,mid);op->r=build(mid+1,r);
	op->sum=op->l->sum+op->r->sum;
	return op;
}
node* update(int tl,int tr,ll add,int l,int r,node* t){
	node* op=pt++;op->sum=t->sum+add*(tr-tl+1);	op->l=t->l,op->r=t->r;op->col=t->col;//if not here is wrong
	if(tl==l&&tr==r) {
		op->col+=add;return op;
	}
	int mid=(l+r)>>1;
	if(tr<=mid) op->l=update(tl,tr,add,l,mid,t->l);
	else if(tl>mid) op->r=update(tl,tr,add,mid+1,r,t->r);
	else op->l=update(tl,mid,add,l,mid,t->l),op->r=update(mid+1,tr,add,mid+1,r,t->r);
	/*if(tl<=mid) op->l=update(tl,mid,add,l,mid,t->l);
	if(tr>mid) op->r=update(mid+1,tr,add,mid+1,r,t->r);*/
	return op;
}
ll query(int tl,int tr,int l,int r,node* t){
	if(tl==l&&tr==r) return t->sum;
	int mid=(l+r)>>1;ll ans=t->col*(tr-tl+1);
	if(tr<=mid) return ans+=query(tl,tr,l,mid,t->l);
	if(tl>mid) return ans+=query(tl,tr,mid+1,r,t->r);
	return ans+=query(tl,mid,l,mid,t->l)+query(mid+1,tr,mid+1,r,t->r);
	/*if(tl<=mid) ans+=query(tl,mid,l,mid,t->l);
	if(tr>mid) ans+=query(mid+1,tr,mid+1,r,t->r);if(tl==4,tr==4)wrong answer...*/
}
int main(){
	int n,m;
	while(scanf("%d%d",&n,&m)!=EOF){
		pt=nodes;
		root[0]=build(1,n);
		node* cur=root[0];
		int u,v,cnt=0;ll d;char s[5];
		rep(i,1,m){
			scanf("%s",s);
			if(s[0]==‘C‘){
				u=read(),v=read(),d=readll();
				root[++cnt]=update(u,v,d,1,n,cur);
				cur=root[cnt];
			}else if(s[0]==‘Q‘){
				u=read(),v=read();
				printf("%lld\n",query(u,v,1,n,cur));
			}else if(s[0]==‘H‘){
				u=read(),v=read(),d=read();
				printf("%lld\n",query(u,v,1,n,root[d]));
			}else{
				cnt=u=read();cur=root[cnt];
			}
		}
	}
	return 0;
}

  

时间: 2024-12-16 11:05:49

HDU4348:To the moon的相关文章

[HDU4348]To the moon(主席树+标记永久化)

学可持久化treap的时候才发现自己竟然没写过需要标记下传的主席树,然而现在发现大部分操作都可以标记永久化,下传会增大占用空间. 这题一种写法是和普通的线段树一样标记下传,注意所有修改操作(包括put())都要新建点.于是MLE了. 1 #include<cstdio> 2 #include<algorithm> 3 #define lson v[x].ls,L,mid 4 #define rson v[x].rs,mid+1,R 5 #define rep(i,l,r) for

hdu4348 - To the moon 可持久化线段树 区间修改 离线处理

法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区间被完全包含,返回sum,否则加上add * 询问落在这段区间的长度再递归回答. 怎么还是MLE? 麻辣鸡指针好像8字节,所以改成数组的就过了... #include<cstdio> #include<cstring> #include<cstdlib> #include&

【HDU4348】【主席树】To the moon

Problem Description BackgroundTo The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.The premise of To The Moon is based around a technology that allows us to permanently reconstruct the

HDU 4348 / SPOJ TTM - To the moon

终于过了..感谢xiaodao提供测试数据,然后最终找到了一个十分ruozhi的BUG...= =,坑爹..没什么好说的,直接上主席树的干活...下面是HDU的代码,如果是SPOJ自行把%I64d换成%lld继续干活.. 1 /* 2 ID:esxgx1 3 LANG:C++ 4 PROG:hdu4348 5 */ 6 #include <cassert> 7 #include <cstdio> 8 #include <cstring> 9 #include <i

hdu 5318 The Goddess Of The Moon(矩阵快速幂)

题目链接:hdu 5318 The Goddess Of The Moon 将50个串处理成50*50的矩阵,注意重复串. #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int maxn = 55; const int mod = 1e9+7; int N, M, A[maxn]; struct M

FZU 2148 Moon Game 判断凸边形

点击打开链接 Problem 2148 Moon Game Accept: 512    Submit: 1419 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a k

FZOJ Problem 2148 Moon Game

                                                                                              Problem 2148 Moon Game Problem Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consid

ACM学习历程—FZU2148 Moon Game(计算几何)

Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a kind of two-dimensional plane. Then Fat brother starts to draw N starts in the sky which we can just consid

ACM: FZU 2148 Moon Game - 海伦公式

FZU 2148  Moon Game Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description Fat brother and Maze are playing a kind of special (hentai) game in the clearly blue sky which we can just consider as a kind of two-dimensio