답안 #796493

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
796493 2023-07-28T12:42:54 Z cig32 Izbori (COCI22_izbori) C++17
110 / 110
776 ms 101468 KB
#include "bits/stdc++.h"
using namespace std;
#define int long long
const int MAXN = 4e5 + 10;
const int MOD = 1e9 + 7;
mt19937_64 rng((int)std::chrono::steady_clock::now().time_since_epoch().count());
int rnd(int x, int y) {
  int u = uniform_int_distribution<int>(x, y)(rng); return u;
}
int bm(int b, int p) {
  if(p==0) return 1 % MOD;
  int r = bm(b, p >> 1);
  if(p&1) return (((r*r) % MOD) * b) % MOD;
  return (r*r) % MOD;
}
int inv(int b) { 
  return bm(b, MOD-2);
}
int fastlog(int x) {
  return (x == 0 ? -1 : 64 - __builtin_clzll(x) - 1);
}
void printcase(int i) { cout << "Case #" << i << ": "; }
struct segtree_basic {
  struct node {
    int lazyval, mi, ma, sum; char lazytag;
    int len; // not changing
  };
  int stok;
  vector<node> st;
  void bu(int l, int r, int idx) {
    st[idx].lazyval = st[idx].mi = st[idx].ma = st[idx].sum = 0;
    st[idx].lazytag = '?';
    st[idx].len = r - l + 1;
    if(l == r) {
      return;
    }
    int mid = (l + r) >> 1;
    bu(l, mid, 2*idx+1);
    bu(mid+1, r, 2*idx+2);
  }
  void push_down(int idx) {
    if(st[idx].lazytag == '?') return;
    if(st[idx].lazytag == ':') {
      st[2*idx+1].lazyval = st[idx].lazyval;
      st[2*idx+1].mi = st[idx].lazyval;
      st[2*idx+1].ma = st[idx].lazyval;
      st[2*idx+1].sum = st[idx].lazyval * st[2*idx+1].len;
      st[2*idx+1].lazytag = ':';
 
      st[2*idx+2].lazyval = st[idx].lazyval;
      st[2*idx+2].mi = st[idx].lazyval;
      st[2*idx+2].ma = st[idx].lazyval;
      st[2*idx+2].sum = st[idx].lazyval * st[2*idx+2].len;
      st[2*idx+2].lazytag = ':';
 
    }
    else {
      st[2*idx+1].lazyval += st[idx].lazyval;
      st[2*idx+1].mi += st[idx].lazyval;
      st[2*idx+1].ma += st[idx].lazyval;
      st[2*idx+1].sum += st[idx].lazyval * st[2*idx+1].len;
      st[2*idx+1].lazytag = (st[2*idx+1].lazytag == ':' ? ':' : '+');
 
      st[2*idx+2].lazyval += st[idx].lazyval;
      st[2*idx+2].mi += st[idx].lazyval;
      st[2*idx+2].ma += st[idx].lazyval;
      st[2*idx+2].sum += st[idx].lazyval * st[2*idx+2].len;
      st[2*idx+2].lazytag = (st[2*idx+2].lazytag == ':' ? ':' : '+');
    }
    st[idx].lazytag = '?';
    st[idx].lazyval = 0;
  }
  void push_up(int idx) {
    st[idx].mi = min(st[2*idx+1].mi, st[2*idx+2].mi);
    st[idx].ma = max(st[2*idx+1].ma, st[2*idx+2].ma);
    st[idx].sum = st[2*idx+1].sum + st[2*idx+2].sum;
  }
  void u1(int l, int r, int constl, int constr, int idx, int val) { // range := v
    if(l <= constl && constr <= r) {
      st[idx].lazyval = val;
      st[idx].mi = val;
      st[idx].ma = val;
      st[idx].sum = val * st[idx].len;
      st[idx].lazytag = ':';
      return;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) u1(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) u1(l, r, constl, mid, 2*idx+1, val);
    else {
      u1(l, r, constl, mid, 2*idx+1, val);
      u1(l, r, mid+1, constr, 2*idx+2, val);
    }
    push_up(idx);
  }
 
  void u2(int l, int r, int constl, int constr, int idx, int val) { // range += v
    if(l <= constl && constr <= r) {
      st[idx].lazyval += val;
      st[idx].mi += val;
      st[idx].ma += val;
      st[idx].sum += val * st[idx].len;
      st[idx].lazytag = (st[idx].lazytag == ':' ? ':' : '+');
      return;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) u2(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) u2(l, r, constl, mid, 2*idx+1, val);
    else {
      u2(l, r, constl, mid, 2*idx+1, val);
      u2(l, r, mid+1, constr, 2*idx+2, val);
    }
    push_up(idx);
  }
 
  int qu1(int l, int r, int constl, int constr, int idx) { // range min
    if(l <= constl && constr <= r) return st[idx].mi;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu1(l, r, mid+1, constr, 2*idx+2);
    else if(constr < l || r < mid + 1) return qu1(l, r, constl, mid, 2*idx+1);
    else {
      return min(qu1(l, r, constl, mid, 2*idx+1), qu1(l, r, mid+1, constr, 2*idx+2));
    }
  }
 
  int qu2(int l, int r, int constl, int constr, int idx) { // range max
    if(l <= constl && constr <= r) return st[idx].ma;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu2(l, r, mid+1, constr, 2*idx+2); 
    else if(constr < l || r < mid + 1) return qu2(l, r, constl, mid, 2*idx+1);
    else {
      return max(qu2(l, r, constl, mid, 2*idx+1), qu2(l, r, mid+1, constr, 2*idx+2));
    }
  }
  int qu3(int l, int r, int constl, int constr, int idx) { // range sum
    if(l <= constl && constr <= r) return st[idx].sum;
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu3(l, r, mid+1, constr, 2*idx+2);
    else if(constr < l || r < mid + 1) return qu3(l, r, constl, mid, 2*idx+1);
    else {
      return qu3(l, r, constl, mid, 2*idx+1) + qu3(l, r, mid+1, constr, 2*idx+2);
    }
  }
  int qu4(int l, int r, int constl, int constr, int idx, int val) { // first at least v
    if(l <= constl && constr <= r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+1].ma >= val) constr = mid, idx = 2*idx + 1;
        else constl = mid+1, idx = 2*idx + 2;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu4(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu4(l, r, constl, mid, 2*idx+1, val);
    else {
      int lchild = qu4(l, r, constl, mid, 2*idx+1, val);
      if(lchild != -1) return lchild;
      return qu4(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu5(int l, int r, int constl, int constr, int idx, int val) { // first at most v
    if(l <= constl && constr <= r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+1].mi <= val) constr = mid, idx = 2*idx + 1;
        else constl = mid+1, idx = 2*idx + 2;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu5(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu5(l, r, constl, mid, 2*idx+1, val);
    else {
      int lchild = qu5(l, r, constl, mid, 2*idx+1, val);
      if(lchild != -1) return lchild;
      return qu5(l, r, mid+1, constr, 2*idx+2, val);
    }
  }
  int qu6(int l, int r, int constl, int constr, int idx, int val) { // last at least v
    if(l <= constl && constr <= r) {
      if(st[idx].ma < val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+2].ma >= val) constl = mid+1, idx = 2*idx + 2;
        else constr = mid, idx = 2*idx + 1;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu6(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu6(l, r, constl, mid, 2*idx+1, val);
    else {
      int rchild = qu6(l, r, mid+1, constr, 2*idx+2, val);
      if(rchild != -1) return rchild;
      return qu6(l, r, constl, mid, 2*idx+1, val);
    }
  }
  int qu7(int l, int r, int constl, int constr, int idx, int val) { // last at most v
    if(l <= constl && constr <= r) {
      if(st[idx].mi > val) return -1;
      while(constl < constr) {
        int mid = (constl + constr) >> 1;
        push_down(idx);
        if(st[2*idx+2].mi <= val) constl = mid+1, idx = 2*idx + 2;
        else constr = mid, idx = 2*idx + 1;
      }
      return constl;
    }
    int mid = (constl + constr) >> 1;
    push_down(idx);
    if(mid < l || r < constl) return qu7(l, r, mid+1, constr, 2*idx+2, val);
    else if(constr < l || r < mid + 1) return qu7(l, r, constl, mid, 2*idx+1, val);
    else {
      int rchild = qu7(l, r, mid+1, constr, 2*idx+2, val);
      if(rchild != -1) return rchild;
      return qu7(l, r, constl, mid, 2*idx+1, val);
    }
  }
  public:
  void resize(int k) {
    st.resize(4*k + 10);
    stok = k;
    bu(0, k, 0);
  }
  void range_assign(int l, int r, int v) { u1(l, r, 0, stok, 0, v); }
 
  void range_add(int l, int r, int v) { u2(l, r, 0, stok, 0, v); }
 
  int query_min(int l, int r) { return qu1(l, r, 0, stok, 0); }
 
  int query_max(int l, int r) { return qu2(l, r, 0, stok, 0); }
 
  int query_sum(int l, int r) { return qu3(l, r, 0, stok, 0); }
 
  int query_firstAtLeast(int l, int r, int v) { return qu4(l, r, 0, stok, 0, v); }
 
  int query_firstAtMost(int l, int r, int v) { return qu5(l, r, 0, stok, 0, v); }
 
  int query_lastAtLeast(int l, int r, int v) { return qu6(l, r, 0, stok, 0, v); } 
 
  int query_lastAtMost(int l, int r, int v) { return qu7(l, r, 0, stok, 0, v); }
};
pair<int, int> st[4*MAXN];
void push_down(int idx, int l, int r) {
  int m = (l + r) >> 1;
  st[(idx<<1)+1].first += st[idx].first;
  st[(idx<<1)+1].second += st[idx].first * (m-l+1) * (l+m) / 2;
  st[(idx<<1)+2].first += st[idx].first;
  st[(idx<<1)+2].second += st[idx].first * (r-m) * (m+1+r) / 2;
  st[idx].first = 0;
  return;
}
void push_up(int idx) {
  st[idx].second = st[(idx<<1)+1].second + st[(idx<<1)+2].second;
}
void u(int l, int r, int constl, int constr, int idx, int val) {
  if(l <= constl && constr <= r) {
    st[idx].first += val;
    st[idx].second += val * (constr - constl + 1) * (constl + constr) / 2;
    return;
  }
  int mid = (constl + constr) >> 1;
  push_down(idx, constl, constr);
  if(mid < l || r < constl) u(l, r, mid+1, constr, (idx<<1)+2, val);
  else if(constr < l || r < mid+1) u(l, r, constl, mid, (idx<<1)+1, val);
  else {
    u(l, r, constl, mid, (idx<<1)+1, val);
    u(l, r, mid+1, constr, (idx<<1)+2, val);
  }
  push_up(idx);
}
int qu(int l, int r, int constl, int constr, int idx) {
  if(l <= constl  &&  constr <= r) return st[idx].second;
  int mid = (constl + constr) >> 1;
  push_down(idx, constl, constr);
  if(mid < l || r < constl) return qu(l, r, mid+1, constr, (idx<<1)+2);
  else if(constr < l || r < mid+1) return qu(l, r, constl, mid, (idx<<1)+1);
  else {
    return qu(l, r, constl, mid, (idx<<1)+1) + qu(l, r, mid+1, constr, (idx<<1)+2);
  }
}
segtree_basic ps;
int n;
int weighted_sum(int l, int r) {
  int bruh = qu(l, r, 0, 2*n, 0);
  bruh *= -1;
  bruh += ps.query_sum(l, r) * (r + 1);
  return bruh;
}
void range_add(int l, int r, int v) {
  u(l, r, 0, 2*n, 0, v);
}
void solve(int tc) {
  cin>>n;
  int a[n+1];
  for(int i=1;i<=n;i++)cin>>a[i];
  set<int>st;
  for(int i=1;i<=n;i++)st.insert(a[i]);
  unordered_map<int,int>in;
  int it=0;
  for(int x:st)in[x]=++it;
  for(int i=1;i<=n;i++)a[i]=in[a[i]];
  vector<int>b[n+1];
  for(int i=1;i<=n;i++)b[a[i]].push_back(i);
  int ans=0;
  ps.resize(2*n);
  for(int i=1;i<=n;i++){
    if(b[i].empty())continue;
    vector<pair<pair<int, int>, int> > v;
    int s=-(b[i][0]-1);
    ps.range_assign(0,2*n,0);
    ps.range_add(n+s,n,1);
    range_add(n+s,n,1);
    v.push_back({{n+s, n}, 1});
    for(int j=0;j<b[i].size();j++){
      int c=(j+1==b[i].size()?n:b[i][j+1]-1)-b[i][j];
      s++;
      if(n+s-c-1>=0) ans+=ps.query_sum(0,n+s-c-1)*(c+1);
      if(n+s-1 >= 0 && c>0) ans+=weighted_sum(max(0ll, n+s-c), n+s-1);
      //for(int k=1; k<=c; k++) if(n+s-k>=0) ans+=ps.query_sum(n+s-k,n+s-k)*k;
      ps.range_add(n+s-c,n+s,1);
      range_add(n+s-c, n+s, 1);
      v.push_back({{n+s-c, n+s}, 1});
      s-=c;
    }
    for(auto x: v) range_add(x.first.first, x.first.second, -x.second);
  }
  cout<<ans<<'\n';
}
int32_t main() {
  ios::sync_with_stdio(0); cin.tie(0);
  int t = 1; //cin >> t;
  for(int i=1; i<=t; i++) solve(i);
}
// 勿忘初衷

Compilation message

Main.cpp: In function 'void solve(long long int)':
Main.cpp:329:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  329 |     for(int j=0;j<b[i].size();j++){
      |                 ~^~~~~~~~~~~~
Main.cpp:330:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  330 |       int c=(j+1==b[i].size()?n:b[i][j+1]-1)-b[i][j];
      |              ~~~^~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 1256 KB Output is correct
10 Correct 3 ms 1236 KB Output is correct
11 Correct 3 ms 1240 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 3 ms 1236 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 3 ms 1240 KB Output is correct
16 Correct 3 ms 1236 KB Output is correct
17 Correct 2 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 135 ms 60396 KB Output is correct
2 Correct 179 ms 78012 KB Output is correct
3 Correct 102 ms 45196 KB Output is correct
4 Correct 185 ms 81532 KB Output is correct
5 Correct 195 ms 84428 KB Output is correct
6 Correct 196 ms 89172 KB Output is correct
7 Correct 189 ms 89152 KB Output is correct
8 Correct 202 ms 89412 KB Output is correct
9 Correct 193 ms 89152 KB Output is correct
10 Correct 193 ms 89196 KB Output is correct
11 Correct 174 ms 98148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 460 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 1 ms 468 KB Output is correct
7 Correct 2 ms 852 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 3 ms 1256 KB Output is correct
10 Correct 3 ms 1236 KB Output is correct
11 Correct 3 ms 1240 KB Output is correct
12 Correct 3 ms 1236 KB Output is correct
13 Correct 3 ms 1236 KB Output is correct
14 Correct 4 ms 1236 KB Output is correct
15 Correct 3 ms 1240 KB Output is correct
16 Correct 3 ms 1236 KB Output is correct
17 Correct 2 ms 1364 KB Output is correct
18 Correct 135 ms 60396 KB Output is correct
19 Correct 179 ms 78012 KB Output is correct
20 Correct 102 ms 45196 KB Output is correct
21 Correct 185 ms 81532 KB Output is correct
22 Correct 195 ms 84428 KB Output is correct
23 Correct 196 ms 89172 KB Output is correct
24 Correct 189 ms 89152 KB Output is correct
25 Correct 202 ms 89412 KB Output is correct
26 Correct 193 ms 89152 KB Output is correct
27 Correct 193 ms 89196 KB Output is correct
28 Correct 174 ms 98148 KB Output is correct
29 Correct 207 ms 99732 KB Output is correct
30 Correct 121 ms 22212 KB Output is correct
31 Correct 268 ms 43808 KB Output is correct
32 Correct 776 ms 101468 KB Output is correct
33 Correct 410 ms 46176 KB Output is correct
34 Correct 398 ms 48136 KB Output is correct
35 Correct 247 ms 31320 KB Output is correct
36 Correct 137 ms 19828 KB Output is correct
37 Correct 170 ms 23448 KB Output is correct
38 Correct 265 ms 91276 KB Output is correct
39 Correct 268 ms 91188 KB Output is correct
40 Correct 293 ms 91192 KB Output is correct
41 Correct 288 ms 91192 KB Output is correct
42 Correct 247 ms 91172 KB Output is correct
43 Correct 365 ms 93480 KB Output is correct
44 Correct 300 ms 93668 KB Output is correct
45 Correct 330 ms 93456 KB Output is correct
46 Correct 368 ms 93508 KB Output is correct
47 Correct 326 ms 93560 KB Output is correct
48 Correct 472 ms 92732 KB Output is correct
49 Correct 442 ms 92732 KB Output is correct
50 Correct 425 ms 93276 KB Output is correct
51 Correct 455 ms 93508 KB Output is correct
52 Correct 413 ms 92556 KB Output is correct
53 Correct 444 ms 92364 KB Output is correct
54 Correct 396 ms 92520 KB Output is correct