답안 #1013691

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1013691 2024-07-03T20:47:41 Z ttamx Fish 2 (JOI22_fish2) C++17
100 / 100
1288 ms 190292 KB
#include<bits/stdc++.h>

using namespace std;

using ll = long long;

const int N=1e5+5;
const int K=1<<18;
const int INF=INT_MAX/2;
const ll LINF=LLONG_MAX/2;

int n,q;
ll a[N];

struct Fenwick{
    ll t[N];
    void update(int i,ll v){
        for(;i<N;i+=i&-i)t[i]+=v;
    }
    ll query(int i){
        ll res=0;
        for(;i>0;i-=i&-i)res+=t[i];
        return res;
    }
    ll query(int l,int r){
        return query(r)-query(l-1);
    }
}fw;

template<class T,class E,T (*tt)(T,T),E (*ee)(E,E),T (*te)(T,E),T (*tu)(),E (*eu)()>
struct LazySegTree{
    int n;
    T t[K];
    E lz[K];
    void apply(int i,E v){
        t[i]=te(t[i],v);
        lz[i]=ee(lz[i],v);
    }
    void push(int i){
        apply(i*2,lz[i]);
        apply(i*2+1,lz[i]);
        lz[i]=eu();
    }
    void pull(int i){
        t[i]=tt(t[i*2],t[i*2+1]);
    }
    template<class F>
    void build(int l,int r,int i,F f){
        if(l==r){
            t[i]=f();
            lz[i]=eu();
            return;
        }
        int m=(l+r)/2;
        build(l,m,i*2,f);
        build(m+1,r,i*2+1,f);
        pull(i);
    }
    template<class F>
    void build(int _n,F f){
        n=_n;
        build(1,n,1,f);
    }
    void build(int _n){
        build(_n,[](){return tu();});
    }
    void modify(int l,int r,int i,int x,T v){
        if(r<x||x<l)return;
        if(l==r)return void(t[i]=v);
        push(i);
        int m=(l+r)/2;
        modify(l,m,i*2,x,v);
        modify(m+1,r,i*2+1,x,v);
        pull(i);
    }
    void modify(int x,T v){
        modify(1,n,1,x,v);
    }
    void update(int l,int r,int i,int x,int y,E v){
        if(y<l||r<x)return;
        if(x<=l&&r<=y)return apply(i,v);
        push(i);
        int m=(l+r)/2;
        update(l,m,i*2,x,y,v);
        update(m+1,r,i*2+1,x,y,v);
        pull(i);
    }
    void update(int l,int r,E v){
        update(1,n,1,l,r,v);
    }
    T query(int l,int r,int i,int x,int y){
        if(y<l||r<x)return tu();
        if(x<=l&&r<=y)return t[i];
        push(i);
        int m=(l+r)/2;
        return tt(query(l,m,i*2,x,y),query(m+1,r,i*2+1,x,y));
    }
    T query(int l,int r){
        return query(1,n,1,l,r);
    }
    template<class F>
    int find_first(int l,int r,int i,int x,int y,F f){
        if(y<l||r<x||!f(t[i]))return n+1;
        if(l==r)return l;
        push(i);
        int m=(l+r)/2;
        int res=find_first(l,m,i*2,x,y,f);
        if(res!=n+1)return res;
        return find_first(m+1,r,i*2+1,x,y,f);
    }
    template<class F>
    int find_first(int l,int r,F f){
        return find_first(1,n,1,l,r,f);
    }
    template<class F>
    int find_last(int l,int r,int i,int x,int y,F f){
        if(y<l||r<x||!f(t[i]))return 0;
        if(l==r)return l;
        push(i);
        int m=(l+r)/2;
        int res=find_last(m+1,r,i*2+1,x,y,f);
        if(res!=0)return res;
        return find_last(l,m,i*2,x,y,f);
    }
    template<class F>
    int find_last(int l,int r,F f){
        return find_last(1,n,1,l,r,f);
    }
};

struct Info{
    int mn,cnt;
    Info():mn(INF),cnt(0){}
    Info(int _mn,int _cnt):mn(_mn),cnt(_cnt){}
};

namespace S1{
    Info merge_info(Info a,Info b){
        if(a.mn<b.mn)return a;
        if(b.mn<a.mn)return b;
        return Info(a.mn,a.cnt+b.cnt);
    }
    int merge_tag(int a,int b){return a+b;}
    Info apply(Info a,int b){return Info(a.mn+b,a.cnt);}
    Info unit_info(){return Info();}
    int unit_tag(){return 0;}
    LazySegTree<Info,int,merge_info,merge_tag,apply,unit_info,unit_tag> seg;
};
using S1::seg;

namespace S2{
    ll merge_info(ll a,ll b){return max(a,b);}
    ll merge_tag(ll a,ll b){return a+b;}
    ll apply(ll a,ll b){return a+b;}
    ll unit_info(){return -LINF;}
    ll unit_tag(){return 0;}
    LazySegTree<ll,ll,merge_info,merge_tag,apply,unit_info,unit_tag> seg_l,seg_r;
};
using S2::seg_l,S2::seg_r;

struct Interval{
    int l,r;
    Interval(){}
    Interval(int _l,int _r):l(_l),r(_r){}
    bool contains(int x){return l<=x&&x<=r;}
};

struct IntervalTree{
    stack<Interval> t[K];
    void update(int l,int r,int i,Interval v){
        int m=(l+r)/2;
        if(l==r||(v.contains(m)&&v.contains(m+1)))return void(t[i].emplace(v));
        if(v.r<=m)update(l,m,i*2,v);
        else update(m+1,r,i*2+1,v);
    }
    void update(Interval v){
        update(1,n,1,v);
    }
    void query(int l,int r,int i,int x,vector<Interval> &res){
        while(!t[i].empty()&&t[i].top().contains(x)){
            res.emplace_back(t[i].top());
            t[i].pop();
        }
        if(l==r)return;
        int m=(l+r)/2;
        if(x<=m)query(l,m,i*2,x,res);
        else query(m+1,r,i*2+1,x,res);
    }
    vector<Interval> query(int x){
        vector<Interval> res;
        query(1,n,1,x,res);
        return res;
    }
}ranges;

bool valid(int l,int r){
    if(l>r)return false;
    ll sum=fw.query(l,r);
    return sum<a[l-1]&&sum<a[r+1];
}

vector<Interval> find_ranges(int i){
    vector<Interval> res;
    vector<int> left{i+1},right{i-1};
    ll sum_l=fw.query(i),sum_r=fw.query(i-1);
    for(int l=i;l>=1;l=seg_l.find_last(1,l-1,[&](ll x){return x>sum_r;}))left.emplace_back(l);
    for(int r=i;r<=n;r=seg_r.find_first(r+1,n,[&](ll x){return x>-sum_l;}))right.emplace_back(r);
    for(int l:left)for(int r:right)if(valid(l,r))res.emplace_back(l-1,r+1);
    return res;
}

void update(int i,int v){
    int dif=v-a[i];
    a[i]=v;
    for(auto [l,r]:ranges.query(i))seg.update(l+1,r-1,-1);
    fw.update(i,dif);
    seg_l.update(i+1,n,dif);
    seg_r.update(i,n,-dif);
    seg_l.modify(i+1,a[i]+fw.query(i));
    seg_r.modify(i-1,a[i]-fw.query(i-1));
    for(auto [l,r]:find_ranges(i)){
        seg.update(l+1,r-1,1);
        ranges.update(Interval(l,r));
    }
}

void init(){
    for(int i=1;i<=n;i++){
        a[i]=1;
        fw.update(i,1);
    }
    a[0]=a[n+1]=LINF;
    seg.build(n,[]{return Info(1,1);});
    seg_l.build(n);
    seg_r.build(n);
    ranges.update(Interval(0,n+1));
    for(int i=1;i<=n;i++){
        seg_l.modify(i,a[i-1]+i-1);
        seg_r.modify(i,a[i+1]-i);
    }
}

int main(){
    cin.tie(nullptr)->sync_with_stdio(false);
    cin >> n;
    init();
    for(int i=1;i<=n;i++){
        int x;
        cin >> x;
        update(i,x);
    }
    cin >> q;
    while(q--){
        int t;
        cin >> t;
        if(t==1){
            int x,y;
            cin >> x >> y;
            update(x,y);
        }else{
            int l,r;
            cin >> l >> r;
            ll sum_l=fw.query(l-1),sum_r=fw.query(r);
            int nl=max(l,seg_r.find_last(l,r-1,[&](ll x){return x>-sum_l;})+1);
            int nr=min(r,seg_l.find_first(l+1,r,[&](ll x){return x>sum_r;})-1);
            cout << (nl<=nr?seg.query(nl,nr).cnt:0) << "\n";
        }
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 181080 KB Output is correct
2 Correct 71 ms 181072 KB Output is correct
3 Correct 70 ms 180876 KB Output is correct
4 Correct 68 ms 181020 KB Output is correct
5 Correct 75 ms 181032 KB Output is correct
6 Correct 73 ms 181116 KB Output is correct
7 Correct 74 ms 181072 KB Output is correct
8 Correct 74 ms 181044 KB Output is correct
9 Correct 78 ms 180940 KB Output is correct
10 Correct 74 ms 180976 KB Output is correct
11 Correct 75 ms 181076 KB Output is correct
12 Correct 74 ms 181076 KB Output is correct
13 Correct 68 ms 180948 KB Output is correct
14 Correct 78 ms 181072 KB Output is correct
15 Correct 75 ms 181076 KB Output is correct
16 Correct 90 ms 181036 KB Output is correct
17 Correct 101 ms 181076 KB Output is correct
18 Correct 74 ms 181076 KB Output is correct
19 Correct 70 ms 181072 KB Output is correct
20 Correct 76 ms 181120 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 181072 KB Output is correct
2 Correct 330 ms 189524 KB Output is correct
3 Correct 381 ms 189520 KB Output is correct
4 Correct 301 ms 189600 KB Output is correct
5 Correct 388 ms 189556 KB Output is correct
6 Correct 325 ms 189524 KB Output is correct
7 Correct 393 ms 189520 KB Output is correct
8 Correct 296 ms 189520 KB Output is correct
9 Correct 362 ms 189524 KB Output is correct
10 Correct 608 ms 189704 KB Output is correct
11 Correct 585 ms 189520 KB Output is correct
12 Correct 327 ms 189520 KB Output is correct
13 Correct 369 ms 189524 KB Output is correct
14 Correct 316 ms 189752 KB Output is correct
15 Correct 322 ms 189772 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 181080 KB Output is correct
2 Correct 71 ms 181072 KB Output is correct
3 Correct 70 ms 180876 KB Output is correct
4 Correct 68 ms 181020 KB Output is correct
5 Correct 75 ms 181032 KB Output is correct
6 Correct 73 ms 181116 KB Output is correct
7 Correct 74 ms 181072 KB Output is correct
8 Correct 74 ms 181044 KB Output is correct
9 Correct 78 ms 180940 KB Output is correct
10 Correct 74 ms 180976 KB Output is correct
11 Correct 75 ms 181076 KB Output is correct
12 Correct 74 ms 181076 KB Output is correct
13 Correct 68 ms 180948 KB Output is correct
14 Correct 78 ms 181072 KB Output is correct
15 Correct 75 ms 181076 KB Output is correct
16 Correct 90 ms 181036 KB Output is correct
17 Correct 101 ms 181076 KB Output is correct
18 Correct 74 ms 181076 KB Output is correct
19 Correct 70 ms 181072 KB Output is correct
20 Correct 76 ms 181120 KB Output is correct
21 Correct 64 ms 181072 KB Output is correct
22 Correct 330 ms 189524 KB Output is correct
23 Correct 381 ms 189520 KB Output is correct
24 Correct 301 ms 189600 KB Output is correct
25 Correct 388 ms 189556 KB Output is correct
26 Correct 325 ms 189524 KB Output is correct
27 Correct 393 ms 189520 KB Output is correct
28 Correct 296 ms 189520 KB Output is correct
29 Correct 362 ms 189524 KB Output is correct
30 Correct 608 ms 189704 KB Output is correct
31 Correct 585 ms 189520 KB Output is correct
32 Correct 327 ms 189520 KB Output is correct
33 Correct 369 ms 189524 KB Output is correct
34 Correct 316 ms 189752 KB Output is correct
35 Correct 322 ms 189772 KB Output is correct
36 Correct 327 ms 189552 KB Output is correct
37 Correct 372 ms 189572 KB Output is correct
38 Correct 414 ms 189788 KB Output is correct
39 Correct 350 ms 189520 KB Output is correct
40 Correct 391 ms 189520 KB Output is correct
41 Correct 331 ms 189636 KB Output is correct
42 Correct 328 ms 189532 KB Output is correct
43 Correct 385 ms 189524 KB Output is correct
44 Correct 388 ms 189780 KB Output is correct
45 Correct 577 ms 189760 KB Output is correct
46 Correct 621 ms 189776 KB Output is correct
47 Correct 573 ms 189524 KB Output is correct
48 Correct 334 ms 189760 KB Output is correct
49 Correct 287 ms 189708 KB Output is correct
50 Correct 310 ms 189576 KB Output is correct
51 Correct 332 ms 189716 KB Output is correct
52 Correct 324 ms 189528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 181072 KB Output is correct
2 Correct 330 ms 189524 KB Output is correct
3 Correct 381 ms 189520 KB Output is correct
4 Correct 301 ms 189600 KB Output is correct
5 Correct 388 ms 189556 KB Output is correct
6 Correct 325 ms 189524 KB Output is correct
7 Correct 393 ms 189520 KB Output is correct
8 Correct 296 ms 189520 KB Output is correct
9 Correct 362 ms 189524 KB Output is correct
10 Correct 608 ms 189704 KB Output is correct
11 Correct 585 ms 189520 KB Output is correct
12 Correct 327 ms 189520 KB Output is correct
13 Correct 369 ms 189524 KB Output is correct
14 Correct 316 ms 189752 KB Output is correct
15 Correct 322 ms 189772 KB Output is correct
16 Correct 74 ms 181072 KB Output is correct
17 Correct 437 ms 189976 KB Output is correct
18 Correct 408 ms 190036 KB Output is correct
19 Correct 438 ms 190124 KB Output is correct
20 Correct 445 ms 190000 KB Output is correct
21 Correct 465 ms 189920 KB Output is correct
22 Correct 391 ms 190192 KB Output is correct
23 Correct 454 ms 190036 KB Output is correct
24 Correct 462 ms 190036 KB Output is correct
25 Correct 461 ms 190196 KB Output is correct
26 Correct 470 ms 189976 KB Output is correct
27 Correct 425 ms 190192 KB Output is correct
28 Correct 387 ms 190276 KB Output is correct
29 Correct 385 ms 190292 KB Output is correct
30 Correct 423 ms 189936 KB Output is correct
31 Correct 440 ms 189944 KB Output is correct
32 Correct 708 ms 190092 KB Output is correct
33 Correct 608 ms 190292 KB Output is correct
34 Correct 604 ms 190036 KB Output is correct
35 Correct 613 ms 190032 KB Output is correct
36 Correct 624 ms 189976 KB Output is correct
37 Correct 360 ms 189780 KB Output is correct
38 Correct 373 ms 190032 KB Output is correct
39 Correct 413 ms 190292 KB Output is correct
40 Correct 423 ms 190292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 64 ms 181072 KB Output is correct
2 Correct 330 ms 189524 KB Output is correct
3 Correct 381 ms 189520 KB Output is correct
4 Correct 301 ms 189600 KB Output is correct
5 Correct 388 ms 189556 KB Output is correct
6 Correct 325 ms 189524 KB Output is correct
7 Correct 393 ms 189520 KB Output is correct
8 Correct 296 ms 189520 KB Output is correct
9 Correct 362 ms 189524 KB Output is correct
10 Correct 608 ms 189704 KB Output is correct
11 Correct 585 ms 189520 KB Output is correct
12 Correct 327 ms 189520 KB Output is correct
13 Correct 369 ms 189524 KB Output is correct
14 Correct 316 ms 189752 KB Output is correct
15 Correct 322 ms 189772 KB Output is correct
16 Correct 64 ms 181072 KB Output is correct
17 Correct 1185 ms 189776 KB Output is correct
18 Correct 800 ms 189592 KB Output is correct
19 Correct 927 ms 189776 KB Output is correct
20 Correct 801 ms 189776 KB Output is correct
21 Correct 1288 ms 190000 KB Output is correct
22 Correct 858 ms 189796 KB Output is correct
23 Correct 1178 ms 189776 KB Output is correct
24 Correct 951 ms 189780 KB Output is correct
25 Correct 1102 ms 189696 KB Output is correct
26 Correct 576 ms 189776 KB Output is correct
27 Correct 618 ms 189776 KB Output is correct
28 Correct 764 ms 189936 KB Output is correct
29 Correct 571 ms 189864 KB Output is correct
30 Correct 746 ms 189788 KB Output is correct
31 Correct 812 ms 189852 KB Output is correct
32 Correct 1035 ms 189792 KB Output is correct
33 Correct 948 ms 189780 KB Output is correct
34 Correct 1022 ms 190036 KB Output is correct
35 Correct 848 ms 189844 KB Output is correct
36 Correct 956 ms 189780 KB Output is correct
37 Correct 677 ms 190128 KB Output is correct
38 Correct 585 ms 190032 KB Output is correct
39 Correct 620 ms 189912 KB Output is correct
40 Correct 524 ms 190032 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 181080 KB Output is correct
2 Correct 71 ms 181072 KB Output is correct
3 Correct 70 ms 180876 KB Output is correct
4 Correct 68 ms 181020 KB Output is correct
5 Correct 75 ms 181032 KB Output is correct
6 Correct 73 ms 181116 KB Output is correct
7 Correct 74 ms 181072 KB Output is correct
8 Correct 74 ms 181044 KB Output is correct
9 Correct 78 ms 180940 KB Output is correct
10 Correct 74 ms 180976 KB Output is correct
11 Correct 75 ms 181076 KB Output is correct
12 Correct 74 ms 181076 KB Output is correct
13 Correct 68 ms 180948 KB Output is correct
14 Correct 78 ms 181072 KB Output is correct
15 Correct 75 ms 181076 KB Output is correct
16 Correct 90 ms 181036 KB Output is correct
17 Correct 101 ms 181076 KB Output is correct
18 Correct 74 ms 181076 KB Output is correct
19 Correct 70 ms 181072 KB Output is correct
20 Correct 76 ms 181120 KB Output is correct
21 Correct 64 ms 181072 KB Output is correct
22 Correct 330 ms 189524 KB Output is correct
23 Correct 381 ms 189520 KB Output is correct
24 Correct 301 ms 189600 KB Output is correct
25 Correct 388 ms 189556 KB Output is correct
26 Correct 325 ms 189524 KB Output is correct
27 Correct 393 ms 189520 KB Output is correct
28 Correct 296 ms 189520 KB Output is correct
29 Correct 362 ms 189524 KB Output is correct
30 Correct 608 ms 189704 KB Output is correct
31 Correct 585 ms 189520 KB Output is correct
32 Correct 327 ms 189520 KB Output is correct
33 Correct 369 ms 189524 KB Output is correct
34 Correct 316 ms 189752 KB Output is correct
35 Correct 322 ms 189772 KB Output is correct
36 Correct 327 ms 189552 KB Output is correct
37 Correct 372 ms 189572 KB Output is correct
38 Correct 414 ms 189788 KB Output is correct
39 Correct 350 ms 189520 KB Output is correct
40 Correct 391 ms 189520 KB Output is correct
41 Correct 331 ms 189636 KB Output is correct
42 Correct 328 ms 189532 KB Output is correct
43 Correct 385 ms 189524 KB Output is correct
44 Correct 388 ms 189780 KB Output is correct
45 Correct 577 ms 189760 KB Output is correct
46 Correct 621 ms 189776 KB Output is correct
47 Correct 573 ms 189524 KB Output is correct
48 Correct 334 ms 189760 KB Output is correct
49 Correct 287 ms 189708 KB Output is correct
50 Correct 310 ms 189576 KB Output is correct
51 Correct 332 ms 189716 KB Output is correct
52 Correct 324 ms 189528 KB Output is correct
53 Correct 74 ms 181072 KB Output is correct
54 Correct 437 ms 189976 KB Output is correct
55 Correct 408 ms 190036 KB Output is correct
56 Correct 438 ms 190124 KB Output is correct
57 Correct 445 ms 190000 KB Output is correct
58 Correct 465 ms 189920 KB Output is correct
59 Correct 391 ms 190192 KB Output is correct
60 Correct 454 ms 190036 KB Output is correct
61 Correct 462 ms 190036 KB Output is correct
62 Correct 461 ms 190196 KB Output is correct
63 Correct 470 ms 189976 KB Output is correct
64 Correct 425 ms 190192 KB Output is correct
65 Correct 387 ms 190276 KB Output is correct
66 Correct 385 ms 190292 KB Output is correct
67 Correct 423 ms 189936 KB Output is correct
68 Correct 440 ms 189944 KB Output is correct
69 Correct 708 ms 190092 KB Output is correct
70 Correct 608 ms 190292 KB Output is correct
71 Correct 604 ms 190036 KB Output is correct
72 Correct 613 ms 190032 KB Output is correct
73 Correct 624 ms 189976 KB Output is correct
74 Correct 360 ms 189780 KB Output is correct
75 Correct 373 ms 190032 KB Output is correct
76 Correct 413 ms 190292 KB Output is correct
77 Correct 423 ms 190292 KB Output is correct
78 Correct 64 ms 181072 KB Output is correct
79 Correct 1185 ms 189776 KB Output is correct
80 Correct 800 ms 189592 KB Output is correct
81 Correct 927 ms 189776 KB Output is correct
82 Correct 801 ms 189776 KB Output is correct
83 Correct 1288 ms 190000 KB Output is correct
84 Correct 858 ms 189796 KB Output is correct
85 Correct 1178 ms 189776 KB Output is correct
86 Correct 951 ms 189780 KB Output is correct
87 Correct 1102 ms 189696 KB Output is correct
88 Correct 576 ms 189776 KB Output is correct
89 Correct 618 ms 189776 KB Output is correct
90 Correct 764 ms 189936 KB Output is correct
91 Correct 571 ms 189864 KB Output is correct
92 Correct 746 ms 189788 KB Output is correct
93 Correct 812 ms 189852 KB Output is correct
94 Correct 1035 ms 189792 KB Output is correct
95 Correct 948 ms 189780 KB Output is correct
96 Correct 1022 ms 190036 KB Output is correct
97 Correct 848 ms 189844 KB Output is correct
98 Correct 956 ms 189780 KB Output is correct
99 Correct 677 ms 190128 KB Output is correct
100 Correct 585 ms 190032 KB Output is correct
101 Correct 620 ms 189912 KB Output is correct
102 Correct 524 ms 190032 KB Output is correct
103 Correct 1258 ms 189780 KB Output is correct
104 Correct 824 ms 189780 KB Output is correct
105 Correct 635 ms 189956 KB Output is correct
106 Correct 603 ms 190132 KB Output is correct
107 Correct 1269 ms 190012 KB Output is correct
108 Correct 810 ms 189780 KB Output is correct
109 Correct 786 ms 189888 KB Output is correct
110 Correct 692 ms 190032 KB Output is correct
111 Correct 567 ms 190040 KB Output is correct
112 Correct 613 ms 190036 KB Output is correct
113 Correct 591 ms 189764 KB Output is correct
114 Correct 482 ms 190036 KB Output is correct
115 Correct 811 ms 189776 KB Output is correct
116 Correct 635 ms 190036 KB Output is correct
117 Correct 453 ms 190036 KB Output is correct
118 Correct 513 ms 190124 KB Output is correct
119 Correct 566 ms 189780 KB Output is correct
120 Correct 789 ms 189780 KB Output is correct
121 Correct 615 ms 189940 KB Output is correct
122 Correct 1043 ms 189744 KB Output is correct
123 Correct 892 ms 190292 KB Output is correct
124 Correct 804 ms 190032 KB Output is correct
125 Correct 656 ms 189932 KB Output is correct
126 Correct 699 ms 190044 KB Output is correct
127 Correct 672 ms 189780 KB Output is correct
128 Correct 457 ms 190032 KB Output is correct
129 Correct 555 ms 189776 KB Output is correct
130 Correct 446 ms 190092 KB Output is correct