Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#455#81. 「NOIP2017」列队Pigsyy1001873ms24404kbC++233.5kb2025-04-24 13:53:472025-04-24 13:53:47

answer

#include <cstdio>
#include <cstring>
#include <algorithm>
#define F(i,a,b) for(int i=a;i<=b;++i)
#define dF(i,a,b) for(int i=a;i>=b;--i)
#define F2(i,a,b) for(int i=a;i<b;++i)
#define getchar() (SS==TT&&(TT=(SS=BB)+fread(BB,1,1<<15,stdin),TT==SS)?EOF:*SS++)
#define RR register
char BB[1 << 15], *SS = BB, *TT = BB;
inline int read() {
    RR int x;
    RR bool f;
    RR char c;

    for (f = 0; (c = getchar()) < '0' || c > '9'; f = c == '-');

    for (x = c - '0'; (c = getchar()) >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0');

    return f ? -x : x;
}
using namespace std;
int q, I[300010];
long long n, m, a[300010], b[300010];
inline bool cmp(int p1, int p2) {
    return a[p1] == a[p2] ? p1 < p2 : a[p1] < a[p2];
}
int h[300010], len[300010], len2[300010], bit[900010];
long long arr[900010];
long long Ans[300010];
inline void Ins(int *array, int siz, int i, int x) {
    for (; i <= siz; array[i] += x, i += i & -i)
        ;
}
inline int binary(int *array, int siz, int x) {
    int l = 1, r, mid, sum, ans;

    while (l <= siz && array[l] < x)
        l <<= 1, ans = l;

    r = l;
    sum = array[l >>= 1];

    while (l < r - 1) {
        mid = l + r >> 1;

        if (mid > siz || array[mid] + sum >= x)
            r = mid, ans = mid;
        else
            l = mid, sum += array[l];
    }

    ans = r;
    return ans;
}
int stk[300001], top;
int main() {
    n = read(), m = read(), q = read();
    F(i, 1, q) a[i] = read(), b[i] = read(), I[i] = i;
    sort(I + 1, I + q + 1, cmp);
    F(i, 1, m - 1) Ins(bit, m - 1, i, 1);
    F(i, 1, n) len[i] = m - 1;
    F(i, 1, q) {
        if (a[I[i - 1]] != a[I[i]])
            while (top)
                Ins(bit, m - 1, stk[top--], 1);

        if (b[I[i]] > len[a[I[i]]])
            continue;

        int pos = binary(bit, m - 1, b[I[i]]);
        Ans[I[i]] = (a[I[i]] - 1) * m + pos;
        Ins(bit, m - 1, pos, -1);
        stk[++top] = pos;
        --len[a[I[i]]];
    }
    int iter = 0;
    F(i, 1, n) {
        while (iter <= q && a[I[iter]] < i)
            ++iter;

        h[i] = iter - 1;
    }
    h[n + 1] = q;
    memset(bit, 0, sizeof bit);
    F(i, 1, n) len[i] = 0, len2[i] = m - 1;
    len[n + 1] = n;
    F(i, 1, n) Ins(bit + h[n + 1], n + q, i, 1), arr[q + i] = i * m;
    F(i, 1, q) {
        if (Ans[i]) {
            int pos = binary(bit + h[n + 1], n + q, a[i]);
            Ins(bit + h[n + 1], n + q, pos, -1);
            Ins(bit + h[n + 1], n + q, ++len[n + 1], 1);
            arr[h[n + 1] + len[n + 1]] = Ans[i];
            Ins(bit + h[a[i]], h[a[i] + 1] - h[a[i]], ++len[a[i]], 1);
            arr[h[a[i]] + len[a[i]]] = arr[h[n + 1] + pos];
            --len2[a[i]];
        } else {
            int pos = binary(bit + h[n + 1], n + q, a[i]);
            Ins(bit + h[n + 1], n + q, pos, -1);
            Ins(bit + h[n + 1], n + q, ++len[n + 1], 1);

            if (b[i] != m) {
                int pos2 = binary(bit + h[a[i]], h[a[i] + 1] - h[a[i]], b[i] - len2[a[i]]);
                Ins(bit + h[a[i]], h[a[i] + 1] - h[a[i]], pos2, -1);
                Ans[i] = arr[h[a[i]] + pos2];
                Ins(bit + h[a[i]], h[a[i] + 1] - h[a[i]], ++len[a[i]], 1);
                arr[h[a[i]] + len[a[i]]] = arr[h[n + 1] + pos];
            } else
                Ans[i] = arr[h[n + 1] + pos];

            arr[h[n + 1] + len[n + 1]] = Ans[i];
        }
    }
    F(i, 1, q) printf("%lld\n", Ans[i]);
    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 1ms
memory: 13792kb

input:

947 912 485
132 85
132 456
870 210
132 879
132 266
768 732
309 875
815 360
309 145
511 505
132 587
3...

output:

119557
119929
792738
120353
119739
700236
281771
742728
281041
465625
120062
34882
120058
119917
296...

result:

ok 485 tokens

Test #2:

score: 5
Accepted
time: 0ms
memory: 15820kb

input:

987 952 450
514 732
514 353
700 426
934 918
484 764
700 782
467 340
484 554
452 530
700 832
484 255
...

output:

489108
488729
665874
889134
460580
666231
443972
460370
429882
666282
460071
509806
665795
460633
50...

result:

ok 450 tokens

Test #3:

score: 5
Accepted
time: 1ms
memory: 13860kb

input:

958 955 461
64 264
64 131
165 380
860 393
173 771
64 16
529 604
102 511
605 431
64 134
64 797
767 89...

output:

60429
60296
157000
820738
165031
60181
504844
96966
577251
60301
60966
732428
60943
60268
60251
6063...

result:

ok 461 tokens

Test #4:

score: 5
Accepted
time: 0ms
memory: 13780kb

input:

996 961 493
788 314
123 245
546 872
546 775
232 639
7 497
546 29
624 778
546 385
546 123
546 594
7 8...

output:

756621
117487
524617
524520
222630
6263
523774
599481
524131
523869
524342
6634
951368
207772
524580...

result:

ok 493 tokens

Test #5:

score: 5
Accepted
time: 5ms
memory: 13792kb

input:

960 932 483
299 828
822 589
379 787
359 306
939 285
822 471
939 928
939 881
876 909
939 632
702 134
...

output:

278564
765761
353083
333962
874501
765643
875145
875098
816409
874849
653466
874896
874480
874674
87...

result:

ok 483 tokens

Test #6:

score: 5
Accepted
time: 1ms
memory: 17796kb

input:

959 910 498
415 200
534 83
142 226
538 854
142 272
287 733
538 359
287 435
538 485
538 459
538 760
3...

output:

376940
485113
128536
489524
128583
260993
489029
260695
489156
489130
489433
355500
128868
377081
26...

result:

ok 498 tokens

Test #7:

score: 5
Accepted
time: 10ms
memory: 15892kb

input:

46090 48139 469
32888 24558
39842 17220
32888 22555
2432 710
32888 41705
41158 32113
28661 19192
328...

output:

1583171851
1917923119
1583169848
117026619
1583189000
1981288936
1379682932
1583178039
723952041
134...

result:

ok 469 tokens

Test #8:

score: 5
Accepted
time: 10ms
memory: 15892kb

input:

45906 45864 478
32892 32189
32892 8353
32892 43438
32892 3506
32892 14396
31966 8179
44193 32656
447...

output:

1508545013
1508521177
1508556264
1508516330
1508527222
1466050939
2026854544
2052221469
998878588
14...

result:

ok 478 tokens

Test #9:

score: 5
Accepted
time: 3ms
memory: 13780kb

input:

49982 49508 497
10743 1315
10743 25275
10743 322
10743 44566
10743 49087
18459 20641
20288 16221
382...

output:

531816251
531840212
531815258
531859505
531864027
913839305
1004385017
1895387104
531821070
22941930...

result:

ok 497 tokens

Test #10:

score: 5
Accepted
time: 5ms
memory: 17788kb

input:

45008 49587 493
36340 36290
36340 583
36340 8852
6424 18342
13225 19363
36340 24412
36340 2566
36340...

output:

1801978283
1801942576
1801950846
318515643
655757851
1801966407
1801944560
1801951045
318508232
5282...

result:

ok 493 tokens

Test #11:

score: 5
Accepted
time: 80ms
memory: 19032kb

input:

1 95997 94067
1 77052
1 64493
1 17844
1 44007
1 88132
1 12639
1 44876
1 5827
1 92787
1 90765
1 16971...

output:

77052
64493
17844
44008
88136
12639
44879
5827
92795
90773
16973
50641
61983
69610
46608
36294
77397...

result:

ok 94067 tokens

Test #12:

score: 5
Accepted
time: 69ms
memory: 18412kb

input:

1 95878 93419
1 57697
1 69176
1 72649
1 57486
1 92625
1 55523
1 95820
1 500
1 33202
1 38116
1 27944
...

output:

57697
69177
72651
57486
92629
55523
95826
500
33203
38118
27945
43741
52089
20548
52172
4358
77212
5...

result:

ok 93419 tokens

Test #13:

score: 5
Accepted
time: 206ms
memory: 24404kb

input:

1 281529 286011
1 220781
1 176736
1 80232
1 214918
1 94388
1 162948
1 187418
1 260381
1 192288
1 191...

output:

220781
176736
80232
214920
94389
162950
187422
260388
192293
191814
271820
211004
249018
276759
2514...

result:

ok 286011 tokens

Test #14:

score: 5
Accepted
time: 211ms
memory: 24236kb

input:

1 285136 270001
1 55707
1 9327
1 282627
1 36065
1 78607
1 104998
1 111141
1 7132
1 259733
1 205024
1...

output:

55707
9327
282629
36066
78610
105002
111146
7132
259740
205031
207968
180256
14994
260908
152568
475...

result:

ok 270001 tokens

Test #15:

score: 5
Accepted
time: 220ms
memory: 24232kb

input:

276323 289616 271863
1 152634
1 261930
1 75268
1 41750
1 146210
1 282483
1 58759
1 157261
1 228531
1...

output:

152634
261931
75268
41750
146212
282488
58760
157266
228537
70372
163392
169037
114381
188655
196463...

result:

ok 271863 tokens

Test #16:

score: 5
Accepted
time: 237ms
memory: 24388kb

input:

280877 276718 284413
1 98202
1 65837
1 73581
1 159975
1 108572
1 105574
1 254688
1 230526
1 17734
1 ...

output:

98202
65837
73582
159978
108575
105577
254694
230532
17734
33126
231177
265167
176419
191486
229027
...

result:

ok 284413 tokens

Test #17:

score: 5
Accepted
time: 85ms
memory: 20432kb

input:

97664 97048 97136
49584 30364
49584 81753
49584 73120
7549 79074
56069 85192
30521 19391
69800 17074...

output:

4811961348
4812012738
4812004105
732597378
5441372456
2961924351
6773870426
8095588634
4812010314
48...

result:

ok 97136 tokens

Test #18:

score: 5
Accepted
time: 86ms
memory: 20596kb

input:

96913 97691 93607
93839 72869
3014 95009
67429 97651
93839 60624
93839 90458
93839 69517
87196 97194...

output:

9167200927
294437992
6587206399
9167188682
9167218518
9167197576
8518263939
6943973957
1001902123
40...

result:

ok 93607 tokens

Test #19:

score: 5
Accepted
time: 323ms
memory: 24012kb

input:

284325 270205 286060
19082 69433
104320 23185
86843 149368
277753 174142
253549 14125
183675 239428
...

output:

5155851038
28187538580
23465291978
75050153302
68509951465
49629872598
43443827262
75050245891
75050...

result:

ok 286060 tokens

Test #20:

score: 5
Accepted
time: 320ms
memory: 23900kb

input:

296050 294787 286241
227314 294430
278930 294738
87883 294379
144595 245026
126258 94135
66866 29404...

output:

67009211761
82224937861
25906765513
42624676504
37219016394
19711226800
40728609839
34944355349
1566...

result:

ok 286241 tokens