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 ...
随机推荐
- EA强大功能之代码凝视
前面讲了EA怎样方便我们生成代码,这次讲一下,怎样生成具体的凝视. 1.文件表头凝视 (1)点击工具----选项 在常规项里改动作者: 在代码project中改动代码project的默认语言. (2) ...
- cocos2dx游戏开发学习笔记3-lua面向对象分析
在lua中,能够通过元表来实现类.对象.继承等.与元表相关的方法有setmetatable().__index.getmetatable().__newindex. 详细什么是元表在这里就不细说了,网 ...
- Linux 核心阅读工具vim+ctags+cscope+taglist
今天.介绍vim+ctags+cscope+taglist的内核阅读配置. 当使用过之后,我相信大部分人都会舍弃之前的Eclipse(我就是活生生的一个样例).我们先来看看实现的界面是怎么样的: 我们 ...
- Ctrl-A全选
Ctrl-A全选这点事(C#,WinForm) 所有的文本框,不管单行多行都Ctrl-A全选就好了吧?是啊,很方便.Windows的软件基本都是这样.可为什么我们自己制作的WinForm就默认不是 ...
- Codeforces 432D Prefixes and Suffixes(KMP+dp)
题目连接:Codeforces 432D Prefixes and Suffixes 题目大意:给出一个字符串,求全部既是前缀串又是后缀串的字符串出现了几次. 解题思路:依据性质能够依据KMP算法求出 ...
- 《深入理解OSGi:Equinox原理、应用与最佳实践》笔记_2_建立开发环境
本文对应书本5.1.3的内容 书本中通过CVS下载的源码 但是笔者实践的时候发现无法下载...地址已经失效了(也许是笔者的失误输错地址所致) 可以用git下载 地址是: http://git.ecli ...
- C++ Primer 学习笔记_79_模板与泛型编程 --模板编译模型
模板与泛型编程 --模板编译模型 引言: 当编译器看到模板定义的时候,它不马上产生代码.仅仅有在用到模板时,假设调用了函数模板或定义了模板的对象的时候,编译器才产生特定类型的模板实例. 一般而言,当调 ...
- 成为JAVA软件开发工程师要学哪些东西
2010-04-22 15:34 提问者采纳 Java EE(旧称j2ee) 第一阶段:Java基础,包括java语法,面向对象特征,常见API,集合框架: *第二阶段:java界面编程,包括AW ...
- 字符串匹配的KMP算法(转)
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- 关于SVN配置文件的一个小例子
1 背景假设 厦门央瞬公司是一家电子元器件设备供应商,其中有个ARM部门,专门负责ARM芯片的方案设计.销售,并在北京.上海各设立了一个办事处.对于工作日志,原先采用邮件方式发给经理,但是这种方式 ...