QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#761621#5223. 线段树hos_lyric100 ✓315ms5364kbC++145.8kb2024-11-19 02:15:532024-11-19 02:15:53

Judging History

你现在查看的是最新测评结果

  • [2024-11-19 02:15:53]
  • 评测
  • 测评结果:100
  • 用时:315ms
  • 内存:5364kb
  • [2024-11-19 02:15:53]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")

////////////////////////////////////////////////////////////////////////////////
template <unsigned M_> struct ModInt {
  static constexpr unsigned M = M_;
  unsigned x;
  constexpr ModInt() : x(0U) {}
  constexpr ModInt(unsigned x_) : x(x_ % M) {}
  constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
  constexpr ModInt(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
  constexpr ModInt(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
  ModInt &operator+=(const ModInt &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
  ModInt &operator-=(const ModInt &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
  ModInt &operator*=(const ModInt &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
  ModInt &operator/=(const ModInt &a) { return (*this *= a.inv()); }
  ModInt pow(long long e) const {
    if (e < 0) return inv().pow(-e);
    ModInt a = *this, b = 1U; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
  }
  ModInt inv() const {
    unsigned a = M, b = x; int y = 0, z = 1;
    for (; b; ) { const unsigned q = a / b; const unsigned c = a - q * b; a = b; b = c; const int w = y - static_cast<int>(q) * z; y = z; z = w; }
    assert(a == 1U); return ModInt(y);
  }
  ModInt operator+() const { return *this; }
  ModInt operator-() const { ModInt a; a.x = x ? (M - x) : 0U; return a; }
  ModInt operator+(const ModInt &a) const { return (ModInt(*this) += a); }
  ModInt operator-(const ModInt &a) const { return (ModInt(*this) -= a); }
  ModInt operator*(const ModInt &a) const { return (ModInt(*this) *= a); }
  ModInt operator/(const ModInt &a) const { return (ModInt(*this) /= a); }
  template <class T> friend ModInt operator+(T a, const ModInt &b) { return (ModInt(a) += b); }
  template <class T> friend ModInt operator-(T a, const ModInt &b) { return (ModInt(a) -= b); }
  template <class T> friend ModInt operator*(T a, const ModInt &b) { return (ModInt(a) *= b); }
  template <class T> friend ModInt operator/(T a, const ModInt &b) { return (ModInt(a) /= b); }
  explicit operator bool() const { return x; }
  bool operator==(const ModInt &a) const { return (x == a.x); }
  bool operator!=(const ModInt &a) const { return (x != a.x); }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////

constexpr unsigned MO = 1000000007;
using Mint = ModInt<MO>;


int s1(int n) {
  return n*(n+1)/2;
}

int N, Q;
vector<int> A;

Mint dp[2][410][410];

int main() {
  for (; ~scanf("%d%d", &N, &Q); ) {
    A.assign(N + 2, 0);
    for (int i = 1; i <= N; ++i) {
      scanf("%d", &A[i]);
    }
    
    const Mint all = Mint(s1(N)).pow(Q);
    
    vector<int> as(A.begin() + 1, A.begin() + (N + 1));
    sort(as.begin(), as.end());
    
    for (int l = 0; l <= N + 1; ++l) for (int r = l + 1; r <= N + 1; ++r) dp[0][l][r] = 0;
    for (int h = 1; h < N; ++h) {
      /*
        0: <  as[h]
        1: >= as[h]
        subtract [< as[h]]
      */
      int l = 0;
      for (int r = 1; r <= N + 1; ++r) if (r == N + 1 || A[r] >= as[h]) {
        dp[0][l][r] += (as[h] - as[h - 1]);
        l = r;
      }
    }
    
    /*
      l       r
      100...001
    */
    for (int q = 0; q < Q; ++q) {
      auto crt = dp[q & 1], nxt = dp[(q + 1) & 1];
      for (int l = 0; l <= N + 1; ++l) for (int r = l + 1; r <= N + 1; ++r) nxt[l][r] = 0;
      for (int l = 0; l <= N + 1; ++l) for (int r = l + 1; r <= N + 1; ++r) {
        nxt[l][r] += crt[l][r] * (s1(l - 1) + s1(r - l - 1) + s1(N - r));
        // for (int ll = l; ll < r; ++ll) nxt[ll][r] += crt[l][r] * l;
        // for (int rr = r; rr > l; --rr) nxt[l][rr] += crt[l][r] * (N + 1 - r);
      }
      for (int r = 0; r <= N + 1; ++r) {
        Mint sum = 0;
        for (int l = 0; l < r; ++l) nxt[l][r] += sum += crt[l][r] * l;
      }
      for (int l = 0; l <= N + 1; ++l) {
        Mint sum = 0;
        for (int r = N + 1; r > l; --r) nxt[l][r] += sum += crt[l][r] * (N + 1 - r);
      }
    }
    vector<Mint> ans(N + 2, 0);
    for (int l = 0; l <= N + 1; ++l) for (int r = l + 1; r <= N + 1; ++r) {
      ans[l + 1] += dp[Q & 1][l][r];
      ans[r    ] -= dp[Q & 1][l][r];
    }
    for (int i = 1; i <= N; ++i) ans[i + 1] += ans[i];
    
    for (int i = 1; i <= N; ++i) ans[i] = as[N - 1] * all - ans[i];
    
    for (int i = 1; i <= N; ++i) {
      if (i > 1) printf(" ");
      printf("%u", ans[i].x);
    }
    puts("");
  }
  return 0;
}

详细


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 0ms
memory: 4144kb

input:

5 5
848274236 769010281 980719447 464370035 767894862

output:

553166117 746383641 824852494 844559284 946269984

result:

ok 5 number(s): "553166117 746383641 824852494 844559284 946269984"

Test #2:

score: 10
Accepted
time: 0ms
memory: 3840kb

input:

8 400
614955762 836299319 805391502 271803584 779246508 946874165 140366227 909445992

output:

548023716 275366510 158690431 707146199 493342738 356785601 931834441 450735828

result:

ok 8 numbers

Test #3:

score: 10
Accepted
time: 1ms
memory: 3860kb

input:

12 400
971346475 963946917 444454491 318143662 685007420 61143719 62749485 880636401 71002474 659777096 524617255 278289268

output:

174821805 553588095 387413015 521954811 176969548 107520550 646465869 194524853 660747788 351668809 328623015 808871844

result:

ok 12 numbers

Test #4:

score: 10
Accepted
time: 0ms
memory: 3916kb

input:

30 400
231802993 655163372 314861357 212108667 808862225 29748096 778893093 795239034 244537043 99512888 739688278 621716754 750055158 436062396 334391047 818933876 257455408 993880865 179262624 91293728 507171541 396749477 562834154 519090703 76211211 446096130 750398368 790810497 582589895 209489587

output:

392645973 914983713 990788962 694253783 248617779 399977992 534900739 802049343 130585100 954021410 921091443 591941160 278539615 327802952 488446697 227347833 523339036 231790260 685800536 176562442 587878957 685872039 690895020 203160073 675299761 39555691 927846827 317547875 872845970 497454073

result:

ok 30 numbers

Test #5:

score: 10
Accepted
time: 6ms
memory: 4024kb

input:

50 400
910803743 912164462 553705700 448436994 692055368 12785313 74343666 596657572 204278008 249420689 12283829 849666473 296923963 476156914 845795434 137543338 720909861 435810396 102505620 672100447 348893359 797505187 86352103 450199118 926600674 365450376 937880768 726875062 833583424 9064639...

output:

651568031 687470319 158789316 566974591 884818502 115105004 216744029 125932756 981347967 855765635 375837924 670641459 754809539 660338168 327570623 896684754 128763127 980253711 426969767 11433178 703043503 118564550 431088904 86763074 673847220 633745897 438142748 729502963 23359780 644133434 213...

result:

ok 50 numbers

Test #6:

score: 10
Accepted
time: 21ms
memory: 4100kb

input:

100 400
730715553 20593474 897165208 921521683 641549667 417447582 271208996 679110571 36132209 714863315 861590989 863420901 980444352 629978845 815169313 554647767 492714520 431134145 352626807 22611062 689833557 639979196 693463513 576881859 739892865 251006589 132056170 276889225 715365226 40902...

output:

37255344 223860545 239769044 30950469 285484488 521446715 442243381 117990039 436073061 527113135 611892031 859014976 983688395 859739096 412146819 640099870 520789307 129454437 819804779 434991462 735576301 704935448 871264920 766991509 649877667 94463994 486410797 865099960 536543253 143989284 403...

result:

ok 100 numbers

Test #7:

score: 10
Accepted
time: 21ms
memory: 4148kb

input:

100 400
205461216 522648155 862486941 233446938 844043949 262249860 85455944 288399138 371858466 935403922 581276320 521211467 701082957 358317936 229077795 680291549 565767034 107776612 421559368 281246183 102863250 495169609 957897372 522854399 5906033 878474530 679102412 396162430 995015713 45702...

output:

493525732 995729856 995658846 853176291 532157626 347145260 689155573 509848496 16991913 116406883 921314485 264737344 189376093 849759938 71542991 196014838 463477092 388870933 982570432 135168968 85013949 861062509 477508625 625758211 640875815 545437111 33480610 137178059 714278303 635860654 9275...

result:

ok 100 numbers

Test #8:

score: 10
Accepted
time: 315ms
memory: 5364kb

input:

400 400
689118017 75526777 91888205 930494840 985873068 167160671 446962704 739738841 608129832 998387701 594237004 891197021 431717552 103885633 579588512 469998081 945977707 962230087 991635348 906096926 249671089 410521364 354124681 491153772 950058294 507949774 823599756 139183448 41364838 74444...

output:

426129006 237282826 621279509 62452286 130551287 422474322 787699827 442110728 3400831 173204260 353460054 631668700 918675659 156646882 733008327 733688268 793063429 229151277 402970741 927020240 444166323 906589126 329499673 900677315 344341990 684017674 475778468 714361560 284419649 999723375 768...

result:

ok 400 numbers

Test #9:

score: 10
Accepted
time: 312ms
memory: 5108kb

input:

400 400
38470323 587834796 181149151 846710898 484978486 313194286 476086734 748718047 882526140 950760967 122354170 778383548 479870055 976089526 446923396 202836162 927043461 170903301 638015249 70462613 496794379 150318157 13259515 525910318 944013939 612990334 864034184 396500272 641888827 91615...

output:

646146123 772875723 783614424 663547785 224991578 318175345 609560622 978904753 793709059 671257708 254389136 638116423 179594155 739367028 367489236 812961899 422972569 789787885 756353338 902924939 450312709 986346386 994346128 408114290 842700779 442630937 485180165 807743880 926086682 146688170 ...

result:

ok 400 numbers

Test #10:

score: 10
Accepted
time: 314ms
memory: 5128kb

input:

400 400
903458109 853233243 303026073 620703926 219072987 859903613 638691286 922907212 862238018 576453357 149520051 612938185 443930725 924876693 751368321 425995308 581400283 971198525 580432561 942545003 711304901 858879200 355942169 713214802 325087175 973940731 823036070 469595690 943430004 47...

output:

968138392 455719681 154394848 644817604 820743994 253134870 670382261 87500363 654873113 199288623 92648023 207118844 70704773 439935007 228855555 528560645 605253667 86146183 743653176 791615071 569728833 572224177 587831345 813208184 394829306 906309572 715229304 470211149 478117126 806769792 6145...

result:

ok 400 numbers

Extra Test:

score: 0
Extra Test Passed