hihocoder 1058 线段树

#include  
#include
#include   
#include  
#include
#include  
#include
#include
#include
#include
using namespace std;  
#define lch(i) ((i)<<1)  
#define rch(i) ((i)<<1|1)  
#define sqr(i) ((i)*(i))  
#define pii pair  
#define mp make_pair  
#define FOR(i,b,e) for(int i=b;i<=e;i++)  
#define FORE(i,b,e) for(int i=b;i>=e;i--)  
#define ms(a)   memset(a,0,sizeof(a))  
const int maxnum =50003;
const  int  mod = 10007;
int n,m;
int st;
char lock[50005];

struct node
{
	int l,r;
	int all,add,st,step;
	int mid(){return (l+r)>>1;}
	char c;
}tree[maxnum<<2];

void buildtree(int rt,int l,int r){
	tree[rt].add=tree[rt].step=tree[rt].st=0;
	tree[rt].all=-1;
	tree[rt].l=l;tree[rt].r=r;
	if(l==r){
		tree[rt].c=lock[l];
		tree[rt].all=lock[l]-'A';
		return;
	}
	int mid = tree[rt].mid();
	buildtree(lch(rt),l,mid);
	buildtree(rch(rt),mid+1,r);
}

void pushdown(int rt){
	int mid = tree[rt].mid();
	if(tree[rt].all!=-1){
		tree[lch(rt)].all=tree[rch(rt)].all=tree[rt].all;
		tree[lch(rt)].add=tree[rch(rt)].add=tree[rt].add;
		tree[lch(rt)].st=tree[rt].st;
		tree[rch(rt)].st=tree[rt].st+(mid+1-tree[rt].l)*tree[rt].step;
		tree[lch(rt)].step=tree[rch(rt)].step=tree[rt].step;
	}
	else{
		if(tree[rt].add){
			tree[lch(rt)].add+=tree[rt].add;
			tree[rch(rt)].add+=tree[rt].add;
		}
		if(tree[rt].st){
			tree[lch(rt)].st+=tree[rt].st;
			tree[rch(rt)].st+=tree[rt].st+(mid+1-tree[rt].l)*tree[rt].step;
			tree[lch(rt)].step+=tree[rt].step;
			tree[rch(rt)].step+=tree[rt].step;
		}
	}
	tree[rt].all=-1;
	tree[rt].add=tree[rt].step=tree[rt].st=0;
}


void updatetree(int rt,int l,int r,int chg,int flag){
	if(tree[rt].l==l&&tree[rt].r==r){
		if(flag==1){
			tree[rt].add=tree[rt].step=tree[rt].st=0;
			tree[rt].all=chg;
		}
		if(flag==2){
			tree[rt].add+=chg;
		}
		if(flag==4){
			tree[rt].st+=chg;
			tree[rt].step++;
		}
		return;
	}
	int mid = tree[rt].mid();
	pushdown(rt);
	if(r<=mid)
		updatetree(lch(rt),l,r,chg,flag);
	else if(l>mid)
		updatetree(rch(rt),l,r,chg,flag);
	else{
		updatetree(lch(rt),l,mid,chg,flag);
		if(flag==4)
			chg+=mid+1-l;
		updatetree(rch(rt),mid+1,r,chg,flag);
	}
}

void lastupdate(int rt){
	if(tree[rt].all!=-1){
		FOR(i,tree[rt].l,tree[rt].r){
			lock[i]='A'+(tree[rt].all+tree[rt].add+tree[rt].st+tree[rt].step*(i-tree[rt].l))%26; 
		}
		return;
	}
	pushdown(rt);
	lastupdate(lch(rt));
	lastupdate(rch(rt));
}

int main()    
{  
#ifdef _DEBUG_
	fstream fin("G:/1.txt");
#else
#define fin cin
#endif
	fin>>n>>m;
	fin>>lock;
	char cmd[5];
	int a,b;
	st = 0;
	buildtree(1,0,n-1);
	int chg,flag,sm,bi;
	FOR(i,1,m){
		fin>>cmd>>flag;
		if(flag==3){
			fin>>a;
			st=(st+a)%n;
			continue;
		}else{
			if(flag==1){
				fin>>a>>b>>cmd;
				chg=cmd[0]-'A';
			}
			else if(flag==2){
				fin>>a>>b>>chg;
			}
			else if(flag==4){
				fin>>a>>b;
				chg=1;
			}
			sm=(st+a-1)%n;
			bi=(st+b-1)%n;
			if(bi

你可能感兴趣的:(线段树,hihocoder)