吐槽

上午风浔凌julao问我的神题

操作又多又毒瘤又棘手。。。

然后bzoj题号正好是2333,2333333333

思路

貌似只有我是这么写的

线段树合并,

每个线段树存每个连通块的信息,维护点的值,然后并查集维护。。

然后内存被卡,手写一发内存池

然后TLE了,加上O2终于过了

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <stack>
using namespace std;
int fa[300010],Nodecnt,root[300010],n,q,alladd;
struct Node{
int lson,rson;
short maxx,tag;
}Seg[300000*25];
int pool[300000*20],top=0;
inline char Getchar() {
static char buf[(1<<16)], *p1 = buf, *p2 = buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,(1<<16),stdin),p1==p2)?EOF:*p1++;
}
inline int read(){
int x=0,f=1;
char ch=Getchar();
while(ch<'0'||ch>'9'){
if(ch=='-')
f=-1;
ch=Getchar();
}
while(ch>='0'&&ch<='9'){
x=(x<<1)+(x<<3)+(ch^48);
ch=Getchar();
}
return x*f;
}
int getnew(void){
if(top){
int t=pool[top];
top--;
Seg[t].lson=Seg[t].rson=Seg[t].maxx=Seg[t].tag=0;
return t;
}
return ++Nodecnt;
}
void throwin(int x){
pool[++top]=x;
}
int find(int x){
if(fa[x]==x)
return x;
else return fa[x]=find(fa[x]);
}
void pushup(int o){
Seg[o].maxx=max(Seg[Seg[o].lson].maxx,Seg[Seg[o].rson].maxx);
}
void pushdown(int o){
if(Seg[o].tag){
if(Seg[o].lson){
Seg[Seg[o].lson].tag+=Seg[o].tag;
Seg[Seg[o].lson].maxx+=Seg[o].tag;
}
if(Seg[o].rson){
Seg[Seg[o].rson].tag+=Seg[o].tag;
Seg[Seg[o].rson].maxx+=Seg[o].tag;
}
Seg[o].tag=0;
}
}
void merge(int x,int y,int l,int r,int &o){
pushdown(x);
pushdown(y);
if(x*y==0){
o=x+y;
return;
}
if(l==r){
o=getnew();
Seg[o].maxx=max(Seg[x].maxx,Seg[y].maxx);
throwin(x);
throwin(y);
return;
}
o=getnew();
Seg[o].maxx=max(Seg[x].maxx,Seg[y].maxx);
int mid=(l+r)>>1;
merge(Seg[x].lson,Seg[y].lson,l,mid,Seg[o].lson);
merge(Seg[x].rson,Seg[y].rson,mid+1,r,Seg[o].rson);
throwin(x);
throwin(y);
}
void add(int l,int r,int &o,int pos,int val){
// printf("l=%d r=%d o=%d pos=%d val=%d\n",l,r,o,pos,val);
if(!o)
o=getnew();
// printf("l=%d r=%d o=%d pos=%d val=%d\n",l,r,o,pos,val);
if(l==r){
Seg[o].maxx+=val;
return;
}
pushdown(o);
// printf("ok\n");
int mid=(l+r)>>1;
if(pos<=mid)
add(l,mid,Seg[o].lson,pos,val);
else
add(mid+1,r,Seg[o].rson,pos,val);
pushup(o);
}
void erase(int l,int r,int &o,int pos){
pushdown(o);
if(l==r){
throwin(o);
o=0;
return;
}
// printf("ok\n");
int mid=(l+r)>>1;
if(pos<=mid){
erase(l,mid,Seg[o].lson,pos);
if((!Seg[o].rson)&&(!Seg[o].lson)){
throwin(o);
o=0;
}
}
else{
erase(mid+1,r,Seg[o].rson,pos);
if((!Seg[o].rson)&&(!Seg[o].lson)){
throwin(o);
o=0;
}
}
pushup(o);
}
void debug(int l,int r,int o){
if(!o)
return;
printf("l=%d r=%d tag=%d o=%d max=%d\n",l,r,Seg[o].tag,o,Seg[o].maxx);
if(l!=r){
int mid=(l+r)>>1;
debug(l,mid,Seg[o].lson);
debug(mid+1,r,Seg[o].rson);
}
}
void uni(int x,int y){
if(find(x)!=find(y)){
erase(1,n,root[n+1],find(x));
erase(1,n,root[n+1],find(y));
int t=0;
merge(root[find(x)],root[find(y)],1,n,t);
root[find(y)]=t;
fa[find(x)]=find(y);
// debug(1,n,root[find(y)]);
add(1,n,root[n+1],find(y),Seg[root[find(y)]].maxx);
}
}
int query(int l,int r,int o,int pos){
if(l==r){
return Seg[o].maxx;
}
pushdown(o);
int mid=(l+r)>>1;
if(pos<=mid)
return query(l,mid,Seg[o].lson,pos);
else
return query(mid+1,r,Seg[o].rson,pos);
}
int main(){
freopen("8.in","r",stdin);
freopen("test.out","w",stdout);
// scanf("%d",&n);
n=read();
Seg[0].maxx=-0x3f3f3f3f;
for(int i=1;i<=n;i++)
fa[i]=i,root[i]=0;
for(int i=1;i<=n;i++){
int midx;
// scanf("%d",&midx);
midx=read();
add(1,n,root[i],i,midx);
add(1,n,root[n+1],i,midx);
}
// scanf("%d",&q);
q=read();
char opt[4];
for(int i=1;i<=q;i++){
opt[0]=Getchar();
while(opt[0]!='U'&&opt[0]!='A'&&opt[0]!='F')
opt[0]=Getchar();
if(opt[0]=='U'){
int x,y;
// scanf("%d %d",&x,&y);
x=read();
y=read();
uni(x,y);
continue;
}
opt[1]=Getchar();
while(opt[1]!='1'&&opt[1]!='2'&&opt[1]!='3')
opt[1]=Getchar();
if(opt[0]=='A'&&opt[1]=='1'){
int x=read(),v=read();
// scanf("%d %d",&x,&v);
erase(1,n,root[n+1],find(x));
add(1,n,root[find(x)],x,v);
add(1,n,root[n+1],find(x),Seg[root[find(x)]].maxx);
continue;
}
else if(opt[0]=='A'&&opt[1]=='2'){
int x=read(),v=read();
// scanf("%d %d",&x,&v);
add(1,n,root[n+1],find(x),v);
Seg[root[find(x)]].maxx+=v;
Seg[root[find(x)]].tag+=v;
pushdown(root[find(x)]);
continue;
}
else if(opt[0]=='A'&&opt[1]=='3'){
int v=read();
alladd+=v;
continue;
}
else if(opt[0]=='F'&&opt[1]=='1'){
int x=read();
printf("%d\n",query(1,n,root[find(x)],x)+alladd);
continue;
}
else if(opt[0]=='F'&&opt[1]=='2'){
int x=read();
printf("%d\n",Seg[root[find(x)]].maxx+alladd);
continue;
}
else{
printf("%d\n",Seg[root[n+1]].maxx+alladd);
continue;
}
}
return 0;
}

P3273 [SCOI2011]棘手的操作的更多相关文章

  1. 洛谷P3273 [SCOI2011] 棘手的操作 [左偏树]

    题目传送门 棘手的操作 题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边,连接第x个节点和第y个节点 A1 ...

  2. 洛谷P3273 [SCOI2011]棘手的操作

    题目描述 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作:U x y: 加一条边,连接第x个节点和第y个节点A1 x v: 将第x个节点的权 ...

  3. bzoj2333[SCOI2011]棘手的操作 洛谷P3273 [SCOI2011]棘手的操作

    2333? 先记一下吧,这题现在全部都是照着题解做的,因为怎么改都改不出来,只好对着题解改,以后还要再做过 以后再也不用指针了!太恶心了!空指针可不止直接特判那么简单啊,竟然还要因为空指针写奇怪的分类 ...

  4. Luogu P3273 [SCOI2011]棘手的操作(左偏树)

    什么恶心东西啊,又水又烦 两个可并堆维护即可 #include <cstdio> #include <iostream> #include <cstring> #i ...

  5. 【bzoj2333】 [SCOI2011]棘手的操作 可并堆+lazy标记

    2016-05-31  21:45:41 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2333 (学习了黄学长的代码 有如下操作: U x y ...

  6. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  7. 2333: [SCOI2011]棘手的操作[写不出来]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

  8. 2333: [SCOI2011]棘手的操作[离线线段树]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2325  Solved: 909[Submit][Stat ...

  9. 2333: [SCOI2011]棘手的操作[我不玩了]

    2333: [SCOI2011]棘手的操作 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1979  Solved: 772[Submit][Stat ...

随机推荐

  1. c++引用和指针的彻底理解

     ★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名.  ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指 ...

  2. 列表选择模式:单选、按shift、按shift或ctrl

    2018-10-29 21:23:16 开始写 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing ...

  3. 20155228 2016-2017-2 《Java程序设计》第2周学习总结

    20155228 2006-2007-2 <Java程序设计>第2周学习总结 教材学习内容总结 类型 Java可以区分为基本类型和类类型(或称参考类型).对于基本类型,使用时得考虑一下数据 ...

  4. Spark学习之路 (十四)SparkCore的调优之资源调优JVM的GC垃圾收集器

    一.概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本 ...

  5. Mini-Batch 、Momentum、Adam算法的实现

    Mini-Batch 1. 把训练集打乱,但是X和Y依旧是一一对应的 import numpy as np a = np.random.randn(3,3) print(a) b = list(np. ...

  6. (2018干货系列十)最新android开发学习路线整合

    怎么学Android Android是一个以Linux为基础的半开源操作系统,主要用于移动设备,由Google和开放手持设备联盟开发与领导.据2011年初数据显示仅正式上市两年的操作系统Android ...

  7. IE haslayout的属性及其值

    haslayout是IE 渲染引擎的一个内部组成部分.在IE 中,一个元素要么自己对自身的内容进行计算大小和组织,要么依赖于父元素来计算尺寸和组织内容.为了调节这两个不同的概念,渲染引擎采用了hasl ...

  8. 处理springmvc的post和get提交参数乱码问题

    1,post 配置CharacterEncodingFilter拦截器 2,get 在tomcat的Connect 上配置uri 编码

  9. fjwc2019 D4T1 循环流

    #187. 「2019冬令营提高组」循环流 假的网络流,其实是O(1)算法 手画n个图后,你会发现只要分成几种情况讨论讨论就得了. 当$a==1$时显然不存在. 当$a!=1$时 如果$n==2$,显 ...

  10. 【题解】 P2234 [HNOI2002]营业额统计

    平衡树板题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 这道题目还算比较模板的 每输入一个数,先不要插入 要求一下前驱和后继与x差的最小值并加到答案中 再把x插入平衡树 然后你 ...