吐槽

上午风浔凌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. hdu4784

    题意: 给了一个图 从1号节点走到N号节点,然后,每个地方有买卖盐的差价,然后求 到达N的最大价值,一旦走到N这个点就不能再走了,或者走到不能再别的世界走1和N这两个点,然后接下来 用一个 四维的数组 ...

  2. OpenCV-3.3.0测试

    安装包目录下/samples/cpp里是各种例程 其中example_cmake里CMakeLists.txt已写好,直接cmake,make就可以,example.cpp是一个调用笔记本摄像头并显示 ...

  3. redis 性能建议

    因为 RDB 文件只用作后备用途,建议只在 Slave 上持久化 RDB 文件,而且只要15分钟一次就够了,只保留 save 900 1 这条规则. 如果 Enable AOF,好处是在恶劣请看下也只 ...

  4. java集合类图

  5. 分布式系统ID生成方案

    自增ID 不错,可以限度抑制ID的大小.但需要有一个中心化的节点作为解决原子性问题.可以选用Redis,MySQL,Zookeeper.成本有点高. UUID 分布式,而且唯一!缺点是生产的ID太长. ...

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

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

  7. mybatis源码解析10---StatementHandler解析

    StatementHandler解析 接口的作用是statement处理器,位于mybatis包的org.apache.ibatis.executor.statement目录下,源码如下: packa ...

  8. RN与android原生开发混合后的环境报错问题

    RN与android原生开发混合后的环境报错问题 需要先安装nodejs$ yarn --version1.12.1更新当前版本yarn upgrade --latest安装 | Yarnhttps: ...

  9. 苹果企业版签名分发相关问题,蒲公英签名,fir.im分发,安装ipa设置信任

    苹果企业版签名分发相关问题,蒲公英签名,fir.im分发,安装ipa设置信任蒲公英 - 高效安全的内测应用发布.管理平台https://www.pgyer.com/app/signature分发版 2 ...

  10. linux 文件压缩与解压缩