Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2688
Main topic:
Give you n number, (N<=3E6), there are two operations, Q for the current number of logarithm, to meet the strict increment, R l,r for the rotation of l,r this interval
Problem Solving Ideas:
The order of strict increment we can reverse the use of the tree-like array of inverse, 300W data is a little bit tough, but here is also Nlogn, and then for the rotation operation, because the interval size does not exceed 1000, we just need to count the interval of the first number and the number of the relationship between the If the first number is larger than the number behind, then ans++, if less than ans--, equal to regardless, because it is strictly incremented, and then it is here I add read-in optimization, feeling or no egg use, but rather than speed up, maybe I write the optimization not it. This card constant card is a bit tight, to pay attention to the constant optimization, there is the stability of the hdoj is not very good, the same code can sometimes pass, and sometimes can not
1#include <iostream>2#include <cstdio>3#include <cstring>4 using namespacestd;5 6 #defineN 100017 #definell Long Long8 #defineLowbit (x) ((x) & (-X))9 Ten ll C[n]; Onell num[n* -]; A intT; - - the voidAdd (ll c[],ll pos,ll num) { - while(POS <= N) {//x Max is n -C[pos] + =num; -pos + =Lowbit (POS); + } - } + A ll Sum (ll C[],ll end) { atll sum =0; - while(End >0) { -Sum + =C[end]; -End-=lowbit (end); - } - returnsum; in } - to intMain () { + intN, S, T, I, J, T, K; - ll ans, tmp; the while(~SCANF ("%d",&T)) { *memset (C,0,sizeof(C)); $Ans =0;Panax Notoginseng for(i =0; i < T; i + +) { -scanf"%i64d",&num[i]); theAdd (C,num[i],1); +Ans + = Sum (C,num[i]-1); A } thescanf"%d",&n); + Charc[Ten]; - while(n--) { $scanf"%s", c); $ Switch(c[0]) { - Case 'Q': -printf"%i64d\n", ans); the Break; - Case 'R':Wuyiscanf"%d%d",&s,&t); the intv =Num[s]; - for(i = s; i < T; i++){ WuNum[i] = num[i +1]; - if(V > num[i]) ans++; About Else if(v < num[i]) ans--; $ } -NUM[T] =v; - Break; - } A } + the } - return 0; $}
hdu-2688 Rotate---Tree-shaped array + simulation