Monday, 26 October 2015

SUBSTRINGS COUNT

SUBSTRINGS COUNT
Attempted by: 497
|
Solved by: 60
|
Partially Solved by: 353
|

 
  • Algorithms
  • Binary Search
  • Data Structures
  • Hashing
  • Medium
  Edit
Problem
Editorial
My Submissions
Analytics
Jack is the most intelligent student in the class.To boost his intelligence,his class teacher gave him a problem named"Substring Count".
Problem :
His Class teacher gave him n strings numbered from 1 to n which consists of only lowercase letters (each having length not more than 10) and then ask Q questions related to the given strings.
Each question is described by the 2 integers L,R and a string str,Now his teacher wants to know how many strings numbered from L to R contains str as a substrings.
As expected, Jack solved this problem with in a minute but he failed to solve it efficiently.Now ,its your turn to teach him.How to do it efficiently?? and save him from punishment.
INPUT
First line of input contains a single integer n denoting the number of strings given by class teacher to jack.Next n lines of input contains n strings (one per line).Next line fo input contains a single integer Q denoting the number of questions asked by his teacher.next Q lines of input contains Q question (one per line) as explained above.
OUTPUT
print the correct answer for each of the question asked by his teacher.
CONSTRAINTS
1<=n<=10000
1<=strlen(str)<=10
1<=Q<=5*10^5
1<=L,R<=n
NOTE: strings consist of only lower case characters

Sample Input
(Plaintext Link)
3
code
coder
coding
2
1 3 code
1 3 co
Sample Output
(Plaintext Link)
2 
3
Explanation
Q1:: code coder coding only two out of 3 strings contain cod as substring Q2:: code coder coding all the 3 strings contain co as substring

-------------------------------------CODE-----------------------------------------------------------------------------------------------------
GENERATE ALL POSSIBLE SUB STRING OF ALL STRING WHICH CAN BE DONE IN N*45(SINCE LENGTH OF EACH STRING =10 SO 45 SUBSTRINGS IN EACH STRING )

NOW WE  WHENE WE ARE  ADDING ANY INDEX , INCREASE HAS COUNT OF ALL SUBSTRING AT THAT THAT CAN BE FORMED BY STRING AT THAT INDEX (ALL SUBSTRINGS ) ,SIMILARLY FOR DELETE , 

-----------------------------------------------------CODE-----------------------------------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
//map<map<string,int> > ma;
string ss[1000000];
typedef int lli;
int result[1000005];
vector<string>v[1000005];
struct node
 {
   lli x,y,index;
   string str;
   
 } qry[1000000];

 map<string,int> has;
 long long int ans=0;

  lli bck_sz=555;
  
  lli read_int()
  {
 char r;
 bool start=false,neg=false;
  lli ret=0;
 while(true){
  r=getchar();
  if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
   continue;
  }
  if((r-'0'<0 || r-'0'>9) && r!='-' && start){
   break;
  }
  if(start)ret*=10;
  start=true;
  if(r=='-')neg=true;
  else ret+=r-'0';
 }
 if(!neg)
  return ret;
 else
  return -ret;
}

bool compare(node a,node b)
{
    lli i,j;
    i=a.x/bck_sz;
    j=b.x/bck_sz;
    if(i==j)
    {
        if(a.y<b.y)
            return 1;
        else
            return 0;
    }
    else{
        if(i<j)
            return 1;
        else
            return 0;
    }
}

int join(int index)
{
int l=v[index].size();
for(int i=0;i<l;i++)
has[v[index][i]]++;
}

int del(int index)
{
int l=v[index].size();
for(int i=0;i<l;i++)
has[v[index][i]]--;
}



int main()
 {
 int n;
 cin>>n;
 for(int i=0;i<n;i++)
  {
  string s;
  cin>>ss[i];
  int len=ss[i].length();
  map<string,int> mp;
  for(int k=0;k<len;k++)
  {
  string p;
 
  for(int j=k;j<len;j++)
   {
    p+=ss[i][j];
    if(!mp[p])
    v[i].push_back(p);
    mp[p]++;
   
}
  }
  mp.clear();
  
 
 
 }
 
 
 
 
 int q;
 cin>>q;
 for(int i=0;i<q;i++)
  {
    int a,b;
    string s;
     cin>>a>>b;
      cin>>s;
     qry[i].x=a-1;
     qry[i].y=b-1;
     qry[i].index=i;
      qry[i].str=s;
     
  }
  sort(qry,qry+q,compare);
  
 int  r=-1;
int  l=0;
for(int  i=0;i<q;i++)
 {
  int  x=qry[i].x;
               int  y=qry[i].y;
               int  index=qry[i].index;
               string s=qry[i].str;
               // cout<<" query in the range "<<x<<" "<<y<<endl;
                        
                    while(r<y)
                              {

                                  r++;
                           
                                    join(r);
                          }
                          
              while(l>x)
                {
                   l--;
                  join(l);
                  }
    while(l<x)
    {
        del(l);
        l++;
    }
    while(r>y)
    {
        del(r);
        r--;
    }
    
    result[index]=has[s];
   
 }
 
 for(lli i=0;i<q;i++)
   printf("%lld\n",result[i]);
   
}
 
 




D. Powerful array

D. Powerful array

An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, al + 1..., ar, where 1 ≤ l ≤ r ≤ n. For every positive integer s denote by Ks the number of occurrences of s into the subarray. We call the power of the subarray the sum of productsKs·Ks·s for every positive integer s. The sum contains only finite number of nonzero summands as the number of different values in the array is indeed finite.
You should calculate the power of t given subarrays.
Input
First line contains two integers n and t (1 ≤ n, t ≤ 200000) — the array length and the number of queries correspondingly.
Second line contains n positive integers ai (1 ≤ ai ≤ 106) — the elements of the array.
Next t lines contain two positive integers lr (1 ≤ l ≤ r ≤ n) each — the indices of the left and the right ends of the corresponding subarray.
Output
Output t lines, the i-th line of the output should contain single positive integer — the power of the i-th query subarray.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout stream (also you may use%I64d).
Sample test(s)
input
3 2
1 2 1
1 2
1 3
output
3
6
input
8 3
1 1 2 2 1 3 1 1
2 7
1 6
2 7
output
20
20
20
Note
Consider the following array (see the second sample) and its [2, 7] subarray (elements of the subarray are colored):
Then K1 = 3K2 = 2K3 = 1, so the power is equal to 32·1 + 22·2 + 12·3 = 20.

-------------------------------------code------------------------------------------------------------------------------
#include<iostream>
using namespace std;
typedef long long int lli;
#include<bits/stdc++.h>
lli arr[200005];
long long int result[2000005];
struct node
 {
   lli x,y,index;
   
 } qry[1000000];
int has[1000005];
 long long int ans=0;
 
  lli bck_sz=441;
  
  lli read_int()
  {
 char r;
 bool start=false,neg=false;
  lli ret=0;
 while(true){
  r=getchar();
  if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
   continue;
  }
  if((r-'0'<0 || r-'0'>9) && r!='-' && start){
   break;
  }
  if(start)ret*=10;
  start=true;
  if(r=='-')neg=true;
  else ret+=r-'0';
 }
 if(!neg)
  return ret;
 else
  return -ret;
}

bool compare(node a,node b)
{
    lli i,j;
    i=a.x/bck_sz;
    j=b.x/bck_sz;
    if(i==j)
    {
        if(a.y<b.y)
            return 1;
        else
            return 0;
    }
    else{
        if(i<j)
            return 1;
        else
            return 0;
    }
}

lli join(lli num)
 {
 //  cout<<" adding "<<num<<endl;
  
     ans-=num*has[num]*has[num];
    has[num]++;
    ans+=num*has[num]*has[num];
  // cout<<" after adding ans is "<<ans<<endl;
   
 }
 lli del(lli num)
  {
    ans-=num*has[num]*has[num];
    has[num]--;
    ans+=num*has[num]*has[num];
    
  }

 int  main()
  {
    lli n,q;
    // cin>>n>>q;
scanf("%lld%lld",&n,&q);
      for(lli i=0;i<n;i++)
      {
        scanf("%lld",&arr[i]);
        
   }
   for(lli i=0;i<q;i++)
    {
        lli l,r;
scanf("%lld%lld",&l,&r);
        //  l=read_int();
          //r=read_int();
          l--;
          r--;
          
         qry[i].x=l;
         qry[i].y=r;
         qry[i].index=i;
         
    }
    
    sort(qry,qry+q,compare);
    
   lli r=-1;
   lli l=0;
   for(lli i=0;i<q;i++)
    {
      lli x=qry[i].x;
               lli y=qry[i].y;
               lli index=qry[i].index;
               // cout<<" query in the range "<<x<<" "<<y<<endl;
                        
                    while(r<y)
                              {

                                  r++;
                           
                                    join(arr[r]);
                          }
                          
              while(l>x)
                {
                   l--;
                  join(arr[l]);
                  }
    while(l<x)
    {
        del(arr[l]);
        l++;
    }
    while(r>y)
    {
        del(arr[r]);
        r--;
    }
    
    result[index]=ans;
       
    }
    
    for(lli i=0;i<q;i++)
     printf("%lld\n",result[i]);
   
  }

Sunday, 25 October 2015

DQUERY - D-query

DQUERY - D-query


Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) (1 ≤ i ≤ j ≤ n). For each d-query (i, j), you have to return the number of distinct elements in the subsequence ai, ai+1, ..., aj.

Input

  • Line 1: n (1 ≤ n ≤ 30000).
  • Line 2: n numbers a1, a2, ..., an (1 ≤ ai ≤ 106).
  • Line 3: q (1 ≤ q ≤ 200000), the number of d-queries.
  • In the next q lines, each line contains 2 numbers i, j representing a d-query (1 ≤ i ≤ j ≤ n).

Output

  • For each d-query (i, j), print the number of distinct elements in the subsequence ai, ai+1, ..., aj in a single line.

Example

Input
5
1 1 2 1 3
3
1 5
2 4
3 5

Output
3
2
3 
----------------------------------------code-------------------------------------------------------------#include<iostream>
#include<bits/stdc++.h>
using namespace std;
  int ans=0;
struct node
 {
   int x,y,index;
   
 } qry[1000000];
 
  int bck_sz=441;
   int result[1000000];
    int has[10000000];
    int arr[10000000];
 
 int read_int(){
 char r;
 bool start=false,neg=false;
  int ret=0;
 while(true){
  r=getchar();
  if((r-'0'<0 || r-'0'>9) && r!='-' && !start){
   continue;
  }
  if((r-'0'<0 || r-'0'>9) && r!='-' && start){
   break;
  }
  if(start)ret*=10;
  start=true;
  if(r=='-')neg=true;
  else ret+=r-'0';
 }
 if(!neg)
  return ret;
 else
  return -ret;
}

 bool compare(node a,node b)
{
    int i,j;
    i=a.x/bck_sz;
    j=b.x/bck_sz;
    if(i==j)
    {
        if(a.y<b.y)
            return 1;
        else
            return 0;
    }
    else{
        if(i<j)
            return 1;
        else
            return 0;
    }
}

int join(int num)
 {
   //cout<<" join "<<num<<"ans is "<<ans<<endl;
  has[num]++;
  if(has[num]==1) ans++;
 // cout<<" join "<<num<<"ans is "<<ans<<endl;
  
 }
 
 int  del(int num)
  {
   
    has[num]--;
    if(has[num]==0) ans--;
    //cout<<"del "<<num<<" ans is "<<ans<<endl;
  }
  
 int main()
  {
    int n;
     cin>>n;
      for(int i=0;i<n;i++)
      arr[i]=read_int();
       int q;
        cin>>q;
        for(int i=0;i<q;i++)
         {
           int a,b;
            a=read_int();
            b=read_int();
              //cin>>a>>b;
              a-=1;
              b-=1;
              qry[i].x=a;
              qry[i].y=b;
              qry[i].index=i;
              
   }
   
   sort(qry,qry+q,compare);
   int l=0,r=-1;
   
   for(int i=0;i<q;i++)
    {
      int x=qry[i].x;
      int y=qry[i].y;
     //  cout<<" query in the range "<<x<<" "<<y<<endl;
      int index=qry[i].index;
      while(r<y)
                   {

                       r++;
                           // cout<<"r "<<r<<endl;
                        join(arr[r]);
                          }
                          
              while(l>x)
                {
                   l--;
                  join(arr[l]);
                  }
    while(l<x)
    {
        del(arr[l]);
        l++;
    }
    while(r>y)
    {
        del(arr[r]);
        r--;
    }
    
    result[index]=ans;
    
    }
    for(int i=0;i<q;i++) printf("%d\n",result[i]);
  }