当前位置:网站首页>Codeforces round 810 (Div. 2) d. rain (segment tree difference)
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
2022-07-29 04:05:00 【Snow_ raw】
Codeforces Round #810 (Div. 2) D. Rain ( Segment tree difference )
Preface : A problem that disgusted me for nearly a day , common 180 180 180 OK, knock repeatedly 3 3 3 Time , I do not know! d d d How long has it been b u g bug bug , Or is there too little line segment tree theme .
The question : Given a n n n , this n n n It will rain , Every rainy day has a x x x And a p p p , From x x x Centered , Rainfall decreases to the left and right , for example x = 3 x=3 x=3 、 p = 5 p=5 p=5 In the case of :
0 1 2 3 4 5 4 3 2 1 0
-2 -1 0 1 2 3 4 5 6 7 8
ask : Statistics will turn any day into a non rainy day , Whether the maximum rainfall in all places does not exceed m m m , No more than output 1 1 1 Otherwise output − 1 -1 −1
Ideas : First of all, it is obvious that this problem is differential , That is, the center of rainfall to the left and right sides with tolerance d = 1 d=1 d=1 Step by step , adopt Boring series ( Classical line segment tree difference ) We can know in O ( n l o g n ) O(nlogn) O(nlogn) Calculate this within the time complexity n n n When it rains, all rainfall centers Rainfall value This can be achieved through a segment tree . The remaining steps are enumeration 1 1 1 ~ n n n One day in without rain , Whether the maximum rainfall in all places is less than m m m , We open the second segment tree to maintain the three maximum values, namely h [ l ] 、 h [ l ] − v e c [ l ] 、 h [ l ] + v e c [ l ] h[l]、h[l]-vec[l]、h[l]+vec[l] h[l]、h[l]−vec[l]、h[l]+vec[l] The first h [ l ] h[l] h[l] It indicates the maximum rainfall in all regions in the case that the rainfall days are not involved in the current enumeration , Because the precipitation will not decrease . The other two are uphill and downhill Offset writing , Used to compare numbers on the same slash at the same level , Get the maximum precipitation in the interval .
Code :
/* * @author: Snow * @Description: Algorithm Contest * @LastEditTime: 2022-07-27 10:51:20 */
#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(3)
#define re register int
typedef pair<int,int>PII;
#define pb emplace_back
#define debug(a) cout<<a<<' ';
#define fer(i,a,b) for(re i=a;i<=b;i++)
#define der(i,a,b) for(re i=a;i>=b;i--)
int n,m;
const int N = 2e5+10;
int x[N],p[N];
vector<int>vec2;
int vec[N*4];
int h[N*4];
char ans[N];
struct Seg_tree1{
struct Node{
int l,r,k,d;
}tr[N*16];
void build(int u,int l,int r){
if(l==r){
tr[u]={
l,r,0,0};
}
else{
int mid=l+r>>1;
tr[u]={
l,r};
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
}
}
void pushdown(int u){
if(tr[u].k||tr[u].d){
int mid=tr[u].l+tr[u].r>>1;
tr[u<<1].k+=tr[u].k;
tr[u<<1|1].k+=tr[u].k+(vec[tr[u<<1|1].l]-vec[tr[u].l])*tr[u].d;
tr[u<<1].d+=tr[u].d;
tr[u<<1|1].d+=tr[u].d;
tr[u].d=tr[u].k=0;
}
}
void modify(int u,int l,int r,int k,int d){
if(tr[u].l>=l&&tr[u].r<=r){
tr[u].k+=k+(vec[tr[u].l]-vec[l])*d;
tr[u].d+=d;
}
else{
pushdown(u);
int mid=tr[u].l+tr[u].r>>1;
if(l<=mid)modify(u<<1,l,r,k,d);
if(r>mid)modify(u<<1|1,l,r,k,d);
}
}
void floor(int u){
if(tr[u].l==tr[u].r){
h[tr[u].l]=tr[u].k;
}
else{
pushdown(u);
floor(u<<1);
floor(u<<1|1);
}
}
}seg1;
struct Seg_tree2{
struct Node{
int l,r,v1,v2,v3;
}tr[N*16];
void pushup(int u){
tr[u].v1=max(tr[u<<1].v1,tr[u<<1|1].v1);
tr[u].v2=max(tr[u<<1].v2,tr[u<<1|1].v2);
tr[u].v3=max(tr[u<<1].v3,tr[u<<1|1].v3);
}
void build(int u,int l,int r){
if(l==r){
tr[u]={
l,r,h[l],h[l]-vec[l],h[l]+vec[l]};
}
else{
tr[u]={
l,r};
int mid=l+r>>1;
build(u<<1,l,mid);
build(u<<1|1,mid+1,r);
pushup(u);
}
}
Node query(int u,int l,int r){
if(tr[u].l>=l&&tr[u].r<=r){
return tr[u];
}
int mid=tr[u].l+tr[u].r>>1;
if(r<=mid)return query(u<<1,l,r);
if(l>mid)return query(u<<1|1,l,r);
Node res;
Node left=query(u<<1,l,r);
Node right=query(u<<1|1,l,r);
res.v1=max(left.v1,right.v1);
res.v2=max(left.v2,right.v2);
res.v3=max(left.v3,right.v3);
return res;
}
}seg2;
void cf(){
vec2.clear();
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>x[i]>>p[i];
for(int i=1;i<=n;i++){
vec2.push_back(x[i]);
vec2.push_back(x[i]-p[i]+1);
vec2.push_back(x[i]+1);
vec2.push_back(x[i]+p[i]-1);
}
sort(vec2.begin(),vec2.end());
vec2.erase(unique(vec2.begin(),vec2.end()),vec2.end());
for(int i=1;i<=vec2.size();i++)vec[i]=vec2[i-1];
int len=vec2.size();
seg1.build(1,1,len);
for(int i=1;i<=n;i++){
int l=lower_bound(vec+1,vec+1+len,x[i]-p[i]+1)-vec;
int r=lower_bound(vec+1,vec+1+len,x[i])-vec;
if(l<=r){
seg1.modify(1,l,r,1,1);
}
l=lower_bound(vec+1,vec+1+len,x[i]+1)-vec;
r=lower_bound(vec+1,vec+1+len,x[i]+p[i]-1)-vec;
if(l<=r){
seg1.modify(1,l,r,p[i]-1,-1);
}
}
seg1.floor(1);
seg2.build(1,1,len);
fer(i,1,n){
int maxv=0;
int l=lower_bound(vec+1,vec+1+len,x[i]-p[i]+1)-vec;
int r=lower_bound(vec+1,vec+1+len,x[i])-vec;
if(l>1){
int t=seg2.query(1,1,l-1).v1;
maxv=max(maxv,t);
}
if(l<=r){
int t=seg2.query(1,l,r).v2;
maxv=max(maxv,t+vec[l]-1);
}
l=lower_bound(vec+1,vec+1+len,x[i]+1)-vec;
r=lower_bound(vec+1,vec+1+len,x[i]+p[i]-1)-vec;
if(r+1<=len){
int t=seg2.query(1,r+1,len).v1;
maxv=max(maxv,t);
}
if(l<=r){
int t=seg2.query(1,l,r).v3;
maxv=max(maxv,t-vec[r]-1);
}
ans[i]=maxv<=m?'1':'0';
}
fer(i,1,n)cout<<ans[i];
cout<<endl;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int _=1;
cin>>_;
while(_--){
cf();
}
return 0;
}
边栏推荐
- SQL window function
- 基于STM32和阿里云的环境检测系统设计
- The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
- Configmap配置与Secret加密
- Object array merges elements according to a field
- Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
- Lucifer 98 life record ing
- Solve the delay in opening the console of Google browser
- 【C语言入门】ZZULIOJ 1031-1035
- opengauss预检查安装
猜你喜欢

Ssl== certificate related concepts

华为天才少年稚晖君做了一把模块化机械键盘,引起极客圈地震,网友:这才是真正的客制化...

SSL==证书相关概念

Alibaba Font Icon Library Usage and update methods

Configmap配置与Secret加密

店铺排名问题,如何解决?

EMD empirical mode decomposition

The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!

Mmdetection preliminary use

Object array merges elements according to a field
随机推荐
C language - character array - string array - '\0' -sizeof-strlen() -printf()
Deep understanding of browser caching mechanism (HTTP)
Data mining -- code implementation of association analysis example (Part 2)
HCIP BGP
The data type of symbol, a new feature of ES6
The return value of the function is the attention of the pointer, the local variables inside the static limit sub function, and how the pointer to the array represents the array elements
路由 知识
Data too long for column 'xxx' at row 1 solution
第一个ALV程序2
The structure pointer must be initialized, and the pointer must also be initialized
The digitalization of the consumer industry is upgraded to "rigid demand", and weiit's new retail SaaS empowers enterprises!
2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?
I. creation and constraint of MySQL table
Pointer of pointer???...
Flask framework operation database_ Add, delete, modify and query statements
C declaration and initialization and assignment
Typescript from getting started to mastering (XXIII) namespace namespace (Part 2)
有没有大佬帮我看下flink sql连接kafka认证kerberos的参数配置是否有误