Um_nik

Untitled

Aug 12th, 2021
1,502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.76 KB | None | 0 0
  1. #ifndef LOCAL
  2. #pragma GCC optimize ("O3")
  3. //#pragma GCC optimize ("unroll-loops")
  4. #endif
  5.  
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8.  
  9. //fast IO by yosupo
  10. //sc.read(string) だと append される
  11. struct Scanner {
  12.     FILE* fp = nullptr;
  13.     char line[(1 << 15) + 1];
  14.     size_t st = 0, ed = 0;
  15.     void reread() {
  16.         memmove(line, line + st, ed - st);
  17.         ed -= st;
  18.         st = 0;
  19.         ed += fread(line + ed, 1, (1 << 15) - ed, fp);
  20.         line[ed] = '\0';
  21.     }
  22.     bool succ() {
  23.         while (true) {
  24.             if (st == ed) {
  25.                 reread();
  26.                 if (st == ed) return false;
  27.             }
  28.             while (st != ed && isspace(line[st])) st++;
  29.             if (st != ed) break;
  30.         }
  31.         if (ed - st <= 50) reread();
  32.         return true;
  33.     }
  34.     template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
  35.     bool read_single(T& ref) {
  36.         if (!succ()) return false;
  37.         while (true) {
  38.             size_t sz = 0;
  39.             while (st + sz < ed && !isspace(line[st + sz])) sz++;
  40.             ref.append(line + st, sz);
  41.             st += sz;
  42.             if (!sz || st != ed) break;
  43.             reread();            
  44.         }
  45.         return true;
  46.     }
  47.     template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  48.     bool read_single(T& ref) {
  49.         if (!succ()) return false;
  50.         bool neg = false;
  51.         if (line[st] == '-') {
  52.             neg = true;
  53.             st++;
  54.         }
  55.         ref = T(0);
  56.         while (isdigit(line[st])) {
  57.             ref = 10 * ref + (line[st++] - '0');
  58.         }
  59.         if (neg) ref = -ref;
  60.         return true;
  61.     }
  62.     template <class T> bool read_single(vector<T>& ref) {
  63.         for (auto& d : ref) {
  64.             if (!read_single(d)) return false;
  65.         }
  66.         return true;
  67.     }
  68.     void read() {}
  69.     template <class H, class... T> void read(H& h, T&... t) {
  70.         bool f = read_single(h);
  71.         assert(f);
  72.         read(t...);
  73.     }
  74.     Scanner(FILE* _fp) : fp(_fp) {}
  75. };
  76.  
  77. struct Printer {
  78.   public:
  79.     template <bool F = false> void write() {}
  80.     template <bool F = false, class H, class... T>
  81.     void write(const H& h, const T&... t) {
  82.         if (F) write_single(' ');
  83.         write_single(h);
  84.         write<true>(t...);
  85.     }
  86.     template <class... T> void writeln(const T&... t) {
  87.         write(t...);
  88.         write_single('\n');
  89.     }
  90.  
  91.     Printer(FILE* _fp) : fp(_fp) {}
  92.     ~Printer() { flush(); }
  93.  
  94.   private:
  95.     static constexpr size_t SIZE = 1 << 15;
  96.     FILE* fp;
  97.     char line[SIZE], small[50];
  98.     size_t pos = 0;
  99.     void flush() {
  100.         fwrite(line, 1, pos, fp);
  101.         pos = 0;
  102.     }
  103.     void write_single(const char& val) {
  104.         if (pos == SIZE) flush();
  105.         line[pos++] = val;
  106.     }
  107.     template <class T, enable_if_t<is_integral<T>::value, int> = 0>
  108.     void write_single(T val) {
  109.         if (pos > (1 << 15) - 50) flush();
  110.         if (val == 0) {
  111.             write_single('0');
  112.             return;
  113.         }
  114.         if (val < 0) {
  115.             write_single('-');
  116.             val = -val;  // todo min
  117.         }
  118.         size_t len = 0;
  119.         while (val) {
  120.             small[len++] = char('0' + (val % 10));
  121.             val /= 10;
  122.         }
  123.         for (size_t i = 0; i < len; i++) {
  124.             line[pos + i] = small[len - 1 - i];
  125.         }
  126.         pos += len;
  127.     }
  128.     void write_single(const string& s) {
  129.         for (char c : s) write_single(c);
  130.     }
  131.     void write_single(const char* s) {
  132.         size_t len = strlen(s);
  133.         for (size_t i = 0; i < len; i++) write_single(s[i]);
  134.     }
  135.     template <class T> void write_single(const vector<T>& val) {
  136.         auto n = val.size();
  137.         for (size_t i = 0; i < n; i++) {
  138.             if (i) write_single(' ');
  139.             write_single(val[i]);
  140.         }
  141.     }
  142.     void write_single(long double d){
  143.         {
  144.             long long v=d;
  145.             write_single(v);
  146.             d-=v;
  147.         }
  148.         write_single('.');
  149.         for(int _=0;_<8;_++){
  150.             d*=10;
  151.             long long v=d;
  152.             write_single(v);
  153.             d-=v;
  154.         }
  155.     }
  156. };
  157.  
  158. Scanner sc(stdin);
  159. Printer pr(stdout);
  160.  
  161.  
  162. using ll=long long;
  163. //#define int ll
  164.  
  165. #define rng(i,a,b) for(int i=int(a);i<int(b);i++)
  166. #define rep(i,b) rng(i,0,b)
  167. #define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
  168. #define per(i,b) gnr(i,0,b)
  169. #define pb push_back
  170. #define eb emplace_back
  171. #define a first
  172. #define b second
  173. #define bg begin()
  174. #define ed end()
  175. #define all(x) x.bg,x.ed
  176. #define si(x) int(x.size())
  177. #ifdef LOCAL
  178. #define dmp(x) cerr<<__LINE__<<" "<<#x<<" "<<x<<endl
  179. #else
  180. #define dmp(x) void(0)
  181. #endif
  182.  
  183. template<class t,class u> bool chmax(t&a,u b){if(a<b){a=b;return true;}else return false;}
  184. template<class t,class u> bool chmin(t&a,u b){if(b<a){a=b;return true;}else return false;}
  185.  
  186. template<class t> using vc=vector<t>;
  187. template<class t> using vvc=vc<vc<t>>;
  188.  
  189. using pi=pair<int,int>;
  190. using vi=vc<int>;
  191.  
  192. template<class t,class u>
  193. ostream& operator<<(ostream& os,const pair<t,u>& p){
  194.     return os<<"{"<<p.a<<","<<p.b<<"}";
  195. }
  196.  
  197. template<class t> ostream& operator<<(ostream& os,const vc<t>& v){
  198.     os<<"{";
  199.     for(auto e:v)os<<e<<",";
  200.     return os<<"}";
  201. }
  202.  
  203. #define mp make_pair
  204. #define mt make_tuple
  205. #define one(x) memset(x,-1,sizeof(x))
  206. #define zero(x) memset(x,0,sizeof(x))
  207. #ifdef LOCAL
  208. void dmpr(ostream&os){os<<endl;}
  209. template<class T,class... Args>
  210. void dmpr(ostream&os,const T&t,const Args&... args){
  211.     os<<t<<" ";
  212.     dmpr(os,args...);
  213. }
  214. #define dmp2(...) dmpr(cerr,__LINE__,##__VA_ARGS__)
  215. #else
  216. #define dmp2(...) void(0)
  217. #endif
  218.  
  219. using uint=unsigned;
  220. using ull=unsigned long long;
  221.  
  222. template<class t,size_t n>
  223. ostream& operator<<(ostream&os,const array<t,n>&a){
  224.     return os<<vc<t>(all(a));
  225. }
  226.  
  227. template<int i,class T>
  228. void print_tuple(ostream&,const T&){
  229. }
  230.  
  231. template<int i,class T,class H,class ...Args>
  232. void print_tuple(ostream&os,const T&t){
  233.     if(i)os<<",";
  234.     os<<get<i>(t);
  235.     print_tuple<i+1,T,Args...>(os,t);
  236. }
  237.  
  238. template<class ...Args>
  239. ostream& operator<<(ostream&os,const tuple<Args...>&t){
  240.     os<<"{";
  241.     print_tuple<0,tuple<Args...>,Args...>(os,t);
  242.     return os<<"}";
  243. }
  244.  
  245. template<class t>
  246. void print(t x,int suc=1){
  247.     cout<<x;
  248.     if(suc==1)
  249.         cout<<"\n";
  250.     if(suc==2)
  251.         cout<<" ";
  252. }
  253.  
  254. ll read(){
  255.     ll i;
  256.     cin>>i;
  257.     return i;
  258. }
  259.  
  260. vi readvi(int n,int off=0){
  261.     vi v(n);
  262.     rep(i,n)v[i]=read()+off;
  263.     return v;
  264. }
  265.  
  266. pi readpi(int off=0){
  267.     int a,b;cin>>a>>b;
  268.     return pi(a+off,b+off);
  269. }
  270.  
  271. template<class t,class u>
  272. void print(const pair<t,u>&p,int suc=1){
  273.     print(p.a,2);
  274.     print(p.b,suc);
  275. }
  276.  
  277. template<class T>
  278. void print(const vector<T>&v,int suc=1){
  279.     rep(i,v.size())
  280.         print(v[i],i==int(v.size())-1?suc:2);
  281. }
  282.  
  283. template<class T,size_t N>
  284. void print(const array<T,N>&v,int suc=1){
  285.     rep(i,N)
  286.         print(v[i],i==int(N)-1?suc:2);
  287. }
  288.  
  289. string readString(){
  290.     string s;
  291.     cin>>s;
  292.     return s;
  293. }
  294.  
  295. template<class T>
  296. T sq(const T& t){
  297.     return t*t;
  298. }
  299.  
  300. //#define CAPITAL
  301. void yes(bool ex=true){
  302.     #ifdef CAPITAL
  303.     cout<<"YES"<<"\n";
  304.     #else
  305.     cout<<"Yes"<<"\n";
  306.     #endif
  307.     if(ex)exit(0);
  308.     #ifdef LOCAL
  309.     cout.flush();
  310.     #endif
  311. }
  312. void no(bool ex=true){
  313.     #ifdef CAPITAL
  314.     cout<<"NO"<<"\n";
  315.     #else
  316.     cout<<"No"<<"\n";
  317.     #endif
  318.     if(ex)exit(0);
  319.     #ifdef LOCAL
  320.     cout.flush();
  321.     #endif
  322. }
  323. void possible(bool ex=true){
  324.     #ifdef CAPITAL
  325.     cout<<"POSSIBLE"<<"\n";
  326.     #else
  327.     cout<<"Possible"<<"\n";
  328.     #endif
  329.     if(ex)exit(0);
  330.     #ifdef LOCAL
  331.     cout.flush();
  332.     #endif
  333. }
  334. void impossible(bool ex=true){
  335.     #ifdef CAPITAL
  336.     cout<<"IMPOSSIBLE"<<"\n";
  337.     #else
  338.     cout<<"Impossible"<<"\n";
  339.     #endif
  340.     if(ex)exit(0);
  341.     #ifdef LOCAL
  342.     cout.flush();
  343.     #endif
  344. }
  345.  
  346. constexpr ll ten(int n){
  347.     return n==0?1:ten(n-1)*10;
  348. }
  349.  
  350. const ll infLL=LLONG_MAX/3;
  351.  
  352. #ifdef int
  353. const int inf=infLL;
  354. #else
  355. const int inf=INT_MAX/2-100;
  356. #endif
  357.  
  358. int topbit(signed t){
  359.     return t==0?-1:31-__builtin_clz(t);
  360. }
  361. int topbit(ll t){
  362.     return t==0?-1:63-__builtin_clzll(t);
  363. }
  364. int botbit(signed a){
  365.     return a==0?32:__builtin_ctz(a);
  366. }
  367. int botbit(ll a){
  368.     return a==0?64:__builtin_ctzll(a);
  369. }
  370. int popcount(signed t){
  371.     return __builtin_popcount(t);
  372. }
  373. int popcount(ll t){
  374.     return __builtin_popcountll(t);
  375. }
  376. bool ispow2(int i){
  377.     return i&&(i&-i)==i;
  378. }
  379. ll mask(int i){
  380.     return (ll(1)<<i)-1;
  381. }
  382.  
  383. bool inc(int a,int b,int c){
  384.     return a<=b&&b<=c;
  385. }
  386.  
  387. template<class t> void mkuni(vc<t>&v){
  388.     sort(all(v));
  389.     v.erase(unique(all(v)),v.ed);
  390. }
  391.  
  392. ll rand_int(ll l, ll r) { //[l, r]
  393.     static mt19937_64 gen;
  394.     return uniform_int_distribution<ll>(l, r)(gen);
  395. }
  396.  
  397. template<class t>
  398. void myshuffle(vc<t>&a){
  399.     rep(i,si(a))swap(a[i],a[rand_int(0,i)]);
  400. }
  401.  
  402. template<class t>
  403. int lwb(const vc<t>&v,const t&a){
  404.     return lower_bound(all(v),a)-v.bg;
  405. }
  406.  
  407. vvc<int> readGraph(int n,int m){
  408.     vvc<int> g(n);
  409.     rep(i,m){
  410.         int a,b;
  411.         cin>>a>>b;
  412.         //sc.read(a,b);
  413.         a--;b--;
  414.         g[a].pb(b);
  415.         g[b].pb(a);
  416.     }
  417.     return g;
  418. }
  419.  
  420. vvc<int> readTree(int n){
  421.     return readGraph(n,n-1);
  422. }
  423.  
  424. const int nmax=ten(6)+10;
  425.  
  426. //assume z is strictly ascending
  427. //z に重要な点が入っていて
  428. //問題を解くべき区間 [l,r] というのは,[l,r] 内の重要な点の個数の密度が 1/k 以上
  429. //という状況の時,座標圧縮に似たことができて,必要な区間を列挙する.
  430. //線形時間
  431.  
  432. vc<pi> getlr(int k,vi z){
  433.     auto getval=[&](int i){return i*k-z[i];};
  434.     vc<pi> hs;
  435.     rep(i,si(z)){
  436.         int lw=getval(i),up=lw+k-1;
  437.         while(si(hs)&&hs.back().a<=up){
  438.             chmin(lw,hs.back().a);
  439.             chmax(up,hs.back().b);
  440.             hs.pop_back();
  441.         }
  442.         hs.eb(lw,up);
  443.     }
  444.     int head=0;
  445.     vc<pi> lr;
  446.     for(auto [lw,up]:hs){
  447.         int l=z[head]-(up-getval(head));
  448.         while(head<si(z)&&lw<=getval(head))head++;
  449.         int r=z[head-1]+1+(getval(head-1)+k-1-lw);
  450.         lr.eb(l,r);
  451.     }
  452.     return lr;
  453. }
  454.  
  455. vi ps[nmax];
  456. vc<pi> unko[nmax];
  457.  
  458. int pool[ten(7)+10],phead;
  459.  
  460. int num=0;
  461.  
  462. struct Y{
  463.     ll c;
  464.     int n,*buf,rpre,rsum,lpre,lsum;
  465.     void init(int col,int s){
  466.         c=col;
  467.         n=s;
  468.         buf=pool+phead;
  469.         phead+=s;
  470.     }
  471.     void clear(int buri){
  472.         rep(i,n)buf[i]=0;
  473.         rpre=lpre=buri;
  474.         lsum=rsum=0;
  475.     }
  476.     ll left(int i,int w){
  477.         //num++;
  478.         //return 0;
  479.         //assert(abs(lpre-i)<=1);
  480.         if(lpre<i){
  481.             lsum-=buf[++lpre];
  482.         }
  483.         if(lpre>i){
  484.             lsum+=buf[lpre--];
  485.         }
  486.         buf[lpre]+=w;
  487.         if(lpre<rpre)rsum+=w;
  488.         return c*lsum*w;
  489.     }
  490.     ll right(int i,int w){
  491.         //num++;
  492.         //return 0;
  493.         //assert(abs(rpre-i)<=1);
  494.         if(rpre<i){
  495.             rsum+=buf[rpre++];
  496.         }
  497.         if(rpre>i){
  498.             rsum-=buf[--rpre];
  499.         }
  500.         buf[rpre]+=w;
  501.         if(lpre<rpre)lsum+=w;
  502.         return c*rsum*w;
  503.     }
  504. } buf[nmax];
  505.  
  506. int lastseen[nmax],kosuu[nmax];
  507.  
  508. bool dbg=false;
  509.  
  510. void slv(){
  511.     int n,m;
  512.     sc.read(n,m);
  513.     if(n<0){
  514.         n=-n;
  515.         dbg=true;
  516.     }
  517.     vi abuf(n);
  518.     rep(i,n){
  519.         int a;
  520.         if(dbg){
  521.             a=rand_int(1,3);
  522.             a=i%2+1;
  523.             a=i%3+1;
  524.             a*=n/3;
  525.         }else{
  526.             sc.read(a);
  527.         }
  528.         a--;
  529.         ps[a].pb(i);
  530.         abuf[i]=a;
  531.     }
  532.     vc<ll> rw(n+1,0);
  533.     const int L=20;
  534.     vi see(n,0);
  535.     vc<pi> lrlist;
  536.     int u=0;
  537.     {
  538.         vi tmp;
  539.         rep(col,n)if(si(ps[col])){
  540.             auto cur=getlr(2,ps[col]);
  541.             int head=0;
  542.             auto gethei=[&](int i){
  543.                 while(head<si(ps[col])&&ps[col][head]<i)
  544.                     head++;
  545.                 return head*2-i;
  546.             };
  547.             for(auto [l,r]:cur){
  548.                 l++;
  549.                 r--;
  550.                 tmp.clear();
  551.                 chmax(l,0);
  552.                 chmin(r,n);
  553.                 int lw=inf,up=-inf;
  554.                 rng(i,l,r+1){
  555.                     int h=gethei(i);
  556.                     tmp.pb(h);
  557.                     chmin(lw,h);
  558.                     chmax(up,h);
  559.                 }
  560.                 if(r-l<L){
  561.                     rng(i,l,r+1){
  562.                         rng(j,l,i){
  563.                             if(tmp[j-l]<tmp[i-l]){
  564.                                 rw[i]+=col+1;
  565.                             }
  566.                         }
  567.                     }
  568.                     rng(i,l,r)if(abuf[i]==col)see[i]=col+1;
  569.                 }else{
  570.                     rng(i,l,r+1)
  571.                         unko[i].eb(u,tmp[i-l]-lw);
  572.                     buf[u++].init(col+1,up-lw+1);
  573.                     lrlist.eb(l,r+1);
  574.                 }
  575.             }
  576.         }
  577.     }
  578.     vi gomi(u);
  579.     int norg=n;
  580.     vc<ll> sing(n+2);
  581.     rep(i,n+1)sing[i+1]=sing[i]+rw[i];
  582.     vi xs;
  583.     rep(i,n+1)if(si(unko[i]))xs.pb(i);
  584.     n=si(xs);
  585.     rep(i,n)unko[i]=unko[xs[i]];
  586.     vi pre(n+1);
  587.     rep(i,n)pre[i+1]=pre[i]+si(unko[i]);
  588.     //cerr<<u<<endl;
  589.     //cerr<<pre[n]<<endl;
  590.     const int B=max<int>(pre[n]/sqrt(m),1);
  591.     //const int B=4000;
  592.     //cerr<<B<<endl;
  593.     struct Q{
  594.         int l,r,i;
  595.     };
  596.     vc<Q> qs(m);
  597.     vc<ll> res(m);
  598.     int curtime=0;
  599.     rep(idx,m){
  600.         int l,r;
  601.         if(dbg){
  602.             do{
  603.                 l=rand_int(1,norg);
  604.                 //r=rand_int(1,norg);
  605.                 r=norg;
  606.             }while(l>r);
  607.         }else{
  608.             sc.read(l,r);
  609.         }
  610.         l--;r++;
  611.         res[idx]=sing[r]-sing[l];
  612.         rng(i,l,min(l+L,r)){
  613.             res[idx]-=rw[i];
  614.         }
  615.         //dmp(res[idx]);
  616.         rng(i,l,min(l+L,r)){
  617.             curtime++;
  618.             int tot=0,mx=0,val=0;
  619.             gnr(j,l,i){
  620.                 int v=see[j];
  621.                 if(lastseen[v]<curtime){
  622.                     lastseen[v]=curtime;
  623.                     kosuu[v]=0;
  624.                 }
  625.                 if(chmax(mx,++kosuu[v])){
  626.                     val=v;
  627.                 }
  628.                 tot++;
  629.                 //dmp2(j,i,tot,mx,val);
  630.                 if(mx*2>tot)res[idx]+=val;
  631.             }
  632.         }
  633.         qs[idx]={lwb(xs,l),lwb(xs,r),idx};
  634.     }
  635.     //cerr<<114514<<endl;
  636.     sort(all(qs),[&](const Q&a,const Q&b){
  637.         int x=pre[a.l]/B,y=pre[b.l]/B;
  638.         if(x!=y)return x<y;
  639.         return a.r<b.r;
  640.     });
  641.     ll ans=0;
  642.     auto clear=[&](int pos){
  643.         ans=0;
  644.         rep(col,u){
  645.             if(lrlist[col].a<=pos)gomi[col]=0;
  646.             else gomi[col]=buf[col].n-1;
  647.         }
  648.         for(auto [c,h]:unko[pos]){
  649.             gomi[c]=h;
  650.         }
  651.         rep(col,u){buf[col].clear(gomi[col]);}
  652.     };
  653.     auto addL=[&](int i){
  654.         for(auto [c,h]:unko[i]){
  655.             ans+=buf[c].left(h,1);
  656.         }
  657.     };
  658.     auto delL=[&](int i){
  659.         for(auto [c,h]:unko[i]){
  660.             ans+=buf[c].left(h,-1);
  661.         }
  662.     };
  663.     auto addR=[&](int i){
  664.         for(auto [c,h]:unko[i]){
  665.             ans+=buf[c].right(h,1);
  666.         }
  667.     };
  668.     auto delR=[&](int i){
  669.         for(auto [c,h]:unko[i]){
  670.             ans+=buf[c].right(h,-1);
  671.         }
  672.     };
  673.     int block=-1,b=0,e=0;
  674.     for(auto z:qs){
  675.         {
  676.             int cur=pre[z.l]/B;
  677.             if(block!=cur){
  678.                 block=cur;
  679.                 b=e=lwb(pre,cur*B);
  680.                 clear(b);
  681.             }
  682.         }
  683.         while(z.l<b){
  684.             addL(--b);
  685.         }
  686.         while(e<z.r){
  687.             addR(e++);
  688.         }
  689.         while(b<z.l){
  690.             delL(b++);
  691.         }
  692.         while(z.r<e){
  693.             delR(--e);
  694.         }
  695.         res[z.i]+=ans;
  696.     }
  697.     rep(i,m)pr.writeln(res[i]);
  698.     //cerr<<num<<endl;
  699. }
  700.  
  701. signed main(){
  702.     cin.tie(0);
  703.     ios::sync_with_stdio(0);
  704.     cout<<fixed<<setprecision(20);
  705.    
  706.     //int t;cin>>t;rep(_,t)
  707.     slv();
  708. }
  709.  
Advertisement
Add Comment
Please, Sign In to add comment