hdu4288 Coder(段树+分离)
主题链接:
题意:
。。
。由于时间很充足,须要对stl里面的函数有所了解。
。
。只是这个效率非常低。。
。(强哥的代码)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std; char s[5];
int n; vector<int>a; int main()
{
int len,val;
vector<int>::iterator iter;
while(cin>>n)
{
len=0;
a.clear();
while(n--)
{
scanf("%s",s);
if(s[0]=='s')
{
long long ans = 0;
for(int i=2; i < len ; i+=5)
ans += a[i];
cout<<ans<<endl;
}
else if(s[0]=='a')
{
len++;
scanf("%d",&val);
iter=lower_bound(a.begin(),a.end(),val);
a.insert(iter,val);
}
else
{
len--;
scanf("%d",&val);
iter= lower_bound(a.begin(),a.end(),val);
a.erase(iter); // basic coding
}
}
}
return 0;
}
。
最后由于没有告诉数据范围,所以要採取离散化,然后离线处理,最后得出全部要操作的总个数,然后依此建树。第一次用离散化,认为好高大上。。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<string>
#include<queue>
#define eps 1e-9
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn=100000+10;
int n,a[maxn],b[maxn];
char op[maxn][5]; struct Tree
{
int cnt;
ll sum[5];
}tree[maxn<<2]; void buildtree(int l,int r,int dex)
{
tree[dex].cnt=0;
memset(tree[dex].sum,0,sizeof(tree[dex].sum));
if(l==r) return;
int mid=(l+r)>>1;
buildtree(l,mid,dex<<1);
buildtree(mid+1,r,dex<<1|1);
} void push_up(int dex)
{
for(int i=0;i<5;i++)
tree[dex].sum[i]=tree[dex<<1].sum[i]+tree[dex<<1|1].sum[((i-tree[dex<<1].cnt)%5+5)%5];
} void update(int l,int r,int dex,int pos,int flag,int val)
{
tree[dex].cnt+=flag;
if(l==r)
{
if(flag==1)
tree[dex].sum[1]=val;
else
tree[dex].sum[1]=0;
return;
}
int mid=(l+r)>>1;
if(pos<=mid) update(l,mid,dex<<1,pos,flag,val);
else update(mid+1,r,dex<<1|1,pos,flag,val);
push_up(dex);
} int main()
{
int tot,pos,flag;
while(~scanf("%d",&n))
{
tot=0;
for(int i=1;i<=n;i++)
{
scanf("%s",op[i]);
if(op[i][0]!='s')
{
scanf("%d",&b[i]);
a[tot++]=b[i];
}
}
sort(a,a+tot);
tot=unique(a,a+tot)-a;
if(tot==0) memset(tree[1].sum,0,sizeof(tree[1].sum));
else buildtree(1,tot,1);
for(int i=1;i<=n;i++)
{
pos=lower_bound(a,a+tot,b[i])-a;
pos++;
if(op[i][0]=='a')
{
flag=1;
update(1,tot,1,pos,flag,b[i]);
}
else if(op[i][0]=='d')
{
flag=-1;
update(1,tot,1,pos,flag,b[i]);
}
else
printf("%I64d\n",tree[1].sum[3]);
}
}
return 0;
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
hdu4288 Coder(段树+分离)的更多相关文章
- HDU4288 Coder(线段树)
注意添加到集合中的数是升序的,先将数据读入,再离散化. sum[rt][i]表示此节点的区域位置对5取模为i的数的和,删除一个数则右边的数循环左移一位,添加一个数则右边数循环右移一位,相当于循环左移4 ...
- hdu-4288 Coder---线段树+离线处理&离散化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4288 题目大意: 维护一个有序数列{An},有三种操作: 1.添加一个元素. 2.删除一个元素. 3 ...
- POJ 2528 QAQ段树+分离
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submitcid=58236#statu ...
- BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询
3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...
- ZOJ 1610 间隔染色段树
要长8000仪表板.间染色的范围,问:最后,能看到的颜色,而且颜色一共有段出现 覆盖段 数据对比水 水可太暴力 段树: #include "stdio.h" #include ...
- HDU 1394 Minimum Inversion Number (数据结构-段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- PKU A Simple Problem with Integers (段树更新间隔总和)
意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...
- BZOJ 2588 Count on a tree (COT) 是持久的段树
标题效果:两棵树之间的首次查询k大点的权利. 思维:树木覆盖树,事实上,它是正常的树木覆盖了持久段树. 由于使用权值段树可以寻求区间k大,然后应用到持久段树思想,间隔可以做减法.详见代码. CODE: ...
- lintcode-439-线段树的构造 II
439-线段树的构造 II 线段树是一棵二叉树,他的每个节点包含了两个额外的属性start和end用于表示该节点所代表的区间.start和end都是整数,并按照如下的方式赋值: 根节点的 start ...
随机推荐
- Samba & Nginx - Resource temporarily unavailable
先说说本人的开发环境:Win7 + Editplus + VMware(Centos+Samba+Nginx).用Samba在Centos上把web文件夹(如www)共享,然后在Win7上訪问这个文件 ...
- VSTO学习笔记(三) 开发Office 2010 64位COM加载项
原文:VSTO学习笔记(三) 开发Office 2010 64位COM加载项 一.加载项简介 Office提供了多种用于扩展Office应用程序功能的模式,常见的有: 1.Office 自动化程序(A ...
- PV(访问量)、UV(独立访客)、IP(独立IP) (转)
网站统计中的PV(访问量):UV(独立访客):IP(独立IP)的定义与区别今天使用了雅虎统计,看到里面就有这个,就说说,其实里面的uv大家可能觉得很新奇,但是和站长统计里的独立访客是一样的嘛.---- ...
- Beijing seminar: China shadow banking
Beijing seminar: China shadow banking-张化桥-财新博客-新世纪的常识传播者-财新网 Beijing seminar: China shadow banking
- KISSY 库 demo
KISSY 是由淘宝前端project师们发起创建的一个开源 JS 类库. 它遵循的原则是 小巧灵活.简洁有用.愉悦编码.快乐开发. DEMO: <!DOCTYPE html> <h ...
- Spring Data Redis—Pub/Sub(附Web项目源码) (转)
一.发布和订阅机制 当一个客户端通过 PUBLISH 命令向订阅者发送信息的时候,我们称这个客户端为发布者(publisher). 而当一个客户端使用 SUBSCRIBE 或者 PSUBSCRIBE ...
- android maven eclipse里面新建mavenprojectThe desired archetype does not exist
这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...
- swift 新功能介绍
原文链接:http://www.cocoachina.com/applenews/devnews/2014/0617/8857.html 假设你和我一样,准备好好看看苹果的 Keynote,兴奋地准备 ...
- 双向绑定 TwoWay MVVM
1前台代码 <Grid> <StackPanel > <Grid x:Name="gridOne"> <Grid.Resources> ...
- HDU 2451 Simple Addition Expression(组合数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php?pid=2451 Problem Description A luxury yacht with 100 pass ...