Submission #1092550

# Submission time Handle Problem Language Result Execution time Memory
1092550 2024-09-24T11:10:19 Z akim9905 역사적 조사 (JOI14_historical) C++17
100 / 100
1645 ms 9096 KB
#include <bits/stdc++.h>
using namespace std;

#define fileio() freopen("input.txt","r",stdin); freopen("output.txt","w",stdout)
#define fio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define allr(x) x.rbegin(), x.rend()
#define cmprs(x) sort(all(x)),x.erase(unique(all(x)),x.end())
#define endl "\n"
#define sp " "
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define rz resize
#define sz(a) (int)(a.size())
#define clr(a) a.clear()

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef tuple<int, int, int> tpi;
typedef tuple<ll, ll, ll> tpl;
typedef pair<double, ll> pdl;
typedef pair<double, int> pdi;

const int dx[] = {1,-1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAX = 101010; // PLZ CHK!

typedef array<int,3> arr;

struct segtr{
    vector<ll> tr;
    int n;
    void rst(int sz) {
        n=sz;
        tr.resize((n+1)<<1);
    }

    void upd(int i, ll v) {
        tr[i+n]+=v; i+=n; //AWARE OF UPD POLICY!!
        for (i>>=1; i; i>>=1) tr[i] = max(tr[i<<1], tr[i<<1|1]);
    }

    ll qry(int l, int r) {
        ll ret=0;
        for (l+=n, r+=n; l<=r; l>>=1, r>>=1) {
            if (l&1) ret = max(ret, tr[l++]);
            if (~r&1) ret = max(ret, tr[r--]);
        }
        return ret;
    }
};

int N,Q;
ll X[MAX],ans[MAX];
arr qry[MAX];


int main() {
    fio();
    cin>>N>>Q;
    vector<ll> t;
    for (int i=1; i<=N; i++) {
        cin>>X[i];
        t.pb(X[i]);
    }
    t.pb(0);

    cmprs(t);
    for (int i=1; i<=N; i++) {
        X[i]=lb(all(t),X[i])-t.begin();
    }

    for (int i=1; i<=Q; i++) {
        int l,r; cin>>l>>r;
        qry[i]={l,r,i};
    }

    const int sq=(int)sqrt(N);
    sort(qry+1,qry+Q+1,[&](auto a, auto b){
        auto [al,ar,ai]=a; auto [bl,br,bi]=b;
        if (al/sq==bl/sq) return ar<br;
        return al/sq<bl/sq;
    });

    segtr seg; seg.rst(N+10);
    for (int l=1,r=0,q=1; q<=Q; q++) {
        auto [ql,qr,idx]=qry[q];
        while (r<qr) {
            r++;
            int j=X[r]; ll val=t[j];
            seg.upd(j,val);
        }
        while (ql<l) {
            l--;
            int j=X[l]; ll val=t[j];
            seg.upd(j,val);
        }

        while (qr<r) {
            int j=X[r]; ll val=t[j];
            seg.upd(j,-val);
            r--;
        }
        while (l<ql) {
            int j=X[l]; ll val=t[j];
            seg.upd(j,-val);
            l++;
        }

        ans[idx]=seg.qry(0,N+1);
    }

    for (int i=1; i<=Q; i++) cout<<ans[i]<<endl;
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 464 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 464 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 612 KB Output is correct
20 Correct 6 ms 604 KB Output is correct
21 Correct 12 ms 856 KB Output is correct
22 Correct 12 ms 876 KB Output is correct
23 Correct 12 ms 872 KB Output is correct
24 Correct 12 ms 856 KB Output is correct
25 Correct 12 ms 860 KB Output is correct
26 Correct 15 ms 892 KB Output is correct
27 Correct 12 ms 908 KB Output is correct
28 Correct 13 ms 856 KB Output is correct
29 Correct 12 ms 860 KB Output is correct
30 Correct 13 ms 860 KB Output is correct
31 Correct 12 ms 868 KB Output is correct
32 Correct 12 ms 860 KB Output is correct
33 Correct 12 ms 896 KB Output is correct
34 Correct 14 ms 856 KB Output is correct
35 Correct 12 ms 908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 1 ms 604 KB Output is correct
6 Correct 1 ms 604 KB Output is correct
7 Correct 2 ms 604 KB Output is correct
8 Correct 4 ms 1116 KB Output is correct
9 Correct 7 ms 1504 KB Output is correct
10 Correct 8 ms 2524 KB Output is correct
11 Correct 44 ms 7336 KB Output is correct
12 Correct 22 ms 4308 KB Output is correct
13 Correct 33 ms 5328 KB Output is correct
14 Correct 48 ms 6644 KB Output is correct
15 Correct 72 ms 8748 KB Output is correct
16 Correct 44 ms 6608 KB Output is correct
17 Correct 23 ms 5068 KB Output is correct
18 Correct 39 ms 7376 KB Output is correct
19 Correct 45 ms 7880 KB Output is correct
20 Correct 28 ms 5328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 464 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 464 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 3 ms 612 KB Output is correct
20 Correct 6 ms 604 KB Output is correct
21 Correct 12 ms 856 KB Output is correct
22 Correct 12 ms 876 KB Output is correct
23 Correct 12 ms 872 KB Output is correct
24 Correct 12 ms 856 KB Output is correct
25 Correct 12 ms 860 KB Output is correct
26 Correct 15 ms 892 KB Output is correct
27 Correct 12 ms 908 KB Output is correct
28 Correct 13 ms 856 KB Output is correct
29 Correct 12 ms 860 KB Output is correct
30 Correct 13 ms 860 KB Output is correct
31 Correct 12 ms 868 KB Output is correct
32 Correct 12 ms 860 KB Output is correct
33 Correct 12 ms 896 KB Output is correct
34 Correct 14 ms 856 KB Output is correct
35 Correct 12 ms 908 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 604 KB Output is correct
41 Correct 1 ms 604 KB Output is correct
42 Correct 2 ms 604 KB Output is correct
43 Correct 4 ms 1116 KB Output is correct
44 Correct 7 ms 1504 KB Output is correct
45 Correct 8 ms 2524 KB Output is correct
46 Correct 44 ms 7336 KB Output is correct
47 Correct 22 ms 4308 KB Output is correct
48 Correct 33 ms 5328 KB Output is correct
49 Correct 48 ms 6644 KB Output is correct
50 Correct 72 ms 8748 KB Output is correct
51 Correct 44 ms 6608 KB Output is correct
52 Correct 23 ms 5068 KB Output is correct
53 Correct 39 ms 7376 KB Output is correct
54 Correct 45 ms 7880 KB Output is correct
55 Correct 28 ms 5328 KB Output is correct
56 Correct 35 ms 1116 KB Output is correct
57 Correct 105 ms 1980 KB Output is correct
58 Correct 219 ms 2776 KB Output is correct
59 Correct 335 ms 3648 KB Output is correct
60 Correct 465 ms 4356 KB Output is correct
61 Correct 607 ms 5364 KB Output is correct
62 Correct 829 ms 6348 KB Output is correct
63 Correct 976 ms 7372 KB Output is correct
64 Correct 1139 ms 8120 KB Output is correct
65 Correct 1390 ms 8396 KB Output is correct
66 Correct 1372 ms 8540 KB Output is correct
67 Correct 1363 ms 8624 KB Output is correct
68 Correct 1390 ms 8580 KB Output is correct
69 Correct 1531 ms 8912 KB Output is correct
70 Correct 1645 ms 8908 KB Output is correct
71 Correct 1602 ms 8672 KB Output is correct
72 Correct 1558 ms 8832 KB Output is correct
73 Correct 1548 ms 8708 KB Output is correct
74 Correct 1532 ms 8912 KB Output is correct
75 Correct 1524 ms 8908 KB Output is correct
76 Correct 1459 ms 8908 KB Output is correct
77 Correct 1470 ms 8828 KB Output is correct
78 Correct 1486 ms 8820 KB Output is correct
79 Correct 1480 ms 8952 KB Output is correct
80 Correct 1386 ms 9096 KB Output is correct