結果
| 問題 |
No.674 n連勤
|
| コンテスト | |
| ユーザー |
ikd
|
| 提出日時 | 2018-04-16 14:47:56 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
AC
|
| 実行時間 | 135 ms / 2,000 ms |
| コード長 | 2,668 bytes |
| コンパイル時間 | 1,112 ms |
| コンパイル使用メモリ | 109,772 KB |
| 実行使用メモリ | 11,932 KB |
| 最終ジャッジ日時 | 2024-06-13 00:29:04 |
| 合計ジャッジ時間 | 2,219 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 17 |
ソースコード
void main(){
import std.stdio, std.string, std.conv, std.algorithm;
long d; int q; rd(d, q);
import std.typecons;
alias Query=Tuple!(long, "a", long, "b");
auto queries=new Query[](q);
long[] vals;
foreach(i; 0..q){
long a, b; rd(a, b);
queries[i]=Query(a, b+1);
vals~=queries[i].a;
vals~=queries[i].b;
}
sort(vals);
int[long] map;
foreach(val; vals){
if((val in map)==null) map[val]=map.length.to!(int);
}
long[int] rev;
foreach(long k; map.keys){
rev[map[k]]=k;
}
auto tree=new SquareRootDecompotision(map.length.to!(int));
long mx=0;
foreach(query; queries){
int l=map[query.a], r=map[query.b];
tree.merge(l, r);
long ol=rev[tree.getLeft(l)], or=rev[tree.getRight(l)]; // mxが更新されるなら、いまのクエリの区間を含む(?)
mx=max(mx, or-ol);
writeln(mx);
}
}
class SquareRootDecompotision{
int D=1;
int[] st, ed;
int[] buc_st, buc_ed; // lazyみたいな感じ
import std.algorithm;
this(int n){
while(D*D<n) D++;
st.length=ed.length=(D*D);
buc_st.length=buc_ed.length=D;
foreach(int i; 0..(D*D)) st[i]=ed[i]=i; // [i, i) = \phi
fill(buc_st, -1);
fill(buc_ed, -1);
}
void merge(int left, int right){
int l=min(left, st[left]), r=max(right, ed[right]);
if(buc_st[left/D]>=0) chmin(l, buc_st[left/D]);
if(buc_st[right/D]>=0) chmax(r, buc_ed[right/D]);
if(left-1>=0){
if(st[left-1]<ed[left-1]) chmin(l, st[left-1]); // not \phi
if(buc_st[(left-1)/D]>=0) chmin(l, buc_st[(left-1)/D]);
}
int bl=l/D, br=r/D;
if(br-bl<=1){
eachFill(l, r, l, r);
}else{
eachFill(l, (bl+1)*D, l, r);
buc_st[(bl+1)..br]=l;
buc_ed[(bl+1)..br]=r;
eachFill(br*D, r, l, r);
}
}
void eachFill(int tl, int tr, int l, int r){ // j in [tl, tr) st[j]<-l, ed[j]<-r
push(tl/D); push(tr/D);
foreach(int j; tl..tr) st[j]=l, ed[j]=r;
}
void push(int k){
if(buc_st[k]>=0) st[k*D..(k+1)*D]=buc_st[k]; // たぶん、単調
if(buc_ed[k]>=0) ed[k*D..(k+1)*D]=buc_ed[k];
buc_st[k]=buc_ed[k]=-1;
}
int getLeft(int i){
int l=i;
if(buc_st[i/D]>=0){
l=min(l, buc_st[i/D]);
}else{
l=min(l, st[i]);
}
return l;
}
int getRight(int i){
int r=i;
if(buc_ed[i/D]>=0){
r=max(r, buc_ed[i/D]);
}else{
r=max(r, ed[i]);
}
return r;
}
}
void chmin(T)(ref T l, T r){if(l>r)l=r;}
void chmax(T)(ref T l, T r){if(l<r)l=r;}
void rd(Type...)(ref Type x){
import std.stdio, std.string, std.conv;
auto l=readln.split;
assert(l.length==x.length);
foreach(i, ref e; x){
e=l[i].to!(typeof(e));
}
}
ikd