답안 #870891

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
870891 2023-11-09T11:56:16 Z willychan Fish 2 (JOI22_fish2) C++14
60 / 100
4000 ms 32164 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds

vector<ll> A;
struct SegTree {
    vector<pair<int,int> > arr;
    vector<int> tag;
    int n;
	void built(int ind,int L,int R){
		arr[ind].second = (R-L+1); 
		if(L==R) return;
		int M = (L+R)/2;
		built(2*ind,L,M);
		built(2*ind+1,M+1,R);
	}
    void init(int _n) {
        n =_n;
        arr.resize(4*n);
        tag.resize(4*n); 
		built(1,1,n);
	} 
	void push(int ind) {
        if(2*ind+1>4*n) return;
        if(tag[ind]) {
            arr[2*ind].first+=tag[ind];
            arr[2*ind+1].first+=tag[ind];
            tag[2*ind]+=tag[ind];
            tag[2*ind+1]+=tag[ind];
            tag[ind]=0;
        }
    }
    inline pair<int,int> merge(const pair<int,int> a,const pair<int,int> b) {
        if(a.first!=b.first) return min(a,b);
        return {a.first,a.second+b.second};
    }
    pair<int,int> query(int ind,int l,int r,int L,int R) {
        if(l==L && r==R) {
            return arr[ind];
        }
        push(ind);
        int M = (L+R)/2;
        if(r<=M) return query(2*ind,l,r,L,M);
        if(l>M) return query(2*ind+1,l,r,M+1,R);
        return merge(query(2*ind,l,M,L,M),query(2*ind+1,M+1,r,M+1,R));
    }
    void modify(int ind,int l,int r,int L,int R,int v) {
		//cout<<l<<" "<<r<<"~"<<L<<" "<<R<<"safd"<<endl;
        if(l==L && r==R) {
            arr[ind].first+=v;
            tag[ind]+=v;
            return ;
        }
        push(ind);
        int M = (L+R)/2;
        if(r<=M) modify(2*ind,l,r,L,M,v);
        else if(l>M) modify(2*ind+1,l,r,M+1,R,v);
        else {
            modify(2*ind,l,M,L,M,v);
            modify(2*ind+1,M+1,r,M+1,R,v);
        }
        arr[ind] = merge(arr[2*ind],arr[2*ind+1]);
    }

} segtree;

struct range {
    int l,r;
    range(int _l,int _r) {
        l = _l;
        r = _r;
    }
    bool operator<(const range &rhs) const {
        return (r-l)<(rhs.r-rhs.l);
    }
};
struct IntTree {
    vector<priority_queue<range> > arr;
    int n;
    void init(int _n) {
        n = _n;
        arr.resize(4*n);
    }
    void insert(range a,int ind,int L,int R) {
        int M = (L+R)/2;
        if(a.l<=M && a.r>=M) { 
			//cout<<"insert "<<a.l<<" "<<a.r<<"->"<<ind<<"\n";
            arr[ind].push(a);
            segtree.modify(1,a.l,a.r,1,n,1);
            return;
        }
        if(a.r<=M) insert(a,2*ind,L,M);
        else insert(a,2*ind+1,M+1,R);
    }
    void remove(int x,int ind,int L,int R) {
        while(arr[ind].size()) {
            range cur = arr[ind].top();
            if(cur.l<=x && cur.r>=x) {
                segtree.modify(1,cur.l,cur.r,1,n,-1);
                arr[ind].pop();
            } else {
                break;
            }
        }
        int M = (L+R)/2;
		if(x==M) return;
        if(x<M) remove(x,2*ind,L,M);
        if(x>M) remove(x,2*ind+1,M+1,R);
    }
	void out(int ind,int L,int R){
		auto pq = arr[ind];
		while(pq.size()){
//			cout<<pq.top().l<<" "<<pq.top().r<<"->"<<ind<<"k\n";
			pq.pop();
		}
		if(L==R) return;
		int M = (L+R)/2;
		out(2*ind,L,M);
		out(2*ind+1,M+1,R);
	}
} inttree;

struct SegTree2 {
    vector< pair<ll,ll> > arr;
    // a[i]+A[i]>A[x]
    // 2*a[i]-A[i]>-A[x-1]
    vector<ll> tag;
    int n;
    void init(int _n) {
        n = _n;
        arr.resize(4*n);
        tag.resize(4*n);
    }
    void apply(int ind,ll v) {
        arr[ind].first+=v;
        arr[ind].second-=v;
        tag[ind]+=v;
    }
    void push(int ind) {
        if(2*ind+1>=4*n) return;
        if(tag[ind]) {
            apply(2*ind,tag[ind]);
            apply(2*ind+1,tag[ind]);
            tag[ind]=0;
        }
    }
    ll sum(int ind,int x,int L,int R) {
        if(L==R && L==x) {
            return arr[ind].first-A[x];
        }
        push(ind);
        int M = (L+R)/2;
        if(x<=M) return sum(2*ind,x,L,M);
        if(x>M) return sum(2*ind+1,x,M+1,R);
        assert(1==0);
        return 0;
    }
    void smodify(int ind,int x,int L,int R,ll v) {
        if(L==x && R==x) {
            arr[ind].first+=v;
            arr[ind].second+=2*v;
            return;
        }
        push(ind);
        int M = (L+R)/2;
        if(x<=M) smodify(2*ind,x,L,M,v);
        if(x>M) smodify(2*ind+1,x,M+1,R,v);
        arr[ind] = {max(arr[2*ind].first,arr[2*ind+1].first),max(arr[2*ind].second,arr[2*ind+1].second)};
    }
    void rmodify(int ind,int l,int r,int L,int R,ll v) {
        if(l==L && r==R) {
            apply(ind,v);
            return;
        }
        push(ind);
        int M = (L+R)/2;
        if(r<=M) rmodify(2*ind,l,r,L,M,v);
        else if(l>M)  rmodify(2*ind+1,l,r,M+1,R,v);
        else {
            rmodify(2*ind,l,M,L,M,v);
            rmodify(2*ind+1,M+1,r,M+1,R,v);
        }
        arr[ind] = {max(arr[2*ind].first,arr[2*ind+1].first),max(arr[2*ind].second,arr[2*ind+1].second)};
    }

    void update(int ind,ll v) {
        ll k = v-A[ind];
        smodify(1,ind,1,n,k);
        rmodify(1,ind,n,1,n,k);
        A[ind]=v;
    }
    void getr(int ind,ll v,int x,int L,int R,vector<pair<int,ll> > &ret) {
        if(R<x) return;
        if(arr[ind].second<=v) return;
        if(L==R) {
            ret.push_back({L,arr[ind].second});
            return;
        }
        push(ind);
        int M =(L+R)/2;
        getr(2*ind,v,x,L,M,ret);
        getr(2*ind+1,v,x,M+1,R,ret);
    }
    void getl(int ind,ll v,int x,int L,int R,vector<pair<int,ll> > &ret) {
        if(L>x)	 return;
        if(arr[ind].first<=v) return;
        if(L==R) {
            ret.push_back({L,arr[ind].first});
            return;
        }
        push(ind);
        int M = (L+R)/2;
        getl(2*ind,v,x,L,M,ret);
        getl(2*ind+1,v,x,M+1,R,ret);
    }
} segtree2;

int n;
void MainUpdate(int ind,ll v) {
    inttree.remove(ind,1,1,n);
	inttree.remove(ind-1,1,1,n);
	inttree.remove(ind+1,1,1,n);
    segtree2.update(ind,v);
    vector<pair<int,ll> > rcan;
    vector<pair<int,ll> > lcan;
    ll Ax = segtree2.sum(1,ind,1,n);
    segtree2.getr(1,-(Ax),ind,1,n,rcan);
    segtree2.getl(1,Ax-A[ind],ind,1,n,lcan);
    // a[i]+A[i]>A[x-1]
    // 2*a[i]-A[i]>-A[x]
    for(auto pr : rcan) {
        for(auto pl : lcan) { if(pr.first-1<pl.first+1) continue;
            ll sumbet = (A[pr.first]-pr.second)-(pl.second-A[pl.first]);
            if(A[pr.first]>sumbet && A[pl.first]>sumbet){
				//if(ind==4) cout<<pl.first<<" "<<pr.first<<"gg\n";
				inttree.insert(range(pl.first+1,pr.first-1),1,1,n);
			}
        }
    }

}

int main() {
    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    n+=2;
    segtree.init(n);
    inttree.init(n);
    segtree2.init(n);
    A.resize(n+1);
    inttree.insert(range(2,n-1),1,1,n);
    segtree2.update(1,1e16);
    segtree2.update(n,1e16);
    for(int i=2; i<n; i++) {
        ll v;
        cin>>v;
		MainUpdate(i,v);
    }
    int q;
    cin>>q;
    while(q--) {
        int t;
        cin>>t;
        if(t==1) {
            int x,y;
            cin>>x>>y;
            x++;
//			cout<<"t\n";
			MainUpdate(x,y);
        } else {
            int l,r;
            cin>>l>>r;
            l++;
            r++;
			ll rog = A[r+1];
			ll log = A[l-1];
			if(r+1<n) MainUpdate(r+1,1e16);
			if(l-1>1) MainUpdate(l-1,1e16);
            pair<int,int> ans = segtree.query(1,l,r,1,n);
           cout<<ans.second<<"\n";
			if(r+1<n) MainUpdate(r+1,rog);
			if(l-1>1) MainUpdate(l-1,log);
        }
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 5 ms 604 KB Output is correct
6 Correct 9 ms 464 KB Output is correct
7 Correct 5 ms 604 KB Output is correct
8 Correct 12 ms 456 KB Output is correct
9 Correct 8 ms 604 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 6 ms 620 KB Output is correct
12 Correct 4 ms 856 KB Output is correct
13 Correct 8 ms 600 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 8 ms 600 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 8 ms 604 KB Output is correct
19 Correct 4 ms 612 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 383 ms 29472 KB Output is correct
3 Correct 556 ms 29352 KB Output is correct
4 Correct 414 ms 29468 KB Output is correct
5 Correct 548 ms 29256 KB Output is correct
6 Correct 359 ms 29904 KB Output is correct
7 Correct 593 ms 29192 KB Output is correct
8 Correct 358 ms 29800 KB Output is correct
9 Correct 632 ms 28804 KB Output is correct
10 Correct 899 ms 29104 KB Output is correct
11 Correct 1066 ms 29172 KB Output is correct
12 Correct 464 ms 29504 KB Output is correct
13 Correct 438 ms 29156 KB Output is correct
14 Correct 407 ms 30380 KB Output is correct
15 Correct 406 ms 29748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 5 ms 604 KB Output is correct
6 Correct 9 ms 464 KB Output is correct
7 Correct 5 ms 604 KB Output is correct
8 Correct 12 ms 456 KB Output is correct
9 Correct 8 ms 604 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 6 ms 620 KB Output is correct
12 Correct 4 ms 856 KB Output is correct
13 Correct 8 ms 600 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 8 ms 600 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 8 ms 604 KB Output is correct
19 Correct 4 ms 612 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 383 ms 29472 KB Output is correct
23 Correct 556 ms 29352 KB Output is correct
24 Correct 414 ms 29468 KB Output is correct
25 Correct 548 ms 29256 KB Output is correct
26 Correct 359 ms 29904 KB Output is correct
27 Correct 593 ms 29192 KB Output is correct
28 Correct 358 ms 29800 KB Output is correct
29 Correct 632 ms 28804 KB Output is correct
30 Correct 899 ms 29104 KB Output is correct
31 Correct 1066 ms 29172 KB Output is correct
32 Correct 464 ms 29504 KB Output is correct
33 Correct 438 ms 29156 KB Output is correct
34 Correct 407 ms 30380 KB Output is correct
35 Correct 406 ms 29748 KB Output is correct
36 Correct 411 ms 29508 KB Output is correct
37 Correct 584 ms 29780 KB Output is correct
38 Correct 637 ms 29268 KB Output is correct
39 Correct 403 ms 29652 KB Output is correct
40 Correct 586 ms 29880 KB Output is correct
41 Correct 368 ms 29776 KB Output is correct
42 Correct 378 ms 30252 KB Output is correct
43 Correct 599 ms 28752 KB Output is correct
44 Correct 594 ms 28748 KB Output is correct
45 Correct 973 ms 28992 KB Output is correct
46 Correct 925 ms 28948 KB Output is correct
47 Correct 1079 ms 28440 KB Output is correct
48 Correct 448 ms 29036 KB Output is correct
49 Correct 453 ms 29400 KB Output is correct
50 Correct 419 ms 30064 KB Output is correct
51 Correct 428 ms 30032 KB Output is correct
52 Correct 417 ms 30068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 383 ms 29472 KB Output is correct
3 Correct 556 ms 29352 KB Output is correct
4 Correct 414 ms 29468 KB Output is correct
5 Correct 548 ms 29256 KB Output is correct
6 Correct 359 ms 29904 KB Output is correct
7 Correct 593 ms 29192 KB Output is correct
8 Correct 358 ms 29800 KB Output is correct
9 Correct 632 ms 28804 KB Output is correct
10 Correct 899 ms 29104 KB Output is correct
11 Correct 1066 ms 29172 KB Output is correct
12 Correct 464 ms 29504 KB Output is correct
13 Correct 438 ms 29156 KB Output is correct
14 Correct 407 ms 30380 KB Output is correct
15 Correct 406 ms 29748 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Execution timed out 4057 ms 32144 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 383 ms 29472 KB Output is correct
3 Correct 556 ms 29352 KB Output is correct
4 Correct 414 ms 29468 KB Output is correct
5 Correct 548 ms 29256 KB Output is correct
6 Correct 359 ms 29904 KB Output is correct
7 Correct 593 ms 29192 KB Output is correct
8 Correct 358 ms 29800 KB Output is correct
9 Correct 632 ms 28804 KB Output is correct
10 Correct 899 ms 29104 KB Output is correct
11 Correct 1066 ms 29172 KB Output is correct
12 Correct 464 ms 29504 KB Output is correct
13 Correct 438 ms 29156 KB Output is correct
14 Correct 407 ms 30380 KB Output is correct
15 Correct 406 ms 29748 KB Output is correct
16 Correct 0 ms 344 KB Output is correct
17 Correct 1169 ms 29996 KB Output is correct
18 Correct 1202 ms 32096 KB Output is correct
19 Correct 1143 ms 30636 KB Output is correct
20 Correct 991 ms 32164 KB Output is correct
21 Correct 1075 ms 30480 KB Output is correct
22 Correct 1099 ms 32144 KB Output is correct
23 Correct 1256 ms 30284 KB Output is correct
24 Correct 1078 ms 32080 KB Output is correct
25 Correct 1158 ms 30244 KB Output is correct
26 Correct 622 ms 31588 KB Output is correct
27 Correct 729 ms 31316 KB Output is correct
28 Correct 973 ms 31044 KB Output is correct
29 Correct 662 ms 31256 KB Output is correct
30 Correct 752 ms 31744 KB Output is correct
31 Correct 1179 ms 31852 KB Output is correct
32 Correct 1621 ms 31732 KB Output is correct
33 Correct 1373 ms 29696 KB Output is correct
34 Correct 1421 ms 31952 KB Output is correct
35 Correct 1179 ms 30036 KB Output is correct
36 Correct 1519 ms 31640 KB Output is correct
37 Correct 870 ms 30696 KB Output is correct
38 Correct 712 ms 30800 KB Output is correct
39 Correct 724 ms 31356 KB Output is correct
40 Correct 537 ms 31312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 5 ms 604 KB Output is correct
6 Correct 9 ms 464 KB Output is correct
7 Correct 5 ms 604 KB Output is correct
8 Correct 12 ms 456 KB Output is correct
9 Correct 8 ms 604 KB Output is correct
10 Correct 3 ms 856 KB Output is correct
11 Correct 6 ms 620 KB Output is correct
12 Correct 4 ms 856 KB Output is correct
13 Correct 8 ms 600 KB Output is correct
14 Correct 5 ms 604 KB Output is correct
15 Correct 7 ms 604 KB Output is correct
16 Correct 8 ms 600 KB Output is correct
17 Correct 5 ms 604 KB Output is correct
18 Correct 8 ms 604 KB Output is correct
19 Correct 4 ms 612 KB Output is correct
20 Correct 5 ms 604 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 383 ms 29472 KB Output is correct
23 Correct 556 ms 29352 KB Output is correct
24 Correct 414 ms 29468 KB Output is correct
25 Correct 548 ms 29256 KB Output is correct
26 Correct 359 ms 29904 KB Output is correct
27 Correct 593 ms 29192 KB Output is correct
28 Correct 358 ms 29800 KB Output is correct
29 Correct 632 ms 28804 KB Output is correct
30 Correct 899 ms 29104 KB Output is correct
31 Correct 1066 ms 29172 KB Output is correct
32 Correct 464 ms 29504 KB Output is correct
33 Correct 438 ms 29156 KB Output is correct
34 Correct 407 ms 30380 KB Output is correct
35 Correct 406 ms 29748 KB Output is correct
36 Correct 411 ms 29508 KB Output is correct
37 Correct 584 ms 29780 KB Output is correct
38 Correct 637 ms 29268 KB Output is correct
39 Correct 403 ms 29652 KB Output is correct
40 Correct 586 ms 29880 KB Output is correct
41 Correct 368 ms 29776 KB Output is correct
42 Correct 378 ms 30252 KB Output is correct
43 Correct 599 ms 28752 KB Output is correct
44 Correct 594 ms 28748 KB Output is correct
45 Correct 973 ms 28992 KB Output is correct
46 Correct 925 ms 28948 KB Output is correct
47 Correct 1079 ms 28440 KB Output is correct
48 Correct 448 ms 29036 KB Output is correct
49 Correct 453 ms 29400 KB Output is correct
50 Correct 419 ms 30064 KB Output is correct
51 Correct 428 ms 30032 KB Output is correct
52 Correct 417 ms 30068 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Execution timed out 4057 ms 32144 KB Time limit exceeded
55 Halted 0 ms 0 KB -