题目链接: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. git宝典—应付日常工作使用足够的指北手册

    最近公司gitlab又迁移,一堆git的命令骚操作,然鹅git命令,感觉还是得复习下——其实,git现在界面操作工具蛮多,比如intellij 自带的git操作插件就不错,gitlab github ...

  2. mika的模板库

    二分图最大匹配: 1.匈牙利算法:注意dfs中是dfs(c[w[i]]),搜索的是与之匹配的点. #include<cstdio> #include<cstring> ],w[ ...

  3. lua tonumber

    [1]应用tonumber函数 local function test(telnum) , )) == ) -- 5000~5999公司预留号码 , ) == ' or isLen or isRese ...

  4. Hadoop HDFS 用java API 进行读写

    public class HdfsApp { public static FileSystem getFileSystem() throws Exception { Configuration con ...

  5. Cannot locate BeanDefinitionParser for element [scoped-proxy]

    指定使用 CGLIB 而不使用 JDK 生成代理对象:注意:此两个标签必须同时出现,不然会报:Cannot locate BeanDefinitionParser for element [scope ...

  6. ldap认证jupyter notebook

    虽然jupyter hub是支持ldap的,见ldapauthenticator: 但是登录成功后似乎要以登录用户名启动notebook,而登录用户在服务器上不存在,于是500了: 在服务器上通过pa ...

  7. 【Diary】期中考+春游记

    4.22-4.24 期中考 周一 早上 赶来三楼电教厅自习,疯狂复习历史.想到下午考语文历史两大文科,不由得有点慌吧. 突然,沙雕体委说了一句: 下午考政治,你不背诵吗 下午 语文感觉正常发挥. 政治 ...

  8. 使用 Nginx 内置 $http_user_agent 来区分( 电脑 pc、手机 mobile、平板 pad )端的内容访问

    location / { #pc端内容访问 set $flag "pc"; set $num 1; set $hua "${http_user_agent}"; ...

  9. ensp 单臂路由实验

    搭建如下拓扑 交换机: 创建vlan10 20 g0/0/1接口设置访问类型为access,加入到vlan10:g0/0/2接口设置访问类型为access,加入到vlan20 g0/0/3接口设置为t ...

  10. ORACLE——RMAN 参数解读

    查看默认rman配置参数 oracle用户下登录rman:rman target / 查看所有配置参数详情:show all show all的第一行:using target database co ...