Advertisement
Guest User

Untitled

a guest
Jun 19th, 2016
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. /**
  2.  * Author: Sergey Kopeliovich (Burunduk30@gmail.com)
  3.  */
  4.  
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <vector>
  8. #include <algorithm>
  9.  
  10. using namespace std;
  11.  
  12. #define forn(i, n) for (int i = 0; i < (int)(n); i++)
  13. #define pb push_back
  14. #define mp make_pair
  15. #define all(a) (a).begin(), (a).end()
  16.  
  17. typedef long long ll;
  18.  
  19. const int N = 3e5;
  20.  
  21. int n, m, a[N], l[N], r[N];
  22. ll ans[N];
  23.  
  24. struct Event {
  25.     ll x;
  26.     int i, type;
  27.     bool operator < ( const Event &e ) const { return mp(x, type) < mp(e.x, e.type); }
  28. };
  29.  
  30. vector<Event> e;
  31.  
  32. template<const int N>
  33. struct Fenwick {
  34.     int n;
  35.     ll sum[N];
  36.    
  37.     void clear() {
  38.         memset(sum, 0, sizeof(sum));
  39.     }
  40.  
  41.     void add( int y, int d ) {
  42.         for (int x = y; x < N; x |= x + 1)
  43.             sum[x] += d;
  44.     }
  45.  
  46.     ll get( int x ) {
  47.         ll r = 0;
  48.         for (; x >= 0; x &= x + 1, x--)
  49.             r += sum[x];
  50.         return r;
  51.     }
  52.     ll get( int l, int r ) {
  53.         return get(r) - get(l - 1);
  54.     }
  55. };
  56. Fenwick<N> f;
  57.  
  58. inline int readChar();
  59. template <class T = int> inline T readInt();
  60. template <class T> inline void writeInt( T x, char end = 0 );
  61. inline void writeChar( int x );
  62. inline void flush();
  63.  
  64. void solve() {
  65.     n = readInt();
  66.     m = readInt();
  67.     forn(i, n) a[i] = readInt();
  68.     forn(i, m) {
  69.         l[i] = readInt() - 1;
  70.         r[i] = readInt() - 1;
  71.         ans[i] = 1;
  72.     }
  73.     forn(_, 50) {
  74.         e.clear();
  75.         forn(i, n) e.pb({a[i], i, 0});
  76.         forn(i, m) e.pb({ans[i], i, 1});
  77.         sort(all(e));
  78.         f.clear();
  79.         for (auto x : e)
  80.             if (x.type == 0)
  81.                 f.add(x.i, a[x.i]);
  82.             else
  83.                 ans[x.i] = f.get(l[x.i], r[x.i]) + 1;
  84.     }
  85.     forn(i, m) writeInt(ans[i], '\n');
  86.     flush();
  87. }
  88.  
  89. int main() {
  90.     solve();
  91. }
  92.  
  93. /** Read */
  94.  
  95. static const int buf_size = 4096;
  96.  
  97. inline int getChar() {
  98.     static char buf[buf_size];
  99.     static int len = 0, pos = 0;
  100.     if (pos == len)
  101.         pos = 0, len = fread(buf, 1, buf_size, stdin);
  102.     if (pos == len)
  103.         return -1;
  104.     return buf[pos++];
  105. }
  106.  
  107. inline int readChar() {
  108.     int c = getChar();
  109.     while (c <= 32)
  110.         c = getChar();
  111.     return c;
  112. }
  113.  
  114. template <class T>
  115. inline T readInt() {
  116.     int s = 1, c = readChar();
  117.     T x = 0;
  118.     if (c == '-')
  119.         s = -1, c = getChar();
  120.     while ('0' <= c && c <= '9')
  121.         x = x * 10 + c - '0', c = getChar();
  122.     return s == 1 ? x : -x;
  123. }
  124.  
  125. /** Write */
  126.  
  127. static int write_pos = 0;
  128. static char write_buf[buf_size];
  129.  
  130. inline void writeChar( int x ) {
  131.     if (write_pos == buf_size)
  132.         fwrite(write_buf, 1, buf_size, stdout), write_pos = 0;
  133.     write_buf[write_pos++] = x;
  134. }
  135.  
  136. inline void flush() {
  137.     if (write_pos)
  138.         fwrite(write_buf, 1, write_pos, stdout), write_pos = 0;
  139. }
  140.  
  141. template <class T>
  142. inline void writeInt( T x, char end ) {
  143.     if (x < 0)
  144.         writeChar('-'), x = -x;
  145.  
  146.     char s[24];
  147.     int n = 0;
  148.     while (x || !n)
  149.         s[n++] = '0' + x % 10, x /= 10;
  150.     while (n--)
  151.         writeChar(s[n]);
  152.     if (end)
  153.         writeChar(end);
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement