Advertisement
Guest User

Untitled

a guest
Jun 21st, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <algorithm>
  8. #include <queue>
  9. #include <stack>
  10. #include <set>
  11. #include <map>
  12. #include <list>
  13. #include <vector>
  14. #include <string>
  15. #include <cstring>
  16. #include <cmath>
  17. #include <ctime>
  18. #include <cassert>
  19. #include <bitset>
  20.  
  21. using namespace std;
  22.  
  23. #define forn(i, n) for(int i = 0; i < (int)(n); i++)
  24. #define forn1(i, n) for(int i = 1; i <= (int)(n); i++)
  25. #define all(a) (a).begin(), (a).end()
  26. #define sz(a) (int)((a).size())
  27. #define mp make_pair
  28. #define pb push_back
  29. #define X first
  30. #define Y second
  31. #define x first
  32. #define y second
  33. #define y1 __y1
  34. #define sqr(x) ((x) * (x))
  35.  
  36. typedef long long li;
  37. typedef long double ld;
  38. typedef pair<int, int> pt;
  39.  
  40. const int INF = (int)(1e9);
  41. const li INF64 = (li)(INF) * (li)(INF);
  42. const ld eps = 1e-9;
  43. const ld pi = ld(3.1415926535897932384626433832795);
  44.  
  45. inline bool in(int i, int j, int n, int m)
  46. {
  47.     return i >= 1 && i <= n && j >= 1 && j <= m;
  48. }
  49.  
  50. inline int myrand()
  51. {
  52.     return (rand() ^ (rand() << 15));
  53. }
  54.  
  55. const int dx[] = {-1, 0, 1, 0};
  56. const int dy[] = {0, 1, 0, -1};
  57.  
  58. const int N = 2e5 + 555;
  59. const int L = 300;
  60.  
  61. li a[N];
  62. li sumB[N / L + 1];
  63. li addB[N / L + 1];
  64. li startB[N / L + 1];
  65. li n, m;
  66. li Na[N];
  67. li t[N], x[N], y[N];
  68. li idxB[N];
  69. li cntAdd[N / L + 1];
  70. li addtoB[N];
  71. li starts[N];
  72. li ADD[N];
  73.  
  74. inline void gen()
  75. {
  76.     n = 100000, m = 100000;
  77.  
  78.     forn(i, m)
  79.     {
  80.         t[i] = rand() % 2 + 1;
  81.         int x = myrand() % n + 1, y = myrand() % n + 1;
  82.         if(x > y)
  83.             swap(x, y);
  84.         ::x[i] = x, ::y[i] = y;
  85.     }
  86.  
  87.     /*
  88.     n = 10;
  89.     m = 100;
  90.  
  91.     t[0] = 1, x[0] = 1, y[0] = 10;
  92.     t[1] = 1, x[1] = 3, y[1] = 7;
  93.     t[2] = 2, x[2] = 2, y[2] = 4;
  94.  
  95.     for(int i = 3; i < m; i++)
  96.     {
  97.         t[i] = 2;
  98.         int x = rand() % n + 1, y = rand() % n + 1;
  99.         if(x > y)
  100.             swap(x, y);
  101.         ::x[i] = x, ::y[i] = y;
  102.     }*/
  103.  
  104.     return;
  105. }
  106.  
  107. inline bool read()
  108. {
  109.     //gen();
  110.     //return true;
  111.     if(scanf("%d %d", &n, &m) != 2)
  112.         return false;
  113.  
  114.     forn(i, m)
  115.     {
  116.         assert(scanf("%d %d %d", &t[i], &x[i], &y[i]) == 3);
  117.     }
  118.  
  119.     return true;
  120. }
  121.  
  122. inline void naiveUpd(int lf, int rg)
  123. {
  124.     li start = 2, add = 4;
  125.     for(int i = lf; i <= rg; i++)
  126.     {
  127.         Na[i] += start;
  128.         start += add;
  129.         add += 2;
  130.     }
  131.  
  132.     return;
  133. }
  134.  
  135. const li MOD = INF + 7;
  136. inline void add(li &a, const li &b)
  137. {
  138.     a += b;
  139.     //if(a >= MOD)
  140.     //  a -= MOD;
  141.     return;
  142. }
  143.  
  144. inline int naiveGet(int lf, int rg)
  145. {
  146.     li ans = 0;
  147.     for(int i = lf; i <= rg; i++)
  148.     {
  149.         ans = (ans + Na[i]) % MOD;
  150.     }
  151.  
  152.     return int(ans);
  153. }
  154.  
  155. inline void upd(int lf, int rg)
  156. {
  157.     int LB = idxB[lf];
  158.     int RB = idxB[rg];
  159.  
  160.     if(LB == RB)
  161.     {
  162.         li start = 2, addd = 4;
  163.         for(int i = lf; i <= rg; i++)
  164.         {
  165.             add(a[i], start);
  166.             add(sumB[LB], start);
  167.             add(start, addd);
  168.             add(addd, 2);
  169.         }
  170.  
  171.         return;
  172.     }
  173.  
  174.     li start = 2, addd = 4;
  175.  
  176.     for(int i = lf; i <= LB * L; i++)
  177.     {
  178.         add(a[i], start);
  179.         add(sumB[LB], start);
  180.         add(start, addd);
  181.         add(addd, 2);
  182.     }
  183.  
  184.     //int cnt = LB * L - lf;
  185.     //int last = 2 + (cnt - 1) * 2;
  186.  
  187.     for(int i = LB + 1; i <= RB - 1; i++)
  188.     {
  189.         cntAdd[i]++;
  190.         int id = (i - 1) * L + 1 - lf + 1;
  191.         /*cerr << "i == " << i << endl;
  192.         cerr << "id == " << id << endl;
  193.         cerr << "starts ADD == " << starts[id] << ' ' << ADD[id] << endl;*/
  194.         add(sumB[i], addtoB[id]);
  195.         add(startB[i], starts[id]);
  196.         add(addB[i], ADD[id]);
  197.     }
  198.  
  199.     //cerr << "RBdx == " << (RB - 1) * L + 1 << endl;
  200.     start = starts[(RB - 1) * L + 1 - lf + 1];
  201.     addd = ADD[(RB - 1) * L + 1 - lf + 1];
  202.     //cerr << "start addd == " << start << ' ' << addd << endl;
  203.  
  204.     for(int i = (RB - 1) * L + 1; i <= rg; i++)
  205.     {
  206.         add(a[i], start);
  207.         add(sumB[RB], start);
  208.         add(start, addd);
  209.         add(addd, 2);
  210.     }
  211.  
  212.     return;
  213. }
  214.  
  215. li binpow(li a, li b)
  216. {
  217.     if(b == 0)
  218.         return 1;
  219.     if(b % 2 == 1)
  220.         return (a * binpow(a, b - 1)) % MOD;
  221.     li cur = binpow(a, b / 2) % MOD;
  222.     return (cur * cur) % MOD;
  223. }
  224.  
  225. li mul = 1LL;
  226.  
  227. inline int get(int lf, int rg)
  228. {
  229.     int LB = idxB[lf];
  230.     int RB = idxB[rg];
  231.  
  232.     if(LB == RB)
  233.     {
  234.         li ans = 0;
  235.         for(int i = lf; i <= rg; i++)
  236.         {
  237.             //add(ans, a[i]);
  238.         }
  239.  
  240.         /*int cnt = min(L, n - LB * L);
  241.         int first = startB[LB];
  242.         int last = int(first * 1LL * (cnt - 1) % MOD);
  243.         add(last, first);
  244.         int sum = int((first + last) * 1LL * cnt / 2 % MOD);*/
  245.  
  246.         for(int i = lf; i <= rg; i++)
  247.         {
  248.             li first = addB[LB];
  249.             li lfidx = (LB - 1) * L + 1;
  250.             li cnt = i - lfidx;
  251.             li last = li((first + (cnt - 1) * 1LL * cntAdd[LB] * 2));
  252.             li ff = first % MOD;
  253.             li ll = ((first + (cnt - 1) * 1LL * cntAdd[LB] % MOD)) % MOD;
  254.  
  255.             li sum = li((first + last) * 1LL * cnt / 2);
  256.  
  257.             li ssum = (first + last) * 1LL * cnt % MOD;
  258.             ssum = (ssum * mul) % MOD;
  259.             add(ssum, startB[LB]);
  260.             add(ssum, a[i]);
  261.             add(ans, ssum);
  262.         }
  263.  
  264.         assert(ans >= 0);
  265.         return ans % MOD;
  266.     }
  267.  
  268.     li ans = 0;
  269.  
  270.     for(int i = lf; i <= LB * L; i++)
  271.     {
  272.         li first = addB[LB];
  273.         li lfidx = (LB - 1) * L + 1;
  274.         li cnt = i - lfidx;
  275.         li last = li((first + (cnt - 1) * 1LL * cntAdd[LB] * 2));
  276.         li ff = first % MOD;
  277.         li ll = ((first + (cnt - 1) * 1LL * cntAdd[LB] % MOD)) % MOD;
  278.  
  279.         li sum = li((first + last) * 1LL * cnt / 2);
  280.  
  281.         li ssum = (first + last) * 1LL * cnt % MOD;
  282.         ssum = (ssum * mul) % MOD;
  283.         add(ssum, startB[LB]);
  284.         add(ssum, a[i]);
  285.         add(ans, ssum);
  286.     }
  287.  
  288.     for(int i = LB + 1; i <= RB - 1; i++)
  289.     {
  290.         add(ans, sumB[i]);
  291.         ans %= MOD;
  292.     }
  293.  
  294.  
  295.     for(int i = (RB - 1) * L + 1; i <= rg; i++)
  296.     {
  297.         //add(ans, a[i]);
  298.     }
  299.  
  300.     for(int i = (RB - 1) * L + 1; i <= rg; i++)
  301.     {
  302.         li first = addB[RB];
  303.         li lfidx = (RB - 1) * L + 1;
  304.         li cnt = i - lfidx;
  305.         li last = li((first + (cnt - 1) * 1LL * cntAdd[RB] * 2));
  306.         li ff = first % MOD;
  307.         li ll = ((first + (cnt - 1) * 1LL * cntAdd[RB] % MOD)) % MOD;
  308.  
  309.         li sum = li((first + last) * 1LL * cnt / 2);
  310.  
  311.         li ssum = (first + last) * 1LL * cnt % MOD;
  312.         ssum = (ssum * mul) % MOD;
  313.         add(ssum, startB[RB]);
  314.         add(ssum, a[i]);
  315.         add(ans, ssum);
  316.     }
  317.  
  318.     assert(ans >= 0);
  319.     return ans % MOD;
  320. }
  321.  
  322. inline void solve()
  323. {
  324.     for(int i = 1; i <= n; i++)
  325.     {
  326.         int idx = i / L + 1;
  327.         if(i % L == 0)
  328.             idx--;
  329.         idxB[i] = idx;
  330.     }
  331.  
  332.     forn(i, m)
  333.     {
  334.         int l = x[i], r = y[i];
  335.         int t = ::t[i];
  336.         //cerr << "QEURY" << endl;
  337.         //cerr << t << ' ' << l << ' ' << r << endl;
  338.  
  339.         if(t == 1)
  340.         {
  341.             upd(l, r);
  342.             //naiveUpd(l, r);
  343.         }
  344.         else
  345.         {
  346.             int ans = get(l, r);
  347.             printf("%d\n", ans);
  348.             /*int nans = naiveGet(l, r);
  349.             if(ans != nans)
  350.             {
  351.                 cerr << ans << ' ' << nans << endl;
  352.                 assert(false);
  353.             }
  354.  
  355.             assert(ans == nans);*/
  356.         }
  357.     }
  358.  
  359.     /*forn1(i, 10)
  360.     {
  361.         cerr << "addB[i] == " << addB[i] << ' ' << startB[i] << endl;
  362.     }
  363.  
  364.     forn1(i, n)
  365.         cerr << a[i] << ' ';
  366.     cerr << endl;*/
  367.     return;
  368. }
  369.  
  370. li arr[N];
  371. li sums[N];
  372.  
  373. int main() {
  374. #ifdef _DEBUG
  375.     assert(freopen("input.txt", "rt", stdin));
  376.     assert(freopen("output.txt", "wt", stdout));
  377. #endif
  378.  
  379.     cout << setprecision(10) << fixed;
  380.     cerr << setprecision(10) << fixed;
  381.  
  382.     srand(int(time(NULL)));
  383.  
  384.     mul = binpow(2, MOD - 2);
  385.  
  386.     //for( ; ; )
  387.     {
  388.         //cerr << "!" << endl;
  389.         assert(read());
  390.  
  391.         //cerr << "!" << endl;
  392.         li ss = 2, dd = 4;
  393.         //cerr << "n == " << n << endl;
  394.         for(int i = 1; i <= n; i++)
  395.         {
  396.             //cerr << "i == " << i << ' ' << ss << endl;
  397.             arr[i] = ss;
  398.             ss += dd;
  399.             dd += 2;
  400.         }
  401.  
  402.         //forn1(i, 10)
  403.         //  cerr << arr[i] << ' ';
  404.         //cerr << endl;
  405.  
  406.         for(int i = 1; i <= n; i++)
  407.         {
  408.             sums[i] = sums[i - 1] + arr[i];
  409.         }
  410.  
  411.         for(int i = 1; i <= n; i++)
  412.         {
  413.             starts[i] = li(arr[i]);
  414.             ADD[i] = li((arr[i + 1] - arr[i]));
  415.         }
  416.  
  417.  
  418.         for(int i = 1; i <= n; i++)
  419.         {
  420.             int rg = i + L - 1;
  421.             if(i <= n)
  422.             {
  423.                 addtoB[i] = li((sums[rg] - sums[i - 1]));
  424.             }
  425.         }
  426.         solve();
  427.     }
  428.  
  429. #ifdef _DEBUG
  430.     cerr << "TIME == " << clock() << " ms" << endl;
  431. #endif
  432.     return 0;
  433. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement