The main topic: give a sequence, to find the number of occurrences in a period of a positive even numbers.
Train of thought: 50000, sub-block. The main thing is to deal with two arrays, one is the answer to the whole block, the space complexity is O (√n*√n), there is a prefix and the number of times each block appears, the Space Complexity O (n*√n). Then it was violence. The code is messy, and you need to analyze the complexity of the time based on for.
This question probably does not need to read into the optimization also can, is the space comparison card.
(To make a time stamp!)
CODE:
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define MAX 320#define _max 100010using namespace std; inline int getc () {static const int L = 1 << 15; Static Char buf[l], *s = buf, *t = buf; if (s = = t) {T = (s = buf) + fread (buf, 1, L, stdin); if (S = = T) return EOF; } return *s++;} inline int getint () {int C; while (!isdigit (c = getc ()) && c! = '-'); BOOL sign = c = = '-'; int tmp = sign? 0:c-' 0 '; while (isdigit (c = getc ())) TMP = (tmp << 1) + (TMP << 3) + C-' 0 '; Return sign? -tmp:tmp;} int cnt,cols,asks;int block_size,blocks;int Src[_max],belong[_max],begin[_max];int ans_block[MAX][MAX];int Cnt_ Block[max][_max]; int Temp[_max],v[_max],t;int stack[_max],top; int main () {cin >> cnt >> cols >> asks; Block_size = sqrt (CNT); for (int i = 1; I <= cnt; ++i) {Src[i] = Getint (); scanf ("%d", &src[i]); Belong[i] = i/block_size + 1; if (!begin[belong[i]]) begin[belong[i]] = i; ++cnt_block[belong[i]][src[i]]; } blocks = belong[cnt]; Making cnt_block[][] (presum for (int i = 2, I <= blocks; ++i) for (int j = 1; j <= cols; ++j) c NT_BLOCK[I][J] + = cnt_block[i-1][j]; Making ans_block[][]; int now; for (int i = 1; I <= blocks; ++i) {now = 0; ++t; for (int j = begin[i]; J <= cnt + 1; ++j) {if (belong[j]! = belong[j-1]) ans_block[i][belong [J-1]] = Now; if (v[src[j] [= T) v[src[j]] = t,temp[src[j]] = 0; if (Temp[src[j]] > 0) {if (temp[src[j]]&1) ++now; else--now; } ++temp[src[j]]; }}//asking int last_ans = 0; for (int x,y,i = 1; I <= asks; ++i) {x = Getint (), y = Getint (); scanf ("%d%d ", &x,&y); x = (x + last_ans)% cnt + 1; y = (y + last_ans)% cnt + 1; if (x > Y) Swap (n, y); int st = Belong[x] + 1,ed = belong[y]-1,ans = 0; ++t; if (St > ed) {for (int j = x; j <= y; ++j) {if (v[src[j]]! = T) V[src[j] ] = T,temp[src[j]] = 0; if (Temp[src[j]] > 0) {if (temp[src[j]]&1) ++ans; else--ans; } ++temp[src[j]]; } printf ("%d\n", Last_ans = ans); Continue } ans = ans_block[st][ed]; top = 0; for (int k = x; belong[k] = = belong[x]; ++k) stack[++top] = src[k]; for (int k = y; belong[k] = = Belong[y];--k) stack[++top] = src[k]; Sort (stack + 1,stack + top + 1); Stack[top + 1] = 0; int num = 0; for (int j = 1; J <= top + 1; ++j) {++num; if (stack[j]! = stack[j + 1]) {int temp = Cnt_block[ed][stack[j]]-cnt_block[st-1][stack[j]]; if (!temp) ans + =! (num&1); else if (num&1) {if (temp&1) ++ans; else--ans; num = 0; }} printf ("%d\n", Last_ans = ans); } return 0;}
Bzoj 2821 Poetry (poetize) sub-block