1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<bits/stdc++.h>
using namespace std;
int n,q,l,r,piterek,karm,ter;
bool rozw[300002];
long long k,a[300002],pref[300002],depe[300002],odp[300002];
vector<pair<int,int>>poczatek[300002];
vector<pair<int,pair<int,int>>>koniec[300002];
int main()
{
    scanf("%d%lld%d",&n,&k,&q);
    for(int i=1;i<=n;++i)
    {
        scanf("%lld",&a[i]);
        pref[i]=pref[i-1]+a[i];
    }
    for(int kar=0;kar<q;++kar)
    {
        scanf("%d%d",&l,&r);
        poczatek[l].push_back(make_pair(r,kar));
        koniec[r].push_back(make_pair(0,make_pair(l,kar)));
    }
    for(int sal=1;sal<=n;++sal)
    {
        if(poczatek[sal].size()>=2)
        {
            sort(poczatek[sal].begin(),poczatek[sal].end());
            piterek=0;
            karm=poczatek[sal].size();
            ter=sal;
            depe[sal-1]=0;
            while(piterek<karm)
            {
                depe[ter]=depe[ter-1];
                if(ter-k+1>=sal)
                depe[ter]=max(depe[ter],depe[ter-k]+pref[ter]-pref[ter-k]);
                while(piterek<karm)
                {
                    if(ter!=poczatek[sal][piterek].first)
                    break;
                    rozw[poczatek[sal][piterek].second]=1;
                    odp[poczatek[sal][piterek].second]=depe[ter];
                    ++piterek;
                }
                ++ter;
            }
        }
        piterek=0;
        ter=sal;
        depe[sal+1]=0;
        for(int i=koniec[sal].size()-1;i>=0;--i)
        {
            if(rozw[koniec[sal][i].second.second]==1)
            {
                koniec[sal][i].first=1;
            }
            else
            ++piterek;
        }
        --piterek;
        sort(koniec[sal].begin(),koniec[sal].end());
        while(piterek>=0)
        {
            depe[ter]=depe[ter+1];
            if(ter+k-1<=sal)
            depe[ter]=max(depe[ter],depe[ter+k]+pref[ter+k-1]-pref[ter-1]);
            while(piterek>=0)
            {
                if(ter!=koniec[sal][piterek].second.first)
                break;
                rozw[koniec[sal][piterek].second.second]=1;
                odp[koniec[sal][piterek].second.second]=depe[ter];
                --piterek;
            }
            --ter;
        }
    }
    for(int kar=0;kar<q;++kar)
    {
        printf("%lld\n",odp[kar]);
    }
    return 0;
}