Submission #914656

# Submission time Handle Problem Language Result Execution time Memory
914656 2024-01-22T13:26:37 Z mychecksedad Fire (JOI20_ho_t5) C++17
100 / 100
574 ms 218604 KB
/* Author : Mychecksdead  */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
const int N = 3e6+100, M = 1e5+10, K = 52, MX = 30;


int n, q, L[N], R[N];
ll a[N], ans[N], T[N], lazy[N], F[N], fazy[N];
vector<array<int, 3>> Q[N];
vector<array<ll, 3>> UPD[N];

void build(int l, int r, int k){
  if(l == r){
    F[k] = T[k] = lazy[k] = fazy[k] = 0;
    return;
  }
  int m = l+r>>1;
  build(l, m, k<<1);
  build(m+1, r, k<<1|1);
  T[k] = F[k] = lazy[k] = fazy[k] = 0;
}

void push(int k, int l, int r, int m){
  T[k<<1] += lazy[k]*(ll)(m-l+1);
  T[k<<1|1] += lazy[k]*(ll)(r-m);
  lazy[k<<1] += lazy[k];
  lazy[k<<1|1] += lazy[k];
  lazy[k] = 0;
}
void push2(int k, int l, int r, int m){
  F[k<<1] += fazy[k]*(ll)(m-l+1);
  F[k<<1|1] += fazy[k]*(ll)(r-m);
  fazy[k<<1] += fazy[k];
  fazy[k<<1|1] += fazy[k];
  fazy[k] = 0;
}

void upd(int l, int r, int ql, int qr, int k, ll val){
  if(ql > r || l > qr) return;
  if(ql <= l && r <= qr){
    T[k] += (ll)(r-l+1)*val;
    lazy[k] += val;
    return;
  }
  int m = l+r>>1;
  push(k, l, r, m);
  upd(l, m, ql, qr, k<<1, val);
  upd(m+1, r, ql, qr, k<<1|1, val);
  T[k] = T[k<<1] + T[k<<1|1];
}
void upd2(int l, int r, int ql, int qr, int k, ll val){
  if(ql > r || l > qr) return;
  if(ql <= l && r <= qr){
    F[k] += (ll)(r-l+1)*val;
    fazy[k] += val;
    return;
  }
  int m = l+r>>1;
  push2(k, l, r, m);
  upd2(l, m, ql, qr, k<<1, val);
  upd2(m+1, r, ql, qr, k<<1|1, val);
  F[k] = F[k<<1] + F[k<<1|1];
}

ll get(int l, int r, int ql, int qr, int k){
  if(ql > r || l > qr) return 0;
  if(ql <= l && r <= qr) return T[k];
  int m = l+r>>1;
  push(k, l, r, m);
  return get(l, m, ql, qr, k<<1) + get(m+1, r, ql, qr, k<<1|1);
}
ll get2(int l, int r, int ql, int qr, int k){
  if(ql > r || l > qr) return 0;
  if(ql <= l && r <= qr) return F[k];
  int m = l+r>>1;
  push2(k, l, r, m);
  return get2(l, m, ql, qr, k<<1) + get2(m+1, r, ql, qr, k<<1|1);
}

void add_triangle(int posd, int posv, ll val){
  if(posd >= 1 && posd <= 2*n) upd(1, 2*n, posd, 2*n, 1, val);
  if(posv < n) 
    upd2(1, n, posv + 1, n, 1, -val);
}

void solve(){
  cin >> n >> q;
  for(int i = 1; i <= n; ++i) cin >> a[i];
  vector<int> qq;  
  qq.pb(n);
  L[n] = n + 1;
  for(int i = n-1; i >= 1; --i){
    while(!qq.empty() && a[qq.back()] < a[i]) qq.pop_back();
    if(qq.empty()) L[i] = n + 1;
    else L[i] = qq.back();
    qq.pb(i);
  }
  qq.clear();
  qq.pb(1);
  R[1] = 0;
  for(int i = 2; i <= n; ++i){
    while(!qq.empty() && a[qq.back()] <= a[i]) qq.pop_back();
    if(qq.empty()) R[i] = 0;
    else R[i] = qq.back();
    qq.pb(i);
  }

  for(int i = 0; i < q; ++i){
    int t, l, r; cin >> t >> l >> r;
    Q[t].pb({l, r, i});
    ans[i] = 0;
    assert(t> 0);
  }
  build(1, 2*n, 1);

  for(int i = 1; i <= n; ++i){
    // cout << L[i] << ' ' << R[i] << '\n';
    if(R[i] > 0){
      int t = L[i]-1-i+i-R[i]-1, x = L[i] - 1;
      UPD[t].pb({n-t+x, x, a[i]}); 
      if(i-R[i]-2 >= 1)
        UPD[i-R[i]-2].pb({n-(i-R[i]-2)+i-1, i-1, -a[i]}); 
      if(L[i]-i-2 >= 1)
        UPD[L[i] - i - 2].pb({n-(L[i] - i - 2)+x, x, -a[i]}); 
    }else{
      upd2(1, n, i, L[i]-1, 1, a[i]);
      if(i < n && L[i]-i-2 >= 1)
        UPD[L[i]-i-2].pb({n-(L[i]-i-2)+L[i]-1, L[i]-1, -a[i]});
      // cout << i+n+1 << '\n';
    }
  }

  for(int i = n; i >= 1; --i){
    for(auto k: UPD[i]){
      add_triangle(k[0], k[1], k[2]);
      // cout << k[0] << ' ' << k[1] << ' ' << k[2] <<"triangle\n";
    }
    // cout << i << "\n";
    for(auto x: Q[i]){
      // cout << qq[0] << ' ' << qq[1] << ' ' << n-i+qq[0] << ' ' << n-i+qq[1] << '\n';
      ans[x[2]] = get(1, 2*n, n-i+x[0], n-i+x[1], 1) + get2(1, n, x[0], x[1], 1);
    }
  }


  for(int i = 0 ; i < q; ++i) cout << ans[i] << '\n';
}



int main(){
  cin.tie(0); ios::sync_with_stdio(0);
  int tt = 1, aa;
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  while(tt--){
    solve();
  }
  return 0;
} 

Compilation message

ho_t5.cpp: In function 'void build(int, int, int)':
ho_t5.cpp:23:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   23 |   int m = l+r>>1;
      |           ~^~
ho_t5.cpp: In function 'void upd(int, int, int, int, int, long long int)':
ho_t5.cpp:51:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   51 |   int m = l+r>>1;
      |           ~^~
ho_t5.cpp: In function 'void upd2(int, int, int, int, int, long long int)':
ho_t5.cpp:64:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   64 |   int m = l+r>>1;
      |           ~^~
ho_t5.cpp: In function 'long long int get(int, int, int, int, int)':
ho_t5.cpp:74:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   74 |   int m = l+r>>1;
      |           ~^~
ho_t5.cpp: In function 'long long int get2(int, int, int, int, int)':
ho_t5.cpp:81:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   81 |   int m = l+r>>1;
      |           ~^~
ho_t5.cpp: In function 'int main()':
ho_t5.cpp:159:15: warning: unused variable 'aa' [-Wunused-variable]
  159 |   int tt = 1, aa;
      |               ^~
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156564 KB Output is correct
2 Correct 48 ms 156540 KB Output is correct
3 Correct 45 ms 156504 KB Output is correct
4 Correct 46 ms 156496 KB Output is correct
5 Correct 47 ms 156516 KB Output is correct
6 Correct 46 ms 156488 KB Output is correct
7 Correct 46 ms 156496 KB Output is correct
8 Correct 44 ms 156516 KB Output is correct
9 Correct 46 ms 156508 KB Output is correct
10 Correct 46 ms 156500 KB Output is correct
11 Correct 49 ms 156552 KB Output is correct
12 Correct 46 ms 156544 KB Output is correct
13 Correct 45 ms 156604 KB Output is correct
14 Correct 46 ms 156496 KB Output is correct
15 Correct 45 ms 156548 KB Output is correct
16 Correct 45 ms 156504 KB Output is correct
17 Correct 45 ms 156504 KB Output is correct
18 Correct 46 ms 156432 KB Output is correct
19 Correct 47 ms 156400 KB Output is correct
20 Correct 46 ms 156496 KB Output is correct
21 Correct 48 ms 156544 KB Output is correct
22 Correct 46 ms 156588 KB Output is correct
23 Correct 46 ms 156548 KB Output is correct
24 Correct 46 ms 156564 KB Output is correct
25 Correct 49 ms 156756 KB Output is correct
26 Correct 47 ms 156592 KB Output is correct
27 Correct 47 ms 156496 KB Output is correct
28 Correct 46 ms 156744 KB Output is correct
29 Correct 46 ms 156496 KB Output is correct
30 Correct 46 ms 156484 KB Output is correct
31 Correct 46 ms 156508 KB Output is correct
32 Correct 46 ms 156508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156564 KB Output is correct
2 Correct 434 ms 213192 KB Output is correct
3 Correct 430 ms 212924 KB Output is correct
4 Correct 449 ms 213308 KB Output is correct
5 Correct 443 ms 217172 KB Output is correct
6 Correct 399 ms 213184 KB Output is correct
7 Correct 452 ms 213184 KB Output is correct
8 Correct 470 ms 217320 KB Output is correct
9 Correct 417 ms 215740 KB Output is correct
10 Correct 456 ms 213436 KB Output is correct
11 Correct 454 ms 215352 KB Output is correct
12 Correct 437 ms 213176 KB Output is correct
13 Correct 454 ms 216980 KB Output is correct
14 Correct 497 ms 217236 KB Output is correct
15 Correct 435 ms 217656 KB Output is correct
16 Correct 428 ms 213116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156564 KB Output is correct
2 Correct 435 ms 214140 KB Output is correct
3 Correct 434 ms 213616 KB Output is correct
4 Correct 464 ms 214568 KB Output is correct
5 Correct 403 ms 213860 KB Output is correct
6 Correct 413 ms 213744 KB Output is correct
7 Correct 518 ms 213604 KB Output is correct
8 Correct 481 ms 213856 KB Output is correct
9 Correct 442 ms 213620 KB Output is correct
10 Correct 464 ms 213512 KB Output is correct
11 Correct 544 ms 214892 KB Output is correct
12 Correct 551 ms 215068 KB Output is correct
13 Correct 554 ms 214504 KB Output is correct
14 Correct 508 ms 213692 KB Output is correct
15 Correct 496 ms 214728 KB Output is correct
16 Correct 509 ms 214328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 497 ms 207700 KB Output is correct
2 Correct 502 ms 209312 KB Output is correct
3 Correct 497 ms 209460 KB Output is correct
4 Correct 574 ms 209536 KB Output is correct
5 Correct 419 ms 209472 KB Output is correct
6 Correct 469 ms 210112 KB Output is correct
7 Correct 425 ms 209772 KB Output is correct
8 Correct 444 ms 209300 KB Output is correct
9 Correct 451 ms 209508 KB Output is correct
10 Correct 405 ms 210116 KB Output is correct
11 Correct 454 ms 209728 KB Output is correct
12 Correct 417 ms 209600 KB Output is correct
13 Correct 400 ms 209228 KB Output is correct
14 Correct 403 ms 209724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 156564 KB Output is correct
2 Correct 48 ms 156540 KB Output is correct
3 Correct 45 ms 156504 KB Output is correct
4 Correct 46 ms 156496 KB Output is correct
5 Correct 47 ms 156516 KB Output is correct
6 Correct 46 ms 156488 KB Output is correct
7 Correct 46 ms 156496 KB Output is correct
8 Correct 44 ms 156516 KB Output is correct
9 Correct 46 ms 156508 KB Output is correct
10 Correct 46 ms 156500 KB Output is correct
11 Correct 49 ms 156552 KB Output is correct
12 Correct 46 ms 156544 KB Output is correct
13 Correct 45 ms 156604 KB Output is correct
14 Correct 46 ms 156496 KB Output is correct
15 Correct 45 ms 156548 KB Output is correct
16 Correct 45 ms 156504 KB Output is correct
17 Correct 45 ms 156504 KB Output is correct
18 Correct 46 ms 156432 KB Output is correct
19 Correct 47 ms 156400 KB Output is correct
20 Correct 46 ms 156496 KB Output is correct
21 Correct 48 ms 156544 KB Output is correct
22 Correct 46 ms 156588 KB Output is correct
23 Correct 46 ms 156548 KB Output is correct
24 Correct 46 ms 156564 KB Output is correct
25 Correct 49 ms 156756 KB Output is correct
26 Correct 47 ms 156592 KB Output is correct
27 Correct 47 ms 156496 KB Output is correct
28 Correct 46 ms 156744 KB Output is correct
29 Correct 46 ms 156496 KB Output is correct
30 Correct 46 ms 156484 KB Output is correct
31 Correct 46 ms 156508 KB Output is correct
32 Correct 46 ms 156508 KB Output is correct
33 Correct 434 ms 213192 KB Output is correct
34 Correct 430 ms 212924 KB Output is correct
35 Correct 449 ms 213308 KB Output is correct
36 Correct 443 ms 217172 KB Output is correct
37 Correct 399 ms 213184 KB Output is correct
38 Correct 452 ms 213184 KB Output is correct
39 Correct 470 ms 217320 KB Output is correct
40 Correct 417 ms 215740 KB Output is correct
41 Correct 456 ms 213436 KB Output is correct
42 Correct 454 ms 215352 KB Output is correct
43 Correct 437 ms 213176 KB Output is correct
44 Correct 454 ms 216980 KB Output is correct
45 Correct 497 ms 217236 KB Output is correct
46 Correct 435 ms 217656 KB Output is correct
47 Correct 428 ms 213116 KB Output is correct
48 Correct 435 ms 214140 KB Output is correct
49 Correct 434 ms 213616 KB Output is correct
50 Correct 464 ms 214568 KB Output is correct
51 Correct 403 ms 213860 KB Output is correct
52 Correct 413 ms 213744 KB Output is correct
53 Correct 518 ms 213604 KB Output is correct
54 Correct 481 ms 213856 KB Output is correct
55 Correct 442 ms 213620 KB Output is correct
56 Correct 464 ms 213512 KB Output is correct
57 Correct 544 ms 214892 KB Output is correct
58 Correct 551 ms 215068 KB Output is correct
59 Correct 554 ms 214504 KB Output is correct
60 Correct 508 ms 213692 KB Output is correct
61 Correct 496 ms 214728 KB Output is correct
62 Correct 509 ms 214328 KB Output is correct
63 Correct 497 ms 207700 KB Output is correct
64 Correct 502 ms 209312 KB Output is correct
65 Correct 497 ms 209460 KB Output is correct
66 Correct 574 ms 209536 KB Output is correct
67 Correct 419 ms 209472 KB Output is correct
68 Correct 469 ms 210112 KB Output is correct
69 Correct 425 ms 209772 KB Output is correct
70 Correct 444 ms 209300 KB Output is correct
71 Correct 451 ms 209508 KB Output is correct
72 Correct 405 ms 210116 KB Output is correct
73 Correct 454 ms 209728 KB Output is correct
74 Correct 417 ms 209600 KB Output is correct
75 Correct 400 ms 209228 KB Output is correct
76 Correct 403 ms 209724 KB Output is correct
77 Correct 498 ms 217240 KB Output is correct
78 Correct 567 ms 217444 KB Output is correct
79 Correct 500 ms 217952 KB Output is correct
80 Correct 566 ms 217140 KB Output is correct
81 Correct 465 ms 216848 KB Output is correct
82 Correct 482 ms 217376 KB Output is correct
83 Correct 470 ms 217104 KB Output is correct
84 Correct 510 ms 216944 KB Output is correct
85 Correct 559 ms 218528 KB Output is correct
86 Correct 479 ms 217308 KB Output is correct
87 Correct 451 ms 218604 KB Output is correct
88 Correct 520 ms 217552 KB Output is correct
89 Correct 443 ms 216592 KB Output is correct
90 Correct 447 ms 217996 KB Output is correct
91 Correct 472 ms 216556 KB Output is correct
92 Correct 486 ms 216656 KB Output is correct
93 Correct 532 ms 216656 KB Output is correct
94 Correct 530 ms 217784 KB Output is correct
95 Correct 461 ms 218208 KB Output is correct
96 Correct 507 ms 216700 KB Output is correct
97 Correct 531 ms 216828 KB Output is correct
98 Correct 422 ms 216444 KB Output is correct
99 Correct 427 ms 217192 KB Output is correct
100 Correct 458 ms 217956 KB Output is correct
101 Correct 459 ms 216276 KB Output is correct
102 Correct 487 ms 218216 KB Output is correct