QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#78203#1823. Permutation CFGhos_lyricAC ✓330ms36932kbC++146.3kb2023-02-17 11:25:012023-02-17 11:25:02

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-02-17 11:25:02]
  • 评测
  • 测评结果:AC
  • 用时:330ms
  • 内存:36932kb
  • [2023-02-17 11:25:01]
  • 提交

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 <map>
#include <numeric>
#include <queue>
#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; }


template <class T> void bAdd(vector<T> &bit, int pos, const T &val) {
  const int bitN = bit.size();
  for (int x = pos; x < bitN; x |= x + 1) bit[x] += val;
}
template <class T> T bSum(const vector<T> &bit, int pos) {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) ret += bit[x];
  return ret;
}
template <class T> T bSum(const vector<T> &bit, int pos0, int pos1) {
  return bSum(bit, pos1) - bSum(bit, pos0);
}

// min pos s.t. pred(sum of [0, pos))
//   assume pred(sum of [0, pos)) is non-decreasing
template <class T, class Pred>
int bBinarySearch(const vector<T> &bit, Pred pred) {
  if (pred(T(0))) return 0;
  const int bitLen = bit.size();
  int pos = 0;
  T sum = 0;
  for (int e = 31 - __builtin_clz(bitLen); e >= 0; --e) {
    const int x = (pos | 1 << e) - 1;
    if (x < bitLen && !pred(sum + bit[x])) {
      pos |= 1 << e;
      sum += bit[x];
    }
  }
  return pos + 1;
}


constexpr Int INF = 1001001001001LL;

/*
  expand p
  count k
  prefix (0, a]
*/
struct Query {
  int p;
  int k;
  Int a;
};

int N, S, Q;
vector<int> P;
vector<int> K;
vector<Int> A;

// len from (s, p)
Int lss[6][100'010];

__int128 bit[100'010][6];

int main() {
  __int128 fs[6];
  
  for (; ~scanf("%d%d%d", &N, &S, &Q); ) {
    P.assign(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
      scanf("%d", &P[i]);
    }
    K.resize(Q);
    A.resize(Q);
    for (int q = 0; q < Q; ++q) {
      scanf("%d%lld", &K[q], &A[q]);
    }
    
    fill(lss[0] + 1, lss[0] + (N + 1), 1);
    for (int s = 1; s <= S; ++s) {
      for (int n = 1; n <= N; ++n) {
        lss[s][n] = min(lss[s][n - 1] + lss[s - 1][n], INF);
      }
// cerr<<"lss["<<s<<"] = ";pv(lss[s],lss[s]+N+1);
    }
    
    vector<int> invP(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
      invP[P[i]] = i;
    }
    
    vector<Int> ans(Q, 0);
    vector<Query> qrys(Q);
    for (int q = 0; q < Q; ++q) {
      qrys[q] = {N, K[q], A[q]};
    }
    // s+1 -> s
    for (int s = S; --s >= 0; ) {
// cerr<<"s = "<<s<<endl;
      vector<int> is(Q);
      vector<vector<pair<int, int>>> events(N + 1);
      {
        vector<vector<int>> qss(N + 1);
        for (int q = 0; q < Q; ++q) {
          qss[qrys[q].p].push_back(q);
        }
        vector<Int> bitL(N + 1);
        for (int p = 1; p <= N; ++p) {
// cerr<<"  add p="<<p<<" at "<<invP[p]<<"; "<<lss[s][p]<<endl;
          bAdd(bitL, invP[p], lss[s][p]);
          for (const int q : qss[p]) {
            is[q] = bBinarySearch(bitL, [&](Int l) -> bool {
              return (l >= qrys[q].a);
            }) - 1;
// cerr<<"  is["<<q<<"] = "<<is[q]<<endl;
            assert(is[q] <= N);
            const int k = qrys[q].k;
            if (k <= p) {
              if (s == 0) {
                if (invP[k] <= is[q]) {
                  ans[q] += 1;
                }
              } else {
                events[k].emplace_back(q, +1);
                if (p + 1 <= N) {
                  events[p + 1].emplace_back(q, -1);
                }
              }
            }
            qrys[q].p = P[is[q]];
            qrys[q].a -= bSum(bitL, is[q]);
          }
        }
      }
      if (s >= 1) {
        for (int i = 0; i <= N; ++i) {
          fill(bit[i], bit[i] + s, 0);
        }
        for (int j = N; j >= 1; --j) {
          /*
            multichoose(s, j - X)
            = binom(s + j - X - 1, s - 1)
          */
          Int denom = 1;
          fill(fs, fs + s, 0);
          fs[0] = 1;
          for (int h = 0; h < s - 1; ++h) {
            denom *= (1 + h);
            // *= (-X + s+j-1 - h)
            for (int d = h; d >= 0; --d) {
              fs[d + 1] -= fs[d];
              fs[d] *= (s + j - 1 - h);
            }
          }
// cerr<<"  j = "<<j<<"; fs = ";pv(fs,fs+s);
          for (int x = invP[j]; x < N + 1; x |= x + 1) {
            for (int d = 0; d < s; ++d) {
              bit[x][d] += fs[d];
            }
          }
          for (const auto &event : events[j]) {
            const int q = event.first;
            const int k = qrys[q].k;
            // X = k
            fill(fs, fs + s, 0);
            for (int x = is[q]; x > 0; x &= x - 1) {
              for (int d = 0; d < s; ++d) {
                fs[d] += bit[x - 1][d];
              }
            }
            __int128 val = 0;
            for (int d = s; --d >= 0; ) {
              (val *= k) += fs[d];
            }
            val /= denom;
            ans[q] += event.second * val;
          }
        }
      }
// cerr<<"ans = "<<ans<<endl;
    }
    
    for (int q = 0; q < Q; ++q) {
      printf("%lld\n", ans[q]);
    }
#ifdef LOCAL
vector<int>crt{N};
for(int s=0;s<S;++s){
 vector<int>nxt;
 for(const int j:crt)for(int i=1;i<=N;++i)if(P[i]<=j)nxt.push_back(P[i]);
 crt=nxt;
}
vector<Int>brt(Q,0);
for(int q=0;q<Q;++q){
 assert(A[q]<=(int)crt.size());
 for(int a=0;a<A[q];++a)if(crt[a]==K[q])++brt[q];
}
if(brt!=ans){
 cerr<<"N = "<<N<<", S = "<<S<<", Q = "<<Q<<endl;
 cerr<<"P = "<<P<<endl;
 cerr<<"K = "<<K<<endl;
 cerr<<"A = "<<A<<endl;
 cerr<<"brt = "<<brt<<endl;
 cerr<<"ans = "<<ans<<endl;
}
assert(brt==ans);
#endif
  }
  return 0;
}

详细

Test #1:

score: 100
Accepted
time: 3ms
memory: 5688kb

input:

4 3 6
1
4
3
2
1 6
2 20
4 1
3 5
2 9
1 16

output:

3
6
0
1
2
8

result:

ok 6 numbers

Test #2:

score: 0
Accepted
time: 281ms
memory: 36732kb

input:

100000 5 200000
98129
52962
60307
83804
75634
55811
85666
40965
78529
40850
54600
83058
37001
92436
30323
54632
24238
77437
87162
75991
39863
74990
32168
99841
85430
66056
69893
7561
60704
40795
81535
89902
44331
20995
99461
93620
91817
97370
84025
98836
2455
77447
37078
90192
26249
84337
70311
4006...

output:

20217
9885
20204
20217
20218
20217
20217
727
20202
20218
20218
20214
20217
7790
20217
20217
20200
20217
20218
20217
20218
20218
20218
20217
20202
20217
20218
20218
5609
8419
20218
20200
20218
20216
11843
20218
20217
20207
20217
935
20203
5909
20218
20217
20218
20200
20217
20213
20203
20207
5654
4087...

result:

ok 200000 numbers

Test #3:

score: 0
Accepted
time: 290ms
memory: 36884kb

input:

100000 5 200000
59767
75967
97372
50276
20532
77164
30969
40418
84175
87555
79967
28404
26422
22441
4543
72719
9318
93986
12744
88814
25854
93658
9042
41389
24133
60155
80340
44920
58271
50431
92622
28573
30633
318
43850
78809
69750
67699
17767
8454
2543
26572
52552
77502
24901
94119
87156
19368
394...

output:

33305
33330
19455
33332
25500
1777
33332
33310
33337
33333
33297
33331
33337
33330
33317
33304
33332
33336
33331
33337
33335
5611
33334
33337
33333
33331
33337
33307
33334
22446
31995
27049
33337
5351
32269
33332
33334
33331
33299
33331
33334
24072
21451
33331
33338
33332
33331
33331
33319
23356
333...

result:

ok 200000 numbers

Test #4:

score: 0
Accepted
time: 286ms
memory: 36696kb

input:

97997 5 200000
92883
53079
12146
7142
47500
47118
60176
54625
8908
93576
33824
61522
79201
68956
25790
76970
63243
10575
33345
20943
77076
40068
30980
20739
90036
57454
38446
49088
90613
27293
39453
52577
94545
7934
73793
6201
33713
91255
45678
63783
65019
35224
65407
39863
38120
79943
69106
76540
6...

output:

21513
21504
21512
21513
21512
19613
21418
21511
21512
6191
21512
18462
4097
21511
21511
21510
21499
21502
21513
21512
21499
21512
21512
21513
21512
21512
21501
21512
10071
21513
21510
21511
21513
21512
21511
21498
21512
21511
21497
21513
21512
21512
21513
21512
21491
21513
16589
21508
21511
21500
20...

result:

ok 200000 numbers

Test #5:

score: 0
Accepted
time: 234ms
memory: 34404kb

input:

97997 5 200000
1
2
3
4
5
6
7
8
9
10
97997
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
...

output:

36269
44641
44708
44743
44700
45423
39594
44743
44405
44918
43915
44740
44706
28640
44655
44062
45423
22483
18051
44678
44757
44686
44695
44645
44830
44723
44700
35460
37180
43835
18485
40508
33475
45038
44723
44665
41482
44778
44633
44714
41636
45203
43011
42876
40377
41790
45709
43725
44184
38340
...

result:

ok 200000 numbers

Test #6:

score: 0
Accepted
time: 230ms
memory: 35480kb

input:

100000 4 200000
57295
10377
65950
19742
78625
70832
98216
11307
9435
8964
76095
1478
46489
61182
52528
10230
50341
83069
94103
42263
18143
52203
16940
68520
76816
98586
96583
28873
59764
20859
85162
22208
33614
52858
48657
87020
40526
40052
31847
30496
89165
52856
35045
71923
20595
57732
33398
21247...

output:

34970
34961
34984
34987
34983
34964
34976
34929
34980
34960
34952
34931
34985
34974
34942
34917
34983
34971
34989
34956
34976
34961
34934
34985
34942
34965
34989
34986
34975
34985
34932
34980
34987
34987
34981
34984
34987
34985
34963
34989
34953
34958
34975
34964
34927
34980
34969
34985
34944
34966
...

result:

ok 200000 numbers

Test #7:

score: 0
Accepted
time: 330ms
memory: 36932kb

input:

100000 5 200000
65145
22925
57683
48950
47
95014
89971
86419
68041
34514
54368
9070
62968
12058
2395
12953
52181
44707
15141
42970
42586
24500
7627
11589
2549
58411
97779
75300
26698
28015
65395
73656
93413
6314
67115
29279
47001
60377
25606
34502
91013
49546
42758
59346
59698
46487
44285
25907
1322...

output:

10900
0
22894
30657
9289
30701
30702
30701
30695
30695
30392
439
30491
30304
30696
30481
30355
30701
4108
0
30648
24531
0
6293
4639
8441
8856
30702
23474
30417
30634
8671
30702
30703
30703
30287
18522
8635
21217
0
30690
1205
30700
30684
13422
30680
5336
30459
6987
18437
3330
30686
20754
30702
30291
...

result:

ok 200000 numbers

Test #8:

score: 0
Accepted
time: 136ms
memory: 31496kb

input:

100000 2 200000
58563
67048
17358
71024
31126
36383
27513
94884
69133
65286
93712
27672
17645
50949
98355
2372
66546
2103
98500
62555
45884
64147
34587
58277
2701
14330
80577
48408
94920
43732
23763
39719
18461
34100
35853
73175
64450
77407
54961
17327
5905
22460
81493
59652
22032
32035
61479
57623
...

output:

7939
20142
20133
20156
20157
20158
20155
6571
17959
20148
20134
20155
18873
20152
20158
20131
20140
20140
20150
20138
20156
20157
20142
20156
20156
20158
20158
20147
20146
20156
20157
20156
20157
20133
20135
20153
20133
20142
20145
20156
20134
20150
20157
12025
20158
20157
20156
20145
20148
20157
18...

result:

ok 200000 numbers

Test #9:

score: 0
Accepted
time: 169ms
memory: 20076kb

input:

1000 5 200000
26
949
329
984
769
794
982
993
558
219
670
13
247
251
931
678
557
142
24
539
903
787
436
731
536
29
579
963
365
87
546
912
592
620
129
637
676
945
588
386
438
482
300
43
67
205
274
68
551
71
478
95
260
458
636
414
368
953
586
856
944
914
114
853
444
621
263
631
605
187
201
960
94
764
5...

output:

3703241
4008426
4089839
4001808
3246557
4014241
3989395
391064
4059813
4001808
3784896
4021324
3700469
4077139
4089854
3820273
3588482
3151986
4077116
4014232
3915999
3079302
3820273
4073494
4089846
3791425
4001808
3691479
4051829
3977074
4013302
2581861
3268414
1855291
3726455
4084881
4064457
40267...

result:

ok 200000 numbers

Test #10:

score: 0
Accepted
time: 54ms
memory: 14164kb

input:

1000 1 200000
301
253
940
560
437
708
946
42
29
56
398
103
526
147
238
264
613
507
635
734
110
625
220
186
795
790
189
306
755
923
980
579
572
22
577
530
833
574
722
803
954
159
644
700
657
761
806
225
300
179
471
951
463
1
217
889
379
43
739
709
976
139
817
32
837
243
486
152
950
182
675
972
666
36...

output:

1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
...

result:

ok 200000 numbers

Test #11:

score: 0
Accepted
time: 0ms
memory: 5736kb

input:

3 3 3
2
3
1
1 1
2 5
1 2

output:

0
2
1

result:

ok 3 number(s): "0 2 1"