题目链接:http://codeforces.com/contest/1154/problem/E

题意:两个人轮流取最大值与旁边k个数,问最后这所有的数分别被谁给取走了

分析:看这道题一点思路都没有,想不到模拟链表也该能想到线段树的啊

大部分AC代码是模拟链表的,写起来也更快,但我线段树很不熟,线段树的代码也写一份吧

另外,以后要养成一种习惯,除了主函数定义int main里有int外,其他地方统一用ll了,不要自己给自己挖坑。。。。。

线段树:

意识到是线段树后我建树部分就拿不准怎么做,事实上可以一开始先输入在数组里,建树部分代码稍微改动一下就好

代码本身不难,就着注释读一读很明显,要注意的部分我也提到了

这次的模板我是用的CF大佬的,感觉很好用

然后快读和快写效果十分显著,直接从102ms变成了62ms

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=<<;
const int maxn=*1e5+;
const double pi=acos(-);
const int mod=1e9+;
int a[maxn],ans[maxn],pre[maxn],nxt[maxn],pos[maxn];
struct node
{
int l,r,sum,lazy;
}tree[*maxn];
inline ll read(){
ll x=,tmp=;
char ch=getchar();
while(!isdigit(ch)){
if(ch=='-') tmp=-;
ch=getchar();
}
while(isdigit(ch)){
x=(x<<)+(x<<)+ch-;
ch=getchar();
}
return tmp*x;
} inline void write(ll x){
ll y=,len=;
while(y<=x){
y=(y<<)+(y<<);
len++;
}
while(len--){
y/=;
putchar(x/y+);
x%=y;
}
}
inline void pushdown(ll p){
if(tree[p].lazy){
tree[p<<].sum=; tree[p<<].lazy=;
tree[p<<|].sum=; tree[p<<|].lazy=;
tree[p].lazy=;
}
} void build(ll p,ll l,ll r){
tree[p].l=l; tree[p].r=r;
if(l==r){
tree[p].sum=a[l];
return;
}
ll mid=(l+r)>>;
build(p<<,l,mid);
build(p<<|,mid+,r);
tree[p].sum=max(tree[p<<].sum,tree[p<<|].sum);
}
void update(ll p,ll l,ll r){
if(l<=tree[p].l&&tree[p].r<=r){
tree[p].sum=; tree[p].lazy=;
return;
}
pushdown(p);
ll mid=(tree[p].l+tree[p].r)>>;
if(l<=mid) update(p<<,l,r);
if(r>mid) update(p<<|,l,r);
tree[p].sum=max(tree[p<<].sum,tree[p<<|].sum);
}
ll query(ll p,ll l,ll r){
if(l<=tree[p].l&&tree[p].r<=r) return tree[p].sum;
pushdown(p);
ll mid=(tree[p].l+tree[p].r)>>,ans=;
if(l<=mid) ans=max(ans,query(p<<,l,r));
if(r>mid) ans=max(ans,query(p<<|,l,r));
tree[p].sum=max(tree[p<<].sum,tree[p<<|].sum);
return ans;
}
int main(){
ll n,k;scanf("%I64d%I64d",&n,&k);
nxt[]=,pre[n+]=n;//这点要注意别忘了
for(ll i=;i<=n;i++){
a[i]=read();
pos[a[i]]=i;
pre[i]=i-;
nxt[i]=i+;//咋一看似乎没必要,但取走操作之后可能第5个人的前一个人就不是第四个人而是第二个人了
}
build(,,n);
ll now=;//这个now是第几组的意思,弄成0,1方便异或更改
while(){
ll x=query(,,n);
if(x<=) break;
ans[pos[x]]=now;
ll l=pos[x],r=pos[x];
for(ll i=;i<=k;i++){
if(pre[l]==) break;
else {
l=pre[l];
ans[l]=now;
}
}
for(ll i=;i<=k;i++){
if(nxt[r]==n+) break;
else{
r=nxt[r];
ans[r]=now;
}
}
update(,l,r);
pre[nxt[r]]=pre[l];
nxt[pre[l]]=nxt[r];//这个更新很灵性吧,就是把最右边的前一位更改为最左边的前一位,最左边的后一位
//更改为最右边的后一位,也就是把中间被选走的数的影响给剔除,也是nxt和pre数组的意义所在
now^=;
}
for(int i=;i<=n;i++)write(ans[i]+);
putchar('\n');
return ;
}

模拟链表:

就是用两个优先队列来模拟链表,优先队列内的数是按顺序从大到小存好的,刚好满足要求

看懂了线段树看这个肯定没有一点问题:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int inf=<<;
const int maxn=*1e5+;
const double pi=acos(-);
const int mod=1e9+;
typedef pair<int,int> P;
int pre[maxn],nxt[maxn],a[maxn],ans[maxn];
priority_queue<P> p,q;
int main(){
int n,k;scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=i-,nxt[i]=i+;
q.push(P(a[i],i));
}
int opt=;
while(!q.empty()){
while(p.size()&&q.top()==p.top()) q.pop(),p.pop();
if(q.empty()) break;
int m,i,j;
//求前缀
for(i=pre[q.top().second],j=;j<=k&&i;j++,i=pre[i]){
p.push(P(a[i],i));
ans[i]=opt+;
}
for(m=nxt[q.top().second],j=;j<=k&&m;j++,m=nxt[m]){
p.push(P(a[m],m));
ans[m]=opt+;
}
ans[q.top().second]=opt+;q.pop();
opt^=;
nxt[i]=m,pre[m]=i;
}
for(int i=;i<=n;i++)cout<<ans[i];
cout<<endl;
return ;
}

CF 552(div 3) E Two Teams 线段树,模拟链表的更多相关文章

  1. CF #296 (Div. 1) A. Glass Carving 线段树

    A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. 【CF280D】 k-Maximum Subsequence Sum ,线段树模拟费用流

    昨天考试被教育了一波.为了学习一下\(T3\)的科技,我就找到了这个远古时期的\(cf\)题(虽然最后\(T3\)还是不会写吧\(QAQ\)) 顾名思义,这个题目其实可以建成一个费用流的模型.我们用流 ...

  3. hdu_5818_Joint Stacks(线段树模拟)

    题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度 ...

  4. 2019牛客暑期多校训练营(第八场)E:Explorer(LCT裸题 也可用线段树模拟并查集维护连通性)

    题意:给定N,M,然后给出M组信息(u,v,l,r),表示u到v有[l,r]范围的通行证有效.问有多少种通行证可以使得1和N连通. 思路:和bzoj魔法森林有点像,LCT维护最小生成树.  开始和队友 ...

  5. CF 666E Forensic Examination 【SAM 倍增 线段树合并】

    CF 666E Forensic Examination 题意: 给出一个串\(s\)和\(n\)个串\(t_i\),\(q\)次询问,每次询问串\(s\)的子串\(s[p_l:p_r]\)在串\(t ...

  6. Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并

    D. Developing Game   Pavel is going to make a game of his dream. However, he knows that he can't mak ...

  7. Codeforces Round #275 Div.1 B Interesting Array --线段树

    题意: 构造一个序列,满足m个形如:[l,r,c] 的条件. [l,r,c]表示[l,r]中的元素按位与(&)的和为c. 解法: 线段树维护,sum[rt]表示要满足到现在为止的条件时该子树的 ...

  8. CF 197 DIV2 Xenia and Bit Operations 线段树

    线段树!!1A 代码如下: #include<iostream> #include<cstdio> #define lson i<<1 #define rson i ...

  9. Codeforces Round #244 (Div. 2) B. Prison Transfer 线段树rmq

    B. Prison Transfer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/pro ...

随机推荐

  1. platform怎么实现数据数据和驱动分离

    一些重要的结构体: struct platform_device { const char * name; int id; struct device dev; u32 num_resources; ...

  2. Xamarin.Forms + Prism,整理页面导航跳转流程

    3个Page,Page1 -> Page2 -> Page3 -> Page2 -> Page1. PageViewModel实现接口:INavigatingAware, IN ...

  3. 如何在linux上查看tomcat的端口号

    1.先到tomcat配置文件查看tomcat的端口是什么,配置文件一般是:$CATALINA_HOME/conf/server这个文件,查找<Connector port="8080& ...

  4. Caused by: java.lang.NoSuchMethodError: org.apache.poi.hssf.usermodel.HSSFCell.setEncoding(S)V

    java.lang.reflect.InvocationTargetException.  Coused by : java.lang.NoSuchMethodError:这个异常是找不到方法,但是如 ...

  5. 使用xshell从远程服务器下载文件到本地

    XSHELL工具上传文件到Linux以及下载文件到本地(Windows) Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的L ...

  6. Android界面跳转几种情况

    Android界面简单跳转, Intent intent =new Intent(MainActivity.this,SecondActivity.class); startActivity(inte ...

  7. 第十五节 JS面向对象实例及高级

    实例:面向对象的选项卡 把面向过程的程序,改写成面向对象的形式 原则:不能有函数套函数,但可以有全局变量 过程: onload —— 改写成 构造函数,其中window.onload的功能是在页面加载 ...

  8. Got error -1 from storage engine

    有个小朋友修复从库,但是start slave 后,报错信息如下 Could not execute Write_rows event on table hsfdssdb.mf_textannounc ...

  9. Python Iterables Iterators Generators

    container 某些对象包含其它对象的引用,这个包含其它对象引用的对象叫容器.例如list可以包含int对象,或者由其它数据类型(或数据结构)的对象组成一个list. 对其他对象的引用是容器值的一 ...

  10. 算法(第四版)C# 习题题解——2.2

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更为方便的版本见:http ...