(use util.queue)(use util.match)(use gauche.collection)(use gauche.sequence) (define-syntax def-syntax (syntax-rules()((_ v e ...)(define-syntax v (syntax-rules e ...))))) (define-syntax def-const define-constant)(define-syntax def define)(define-syntax def-inline define-inline) (def-syntax def-names ()((_[x y]...)(begin(define-constant x y)...))) (def-names [// quotient][% modulo][v-make make-vector][v-ref vector-ref][v-set! vector-set!][ht-make make-hash-table][ht-ref hash-table-get][ht-set! hash-table-put!][ht-keys hash-table-keys][ht-values hash-table-values][q-make make-queue][make-q make-queue][enq! enqueue!][deq! dequeue!][q-empty? queue-empty?]) (def-syntax input (: list vec cons str) ((_ : cons e ...)(cons(input : e ...)(input : e ...)))((_ : list str)(input : string->list str)) ((_ : vec str)(input : string->vector str))((_ : vec n e ...)(vector-tabulate n(^_(input : e ...)))) ((_ : list n e ...)(let l([k n])(if(zero? k)'()(cons(input : e ...)(l(- k 1))))))((_ :)(read))((_ : : e)e) ((_ : str)(let1 s(read-line)(if(string=? s "")(read-line)s)))((_[[v]: e ...])(def v(input : e ...))) ((_[[v w ...]: e ...])(begin(def v(input : e ...))(input[[w ...]: e ...]))) ((_[v : e ...])(def v(input : e ...)))((_ : op e ...)(op(input : e ...))) ((_(e ...))(e ...))((_ v)(def v(read)))((_ e ...)(begin(input e)...))) (define-constant *MOD* 1000000007) ;;(define-constant *MOD* 998244353) (def-syntax add-mod! ()((_ v x)(let1 y(+ v x)(set! v(if(< y *MOD*)y(- y *MOD*)))))) (def-inline(v-add-mod! v i x)(let1 y(+(v-ref v i)x)(v-set! v i(if(< y *MOD*)y(- y *MOD*))))) (def-syntax max! ()((_ v x)(set! v (max x v)))) (def-syntax min! ()((_ v x)(set! v (min x v)))) (def-inline(v-add! v i x)(v-set! v i(+ x(v-ref v i)))) (def-inline(ht-add! ht i x)(ht-set! ht i(+ x(ht-ref ht i 0)))) (def-inline(ht-cons! ht i x)(ht-set! ht i(cons x(ht-ref ht i '())))) (def-syntax return () ((_ e ...) (begin (print e ...) (exit)))) ;; 以下スニペットのコピペ ;; fenwick-tree ;; fw := (size . vector) (use scheme.bitwise) (define (make-fenwick-tree n) (cons n (make-vector n 0))) (define-constant %fenwick-tree-N car) (define-constant %fenwick-tree-data cdr) (define (fenwick-tree-add! fw p x) ;; fw[p] += x (let [[data (%fenwick-tree-data fw)] [n (%fenwick-tree-N fw)]] (let loop[[p (+ p 1)]] (when (<= p n) (vector-set! data (- p 1) (modulo (+ x (vector-ref data (- p 1))) *MOD*)) (loop (+ p (bitwise-and p (- p)))))))) (define (fenwick-tree-sum fw l r) ;; sum fw[i] , i <- [l r) (- (%fenwick-tree-sum (%fenwick-tree-data fw) r) (%fenwick-tree-sum (%fenwick-tree-data fw) l))) (define (%fenwick-tree-sum data r) (let loop[[r r] [s 0]] (cond [(zero? r) s] [else (loop (- r (bitwise-and r (- r))) (modulo (+ s (vector-ref data (- r 1))) *MOD*))]))) ;; (v1 ... vi ...vm) → (a1 ... ai ...am) 但しviはv中でai番目に小さい(0番始まり) ;; index->Nth : i->aiのvector ;; Nth->origin : ai->viのvector ;; origin->Nth : vi->aiのhash-table ;; N : aiの種類数 (use gauche.sequence) (define (zaatsu ls) (let* [[len-ls (length ls)] [gls (group-sequence (sort (map cons ls (iota len-ls))) :key car)] [N (length gls)] [origin->Nth (make-hash-table)] [index->Nth (make-vector len-ls)] [Nth->origin (make-vector N)]] (for-each-with-index (^(ai ls) (dolist [p ls] ;; (vi . i) (vector-set! index->Nth (cdr p) ai) (vector-set! Nth->origin ai (car p)) (hash-table-put! origin->Nth (car p) ai))) gls) (values index->Nth Nth->origin origin->Nth N))) ;; 以下が本体 (input N K [A : vec N] [S : (cut map (^c (char=? #\< c)) <>) vec str]) (define-values [index->Nth Nth->origin origin->Nth M] (zaatsu (vector->list A))) (def AA index->Nth) (def v (make-vector N 1)) (dolist [s S] (let [[vv (make-vector N)] [fw (make-fenwick-tree (+ M 1))]] (dotimes [i N] (v-set! vv i (if s (fenwick-tree-sum fw 0 (v-ref AA i)) (fenwick-tree-sum fw (+ (v-ref AA i) 1) M))) (fenwick-tree-add! fw (v-ref AA i) (v-ref v i))) (set! v vv))) (print (fold (^(x s) (modulo (+ x s) *MOD*)) 0 v))