树状数组+二维前缀和(A.The beautiful values of the palace)--The Preliminary Contest for ICPC Asia Nanjing 2019

题意:

给你螺旋型的矩阵,告诉你那几个点有值,问你某一个矩阵区间的和是多少。

思路:

以后记住:二维前缀和sort+树状数组就行了!!!。

  1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
  2 #include <cstdio>//sprintf islower isupper
  3 #include <cstdlib>//malloc  exit strcat itoa system("cls")
  4 #include <iostream>//pair
  5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin);
  6 #include <bitset>
  7 //#include <map>
  8 //#include<unordered_map>
  9 #include <vector>
 10 #include <stack>
 11 #include <set>
 12 #include <string.h>//strstr substr
 13 #include <string>
 14 #include <time.h>//srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
 15 #include <cmath>
 16 #include <deque>
 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
 18 #include <vector>//emplace_back
 19 //#include <math.h>
 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
 23 #define fo(a,b,c) for(register int a=b;a<=c;++a)
 24 #define fr(a,b,c) for(register int a=b;a>=c;--a)
 25 #define mem(a,b) memset(a,b,sizeof(a))
 26 #define pr printf
 27 #define sc scanf
 28 #define ls rt<<1
 29 #define rs rt<<1|1
 30 typedef long long ll;
 31 void swapp(int &a,int &b);
 32 double fabss(double a);
 33 int maxx(int a,int b);
 34 int minn(int a,int b);
 35 int Del_bit_1(int n);
 36 int lowbit(int n);
 37 int abss(int a);
 38 //const long long INF=(1LL<<60);
 39 const double E=2.718281828;
 40 const double PI=acos(-1.0);
 41 const int inf=(1<<30);
 42 const double ESP=1e-9;
 43 const int mod=(int)1e9+7;
 44 const int N=(int)1e6+10;
 45 
 46 ll c[N],ans[N];
 47 void Init(int n)
 48 {
 49     for(int i=1;i<=n;++i)
 50         c[i]=ans[i]=0;
 51 }
 52 void add(int i,ll t)
 53 {
 54     while(i<=N)
 55     {
 56         c[i]+=t;
 57         i+=lowbit(i);
 58     }
 59 }
 60 ll sum(int i)
 61 {
 62     ll sum=0;
 63     while(i)
 64     {
 65         sum+=c[i];
 66         i-=lowbit(i);
 67     }
 68     return sum;
 69 }
 70 //-----------------------------------树状数组;
 71 struct node
 72 {
 73     int f;
 74     int x,y,ans_id;
 75     ll val;
 76     friend bool operator<(node a,node b)
 77     {
 78         if(a.y==b.y)
 79         {
 80             if(a.x==b.x)
 81                 return a.f<b.f;
 82             return a.x<b.x;
 83         }
 84         return a.y<b.y;
 85     }
 86 }p[N];
 87 ll re_val(ll x)
 88 {
 89     ll sum=0;
 90     while(x>0)
 91     {
 92         sum+=x%10;
 93         x/=10;
 94     }
 95     return sum;
 96 }
 97 long long index(long long y,long long x,long long n)
 98 {
 99     long long mid=(n+1)/2;
100     long long p=max(abs(x-mid),abs(y-mid));
101     long long ans=n*n-(1+p)*p*4;
102     long long sx=mid+p,sy=mid+p;
103     if(x==sx&&y==sy)
104         return ans;
105     else
106     {
107         if(y==sy||x==sx-2*p)
108             return ans+abs(x-sx)+abs(y-sy);
109         else
110             return ans+8*p-abs(x-sx)-abs(y-sy);
111     }
112 }
113 void solve(int n)
114 {
115     for(int i=1;i<=n;++i)
116     {
117         if(p[i].f) ans[p[i].ans_id]+=sum(p[i].x)*p[i].val;
118         else add(p[i].x,p[i].val);
119     }
120 }
121 
122 int main()
123 {
124     int T;
125     sc("%d",&T);
126     while(T--)
127     {
128         int n,m,ask;
129         sc("%d%d%d",&n,&m,&ask);
130         Init(N-3);
131         int cnt=0;
132         for(int i=1;i<=m;++i)
133         {
134             int x,y;
135             sc("%d%d",&x,&y);
136             p[++cnt]={0,x,y,-1,re_val(index(x,y,n))};
137         }
138         for(int i=1;i<=ask;++i)
139         {
140             int xl,yl,xr,yr;
141             sc("%d%d%d%d",&xl,&yl,&xr,&yr);
142             p[++cnt]={1,xl-1,yl-1,i,1};
143             p[++cnt]={1,xl-1,yr,i,-1};
144             p[++cnt]={1,xr,yl-1,i,-1};
145             p[++cnt]={1,xr,yr,i,1};
146         }
147         sort(p+1,p+1+cnt);
148         solve(cnt);
149         for(int i=1;i<=ask;++i)
150             pr("%lld\n",ans[i]);
151     }
152     return 0;
153 }
154 
155 /**************************************************************************************/
156 
157 int maxx(int a,int b)
158 {
159     return a>b?a:b;
160 }
161 
162 void swapp(int &a,int &b)
163 {
164     a^=b^=a^=b;
165 }
166 
167 int lowbit(int n)
168 {
169     return n&(-n);
170 }
171 
172 int Del_bit_1(int n)
173 {
174     return n&(n-1);
175 }
176 
177 int abss(int a)
178 {
179     return a>0?a:-a;
180 }
181 
182 double fabss(double a)
183 {
184     return a>0?a:-a;
185 }
186 
187 int minn(int a,int b)
188 {
189     return a<b?a:b;
190 }

 

posted @ 2019-09-02 15:36  ZMWLxh  阅读(597)  评论(0编辑  收藏  举报