吐槽

上午风浔凌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. mysql主从配置,读写分离

    Mysql主从配置,实现读写分离 大型网站为了软解大量的并发访问,除了在网站实现分布式负载均衡,远远不够.到了数据业务层.数据访问层,如果还是传统的数据结构,或者只是单单靠一台服务器扛,如此多的数据库 ...

  2. Robot Framework 教程 (3) - Resource及关键字 的使用

    From:http://www.cnblogs.com/buaawp/p/4754399.html Robot Framework 教程 (3) - Resource及关键字 的使用 在进行软件自动化 ...

  3. codeforces 980B Marlin

    题意: 有一个城市有4行n列,n是奇数,有一个村庄在(1,1),村民的活动地点是(4,n): 有一个村庄在(4,1),村民的活动地点是(1,n): 现在要修建k个宾馆,不能修建在边界上,问能否给出一种 ...

  4. arc 092C 2D Plane 2N Points

    题意: 有n个红色的点和n个蓝色的点,如果红色的点的横坐标和纵坐标分别比蓝色的点的横坐标和纵坐标小,那么这两个点就可以成为一对友好的点. 问最多可以形成多少对友好的点. 思路: 裸的二分图匹配,对于满 ...

  5. Qt 添加 QtNetwork 库文件

    Qt应用程序默认没有加QtNetwork库.如下图: 在开发过程中,因处理业务需要手动添加QtNetwork库.根据常见情况分为以下两种: [1]若使用QTCreator开发程序 在工程的pro文件中 ...

  6. tensorflow学习6

    g_w1 = tf.get_variable('g_w1', [z_dim, 3136], dtype=tf.float32, initializer=tf.truncated_normal_init ...

  7. 证券化代币的时代已经到来,STO将引爆区块链经济

    STOs 似乎会在 2019 年取代 ICOs,即使不是完全取代,但置换的比例也会相当大.所有在美上市的公司都将按照 SEC 制定的相关规定进行交易.Vellum Capital 的 CEO 兼管理合 ...

  8. 用Javascript,DHTML控制表格的某一列的显示与隐藏

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.or ...

  9. 大数据学习路线:Zookeeper集群管理与选举

    大数据技术的学习,逐渐成为很多程序员的必修课,因为趋势也是因为自己的职业生涯.在各个技术社区分享交流成为很多人学习的方式,今天很荣幸给我们分享一些大数据基础知识,大家可以一起学习! 1.集群机器监控 ...

  10. Markdown使用笔记

    下载地址:http://markdownpad.com/ 简明版 Markdown 语法说明(简体中文版) 完整版 Markdown 语法说明(简体中文版) 官方文档:http://www.markd ...