Tranvick

Untitled

Mar 2nd, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:65777216")
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cstdio>
  7. #include <algorithm>
  8. #include <cmath>
  9. #include <vector>
  10. #include <set>
  11. #include <stack>
  12. #include <map>
  13. #include <queue>
  14. #include <string>
  15. #include <memory.h>
  16. #include <iterator>
  17. #define y1 trololoy1
  18. #define y0 trololoy0
  19. #define mem(A,X) memset(A,X,sizeof(A))
  20. #define memo(A) memset(A,0,sizeof(A))
  21. #define forn(I,B) for (int I=1;I<=(B);I++)
  22. #define forg(H,V) for (int H=first[V];h;h=next[H])
  23. #define rep(I,B) for (int I=0;I<(B);I++)
  24. #define labs(X) (((X)>0)?(X):(-(X)))
  25. #define ropen(X) freopen(X,"r",stdin)
  26. #define wopen(X) freopen(X,"w",stdout)
  27. #define rwopen(X) freopen(X".in","r",stdin);freopen(X".out","w",stdout)
  28. #define pb push_back
  29. #define mp make_pair
  30. #define all(X) (X).begin(),(X).end()
  31. #define sqr(X) ((X)*(X))
  32.  
  33. using namespace std;
  34.  
  35. typedef pair <int,int> pii;
  36. typedef double ld;
  37. typedef long long ll;
  38. typedef pair <ll,ll> pll;
  39. typedef vector<int> vi;
  40. const int N=255555;
  41. const int INF=111111111;
  42. const double eps=1e-9;
  43. const double pi=3.14159265358979;
  44.  
  45. struct tree{
  46.     int c[10],l,r,p;
  47. } t[N+N+N];
  48.  
  49. int n,m,old[10],q;
  50. char s[N];
  51.  
  52. inline void push(int v){
  53.     t[v].p%=10;
  54.     if (!t[v].p) return;
  55.     rep(i,10) old[i]=t[v].c[i];
  56.     rep(i,10) t[v].c[i]=old[(i-t[v].p+10)%10];
  57.     t[v+v].p+=t[v].p;t[v+v+1].p+=t[v].p;
  58.     t[v].p=0;
  59. }
  60.  
  61. void build(int v,int l,int r){
  62.     t[v].l=l;t[v].r=r;t[v].p=0;
  63.     if (l==r) t[v].c[s[l-1]-'0']=1;
  64.     else{
  65.         build(v+v,l,(l+r)/2);
  66.         build(v+v+1,(l+r)/2+1,r);
  67.         rep(i,10) t[v].c[i]=t[v+v].c[i]+t[v+v+1].c[i];
  68.     }
  69. }
  70.  
  71. int get_sum(int v,int l,int r){
  72.     if (t[v].l>r || t[v].r<l) return 0;
  73.     push(v);
  74.     if (t[v].l>=l && t[v].r<=r){
  75.         q=0;
  76.         rep(i,10) q+=t[v].c[i]*i;
  77.         return q;
  78.     }
  79.     return get_sum(v+v,l,r)+get_sum(v+v+1,l,r);
  80. }
  81.  
  82. void upd(int v,int l,int r){
  83.     if (t[v].l>r || t[v].r<l) return;
  84.     if (t[v].l>=l && t[v].r<=r){
  85.         ++t[v].p;
  86.         return;
  87.     }
  88.     push(v);
  89.     upd(v+v,l,r);
  90.     upd(v+v+1,l,r);
  91.     rep(i,10) t[v].c[i]=t[v+v].c[i]+t[v+v+1].c[i];
  92. }
  93.  
  94. int main(){
  95.     ropen("input.txt");
  96.     wopen("output.txt");
  97.     scanf("%d%d\n",&n,&m);
  98.     gets(s);
  99.     build(1,1,n);
  100.     forn(i,m){
  101.         int a,b;
  102.         scanf("%d%d",&a,&b);
  103.         printf("%d\n",get_sum(1,a,b));
  104.         upd(1,a,b);
  105.     }
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment