首页 > 其他分享 >山海经&&Atcoder Alternating String (线段树)

山海经&&Atcoder Alternating String (线段树)

时间:2024-02-22 11:45:45浏览次数:36  
标签:lid Atcoder String int pushup && query rid rt

  • 前言:为什么把他们放在一起?因为我发现把pushup向上回溯放结构体类型函数里比较方便
    并且这两题确实也有相同思想

山海经

image
image

这题分三种情况

  1. 左子树前缀和+右子树前缀和
    2.右子树后缀和 与 右总区间+左子树
    3.左区间最大子段 与 右区间最大子段 与 左后缀与右前缀
  • 特别要注意的事项
    1.LONGLONG
    2.是否有等于号(因为要取最小的i,j注意相同情况取最小)
点击查看代码
#include<bits/stdc++.h>
#define lid (rt<<1)
#define rid (rt<<1|1)
using namespace std;
const int N=200010;
int m,n,k,p,a[N];
struct tree
{ int ls=-INT_MAX,rs=-INT_MAX,ms=-INT_MAX;
  int l,r,sum; 
  int ml=-INT_MAX,mr=-INT_MAX;
  int ll,rr;
} t[N<<2];
tree push_up(tree x,tree y)
{  
    tree z;
    z.l=x.l;
	z.r=y.r;
	z.sum=x.sum+y.sum;
	if(x.ls<x.sum+y.ls)
	{
		z.ls=x.sum+y.ls;
		z.rr=y.rr;
	}
	else {z.ls=x.ls;z.rr=x.rr;}
	
	if(y.rs<=y.sum+x.rs)
	{
		z.rs=y.sum+x.rs;
		z.ll=x.ll;
	}
	else 
	{
		z.rs=y.rs;
		z.ll=y.ll;	
	}
	z.ms=x.ms;
	z.ml=x.ml;
	z.mr=x.mr;
	if(x.rs+y.ls>z.ms)
	{
		z.ms=x.rs+y.ls;
		z.ml=x.ll;
		z.mr=y.rr;	
	}
	if(z.ms<y.ms)
	{
		z.ms=y.ms;
		z.ml=y.ml;
		z.mr=y.mr;			
	}
  return z; 
}
void bt(int rt,int l,int r)
{
	t[rt].l=l;t[rt].r=r;
	if(l==r)
	{
		t[rt].sum=a[l];
		t[rt].ml=t[rt].mr=t[rt].ll=t[rt].rr=l;
		t[rt].ls=t[rt].rs=t[rt].ms=a[l];
		return;
	}
	int mid=(t[rt].l+t[rt].r)>>1;
	bt(lid,l,mid);
	bt(rid,mid+1,r);
	t[rt]=push_up(t[lid],t[rid]);
}
tree query(int rt,int l,int r)
{
	if(l<=t[rt].l&&t[rt].r<=r)
	{
		return t[rt];
	}
	int mid=(t[rt].l+t[rt].r)>>1;
	if(r<=mid) return query(lid,l,r);
	if(l>mid) return query(rid,l,r);
	return push_up(query(lid,l,r),query(rid,l,r)); 	
}
int main()
{ 
//  freopen("hill.in","r",stdin);
//  freopen("hill.out","w",stdout);
  scanf("%d%d",&n,&m);
  for (int i=1;i<=n;i++)
  scanf("%d",&a[i]);
  bt(1,1,n);
//  for(int i=1;i<=30;i++)
//  {
//      cout<<i<<' '<<t[i].l<<' '<<t[i].r <<' '<<t[i].ms<<endl;	 
//  }
  for (int i=1;i<=m;i++)
  { 
  	int x,y;
    scanf("%d%d",&x,&y);
    tree tmp=query(1,x,y);
    printf("%d %d %d\n",tmp.ml,tmp.mr,tmp.ms);
  }
  return 0;
}

Alternating String

链接

这题只需要维护一下左端点值和右端点值即可
用异或和维护

点击查看代码
#include <bits/stdc++.h>
#define lid (rt<<1)
#define rid (rt<<1|1)
using namespace std;
int len,q;
const int N=500000;
int a[N+5];
struct tree
{
	int l,r,lz;bool y;
	int stw,enw;
}t[N<<2];
tree pushup(tree x,tree y)
{	
	tree z;
	z.l=x.l;z.r=y.r;
	z.stw=x.stw;z.enw=y.enw;
	if(x.y&&y.y&&x.enw!=y.stw)
	{
		z.y=1;
	}
	else z.y=0;	
	return z;
}
void pushdown(int rt)
{
	if(t[rt].lz)
	{
		int lz=t[rt].lz;
		t[rt].lz=0;
		t[lid].lz^=lz;
		t[rid].lz^=lz;
		t[lid].stw^=lz;t[lid].enw^=lz;
		t[rid].stw^=lz;t[rid].enw^=lz;
	}
}
void bt(int rt,int l,int r)
{
	t[rt].l=l;t[rt].r=r;
	if(l==r)
	{
//		t[rt].sum=a[l];
		t[rt].y=1;
		t[rt].stw=t[rt].enw=a[l];
		return;
	}
	int mid=(l+r)>>1;
	bt(lid,l,mid);
	bt(rid,mid+1,r);
	t[rt]=pushup(t[lid],t[rid]);
}
void update(int rt,int l,int r)
{
	if(l<=t[rt].l&&t[rt].r<=r)
	{
		t[rt].lz^=1;
		t[rt].stw^=1;t[rt].enw^=1;
		return;
	}
	pushdown(rt);
	int mid=(t[rt].l+t[rt].r)>>1;
	if(l<=mid)update(lid,l,r);
	if(r>mid)update(rid,l,r);
	t[rt]=pushup(t[lid],t[rid]);
}
tree query(int rt,int l,int r)
{
	if(l<=t[rt].l&&t[rt].r<=r)
	{
		return t[rt];
	}
	pushdown(rt);
	int mid=(t[rt].l+t[rt].r)>>1;
	int val=0;
	if(l<=mid&&r>mid)
	{
		return pushup(query(lid,l,r),query(rid,l,r));
	}
	else if(l<=mid)return query(lid,l,r);
	else if(r>mid)return query(rid,l,r);	
}
int main()
{
	scanf("%d%d",&len,&q);
	for(int i=1;i<=len;i++)
	{
		scanf("%1d",&a[i]);
	}
	int k=1,aa,b;
	bt(1,1,len);
	for(int i=1;i<=q;i++)
	{
		scanf("%d%d%d",&k,&aa,&b);
		if(k==1)
		{
			//fanzhuan
			update(1,aa,b);
		}else
		{
			if(query(1,aa,b).y)
			{
				cout<<"Yes"<<endl;
			}else 
			{
				cout<<"No"<<endl;
			}
		}
	}
	return 0;
}
/*
5 6
10100
2 1 3
2 1 5
1 1 4
2 1 5
1 3 3
2 2 4

*/

标签:lid,Atcoder,String,int,pushup,&&,query,rid,rt
From: https://www.cnblogs.com/wlesq/p/18026927

相关文章

  • vue中花括号表达式,string类型除以number类型返回NaN值
    bug:数据为0时,el-progress的color还是有颜色,应该是没有颜色的第一步解决:设置动态color<el-progress:show-text="false":percentage="(oilCarOccupationNum/totalNum)*100":color="oilCarOccupationNum?'......
  • 反射突破String不可变
    try{Stringstr="yyg";System.out.println("str="+str+",唯一性hash值="+System.identityHashCode(str));ClassstringClass=str.getClass();//获取String类中的value属性Fieldfield=stringClass.getDeclaredFiel......
  • C++ STL 容器-string类型
    C++STL第一部分-容器STL的介绍C++的STL分为六大部分容器分为String容器例子1std::stringstr1,str2,str3,str4;str1.assign("abcd");//给str1赋值abcdstr2.assign("abcd",3);//获取abcd中的3个,从0到2str3.assign(str1);//获取str1//注意str3()和str3.a......
  • [Rust] Char vs String
    usestd::path::PathBuf;useclap::Parser;#[derive(Parser,Debug)]#[clap()]pubstructOpts{pubargs:Vec<String>,#[clap(short='c',long="config")]pubconfig:Option<PathBuf>,#[clap(short='p'......
  • (ColumnTypes[number] & { editable?: boolean; dataIndex: string; })[]
    (ColumnTypes[number]&{editable?:boolean;dataIndex:string;})[]在TypeScript中,这段类型定义可以分解理解:ColumnTypes[number]:首先,如果ColumnTypes是一个数组类型(如Column[]),那么ColumnTypes[number]就是获取数组中的元素类型。在TypeScript中,number表示数组......
  • String常见面试题
    /***String类常见的面试题。*/publicclassStringExam{@Testpublicvoidtest1(){Strings1="abc";Strings2=newString("abc");System.out.println(s1==s2);//falseSystem.out.println(s1.equal......
  • Go 100 mistakes - #41: Substrings and memory leaks
        WeneedtokeeptwothingsinmindwhileusingthesubstringoperationinGo. First,theintervalprovidedisbasedonthenumberofbytes,notthenumberofrunes. Second,asubstringoperationmayleadtoamemoryleakastheresultings......
  • Go 100 mistakes - #39: Under-optimized string concatenation
           ......
  • Go 100 mistakes - #37: Inaccurate string iteration
           ......
  • Toyota Programming Contest 2024#2(AtCoder Beginner Contest 341)
    ToyotaProgrammingContest2024#2(AtCoderBeginnerContest341)A-Print341代码:#include<bits/stdc++.h>usingnamespacestd;usingll=longlong;usingpii=pair<ll,ll>;#definefifirst#definesesecondusingi128=__int128_t;usingp......